This commit is contained in:
64
internal/bot/telegram.go
Normal file
64
internal/bot/telegram.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
// TelegramClientAdapter адаптирует библиотеку tgbotapi под наш интерфейс interfaces.TelegramClient.
|
||||
type TelegramClientAdapter struct {
|
||||
api *tgbotapi.BotAPI
|
||||
cacheChatID int64
|
||||
}
|
||||
|
||||
// NewTelegramClientAdapter создает новый адаптер.
|
||||
func NewTelegramClientAdapter(api *tgbotapi.BotAPI, cacheChatID int64) *TelegramClientAdapter {
|
||||
return &TelegramClientAdapter{
|
||||
api: api,
|
||||
cacheChatID: cacheChatID,
|
||||
}
|
||||
}
|
||||
|
||||
// SendAudioToCacheChannel загружает аудиофайл в кэш-канал и возвращает его FileID.
|
||||
func (t *TelegramClientAdapter) SendAudioToCacheChannel(ctx context.Context, audioPath, title, performer string) (string, error) {
|
||||
audio := tgbotapi.NewAudio(t.cacheChatID, tgbotapi.FilePath(audioPath))
|
||||
audio.Title = title
|
||||
audio.Performer = performer
|
||||
|
||||
msg, err := t.api.Send(audio)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to send audio to cache channel: %w", err)
|
||||
}
|
||||
if msg.Audio == nil {
|
||||
return "", fmt.Errorf("sent message does not contain audio")
|
||||
}
|
||||
|
||||
slog.Debug("Audio sent to cache channel", "file_id", msg.Audio.FileID)
|
||||
return msg.Audio.FileID, nil
|
||||
}
|
||||
|
||||
// AnswerInlineQuery отвечает на inline-запрос.
|
||||
func (t *TelegramClientAdapter) AnswerInlineQuery(ctx context.Context, queryID string, results []interface{}) error {
|
||||
inlineConfig := tgbotapi.InlineConfig{
|
||||
InlineQueryID: queryID,
|
||||
Results: results,
|
||||
CacheTime: 1, // Кэшируем результат на стороне Telegram на 1 секунду
|
||||
}
|
||||
|
||||
if _, err := t.api.Request(inlineConfig); err != nil {
|
||||
return fmt.Errorf("failed to answer inline query: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendMessage отправляет текстовое сообщение.
|
||||
func (t *TelegramClientAdapter) SendMessage(ctx context.Context, chatID int64, text string) error {
|
||||
msg := tgbotapi.NewMessage(chatID, text)
|
||||
if _, err := t.api.Send(msg); err != nil {
|
||||
return fmt.Errorf("failed to send message: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user