Add video support, extract command handler

This commit is contained in:
2024-08-04 11:20:01 +03:00
parent 118a25da71
commit 548c5817da
5 changed files with 92 additions and 28 deletions
+5 -2
View File
@@ -20,9 +20,12 @@ func main() {
}
log.Printf("Authorized on account %s", bot.Self.UserName)
botAPI := infrastructure.NewBotAPI(bot, conf.OwnerChatID)
errorsChan := make(chan error)
service := app.NewAnonymousQuestionsService(botAPI, errorsChan)
botAPI := infrastructure.NewBotAPI(bot, conf.OwnerChatID)
commandHandler := app.NewCommandHandler(botAPI)
service := app.NewAnonymousQuestionsService(errorsChan, commandHandler, botAPI)
go func() {
for err := range errorsChan {
log.Println(err)
+13 -22
View File
@@ -1,9 +1,14 @@
package app
func NewAnonymousQuestionsService(api BotAPI, errorsChan chan error) AnonymousMessagesService {
func NewAnonymousQuestionsService(
errorsChan chan error,
commandHandler CommandHandler,
api BotAPI,
) AnonymousMessagesService {
return &anonymousMessagesService{
api: api,
errorsChan: errorsChan,
errorsChan: errorsChan,
commandHandler: commandHandler,
api: api,
}
}
@@ -17,14 +22,15 @@ type AnonymousMessagesService interface {
}
type anonymousMessagesService struct {
api BotAPI
errorsChan chan error
errorsChan chan error
commandHandler CommandHandler
api BotAPI
}
func (s *anonymousMessagesService) ServeMessages() error {
return s.api.HandleUpdates(func(update MessageUpdate) {
if update.Command != nil {
err := s.handleCommand(update)
err := s.commandHandler.HandleCommand(update)
if err != nil {
s.errorsChan <- err
}
@@ -43,22 +49,6 @@ func (s *anonymousMessagesService) ServeMessages() error {
})
}
func (s *anonymousMessagesService) handleCommand(update MessageUpdate) error {
if update.Command == nil {
return nil
}
var msgText string
switch *update.Command {
case StartCommand:
msgText = "Жду твоих вопросов!"
case UnknownCommand:
msgText = "Неизвестная команда!"
}
return s.api.SendMessage(update.FromChatID, Message{Text: msgText})
}
func (s *anonymousMessagesService) pingClient(chatID int64) error {
return s.api.SendMessage(chatID, Message{Text: messageSentReply})
}
@@ -68,5 +58,6 @@ func (s *anonymousMessagesService) handleMessage(message Message) error {
return s.api.SendMessageToOwner(Message{
Text: msgText,
Image: message.Image,
Video: message.Video,
})
}
+10 -4
View File
@@ -18,6 +18,15 @@ type MessageUpdate struct {
type Message struct {
Text string
Image *Image
Video *Video
}
type Image struct {
FileID string
}
type Video struct {
FileID string
}
type Command int
@@ -25,8 +34,5 @@ type Command int
const (
UnknownCommand Command = iota
StartCommand
InfoCommand
)
type Image struct {
FileID string
}
+31
View File
@@ -0,0 +1,31 @@
package app
func NewCommandHandler(api BotAPI) CommandHandler {
return &commandHandler{
api: api,
}
}
type CommandHandler interface {
HandleCommand(update MessageUpdate) error
}
type commandHandler struct {
api BotAPI
}
func (h *commandHandler) HandleCommand(update MessageUpdate) error {
if update.Command == nil {
return nil
}
var msgText string
switch *update.Command {
case StartCommand:
msgText = "Жду твоих вопросов!"
case UnknownCommand:
msgText = "Неизвестная команда!"
}
return h.api.SendMessage(update.FromChatID, Message{Text: msgText})
}
+33
View File
@@ -14,6 +14,7 @@ const (
messageParseMode = tgbotapi.ModeMarkdown
startCommand = "start"
infoCommand = "info"
)
func NewBotAPI(bot *tgbotapi.BotAPI, ownerChatID int64) app.BotAPI {
@@ -58,6 +59,10 @@ func (api *botAPI) SendMessage(chatID int64, message app.Message) error {
return api.sendPhotoMessage(chatID, message)
}
if message.Video != nil {
return api.sendVideoMessage(chatID, message)
}
return api.sendTextMessage(chatID, message)
}
@@ -74,6 +79,7 @@ func (api *botAPI) hydrateMessage(msg *tgbotapi.Message) app.Message {
return app.Message{
Text: text,
Image: api.hydrateImage(msg.Photo),
Video: api.hydrateVideo(msg.Video),
}
}
@@ -96,6 +102,14 @@ func (api *botAPI) sendPhotoMessage(chatID int64, message app.Message) error {
return errors.WithStack(err)
}
func (api *botAPI) sendVideoMessage(chatID int64, message app.Message) error {
video := api.prepareVideo(message)
mediaMsg := tgbotapi.NewMediaGroup(chatID, video)
_, err := api.bot.Send(mediaMsg)
return errors.WithStack(err)
}
func (api *botAPI) hydrateCommand(msg *tgbotapi.Message) *app.Command {
if !msg.IsCommand() {
return nil
@@ -105,6 +119,8 @@ func (api *botAPI) hydrateCommand(msg *tgbotapi.Message) *app.Command {
switch msg.Command() {
case startCommand:
cmd = app.StartCommand
case infoCommand:
cmd = app.InfoCommand
default:
cmd = app.UnknownCommand
}
@@ -131,6 +147,16 @@ func (api *botAPI) hydrateImage(photos []tgbotapi.PhotoSize) *app.Image {
}
}
func (api *botAPI) hydrateVideo(video *tgbotapi.Video) *app.Video {
if video == nil {
return nil
}
return &app.Video{
FileID: video.FileID,
}
}
func (api *botAPI) preparePhotos(message app.Message) []interface{} {
photo := tgbotapi.NewInputMediaPhoto(tgbotapi.FileID(message.Image.FileID))
photo.Caption = message.Text
@@ -141,3 +167,10 @@ func (api *botAPI) preparePhotos(message app.Message) []interface{} {
return photos
}
func (api *botAPI) prepareVideo(message app.Message) []interface{} {
video := tgbotapi.NewInputMediaVideo(tgbotapi.FileID(message.Video.FileID))
video.Caption = message.Text
video.ParseMode = messageParseMode
return []interface{}{video}
}