Add video support, extract command handler
This commit is contained in:
@@ -20,9 +20,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
log.Printf("Authorized on account %s", bot.Self.UserName)
|
log.Printf("Authorized on account %s", bot.Self.UserName)
|
||||||
|
|
||||||
botAPI := infrastructure.NewBotAPI(bot, conf.OwnerChatID)
|
|
||||||
errorsChan := make(chan error)
|
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() {
|
go func() {
|
||||||
for err := range errorsChan {
|
for err := range errorsChan {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
func NewAnonymousQuestionsService(api BotAPI, errorsChan chan error) AnonymousMessagesService {
|
func NewAnonymousQuestionsService(
|
||||||
|
errorsChan chan error,
|
||||||
|
commandHandler CommandHandler,
|
||||||
|
api BotAPI,
|
||||||
|
) AnonymousMessagesService {
|
||||||
return &anonymousMessagesService{
|
return &anonymousMessagesService{
|
||||||
api: api,
|
errorsChan: errorsChan,
|
||||||
errorsChan: errorsChan,
|
commandHandler: commandHandler,
|
||||||
|
api: api,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,14 +22,15 @@ type AnonymousMessagesService interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type anonymousMessagesService struct {
|
type anonymousMessagesService struct {
|
||||||
api BotAPI
|
errorsChan chan error
|
||||||
errorsChan chan error
|
commandHandler CommandHandler
|
||||||
|
api BotAPI
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *anonymousMessagesService) ServeMessages() error {
|
func (s *anonymousMessagesService) ServeMessages() error {
|
||||||
return s.api.HandleUpdates(func(update MessageUpdate) {
|
return s.api.HandleUpdates(func(update MessageUpdate) {
|
||||||
if update.Command != nil {
|
if update.Command != nil {
|
||||||
err := s.handleCommand(update)
|
err := s.commandHandler.HandleCommand(update)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.errorsChan <- err
|
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 {
|
func (s *anonymousMessagesService) pingClient(chatID int64) error {
|
||||||
return s.api.SendMessage(chatID, Message{Text: messageSentReply})
|
return s.api.SendMessage(chatID, Message{Text: messageSentReply})
|
||||||
}
|
}
|
||||||
@@ -68,5 +58,6 @@ func (s *anonymousMessagesService) handleMessage(message Message) error {
|
|||||||
return s.api.SendMessageToOwner(Message{
|
return s.api.SendMessageToOwner(Message{
|
||||||
Text: msgText,
|
Text: msgText,
|
||||||
Image: message.Image,
|
Image: message.Image,
|
||||||
|
Video: message.Video,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-4
@@ -18,6 +18,15 @@ type MessageUpdate struct {
|
|||||||
type Message struct {
|
type Message struct {
|
||||||
Text string
|
Text string
|
||||||
Image *Image
|
Image *Image
|
||||||
|
Video *Video
|
||||||
|
}
|
||||||
|
|
||||||
|
type Image struct {
|
||||||
|
FileID string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Video struct {
|
||||||
|
FileID string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Command int
|
type Command int
|
||||||
@@ -25,8 +34,5 @@ type Command int
|
|||||||
const (
|
const (
|
||||||
UnknownCommand Command = iota
|
UnknownCommand Command = iota
|
||||||
StartCommand
|
StartCommand
|
||||||
|
InfoCommand
|
||||||
)
|
)
|
||||||
|
|
||||||
type Image struct {
|
|
||||||
FileID string
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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})
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@ const (
|
|||||||
messageParseMode = tgbotapi.ModeMarkdown
|
messageParseMode = tgbotapi.ModeMarkdown
|
||||||
|
|
||||||
startCommand = "start"
|
startCommand = "start"
|
||||||
|
infoCommand = "info"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewBotAPI(bot *tgbotapi.BotAPI, ownerChatID int64) app.BotAPI {
|
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)
|
return api.sendPhotoMessage(chatID, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if message.Video != nil {
|
||||||
|
return api.sendVideoMessage(chatID, message)
|
||||||
|
}
|
||||||
|
|
||||||
return api.sendTextMessage(chatID, message)
|
return api.sendTextMessage(chatID, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,6 +79,7 @@ func (api *botAPI) hydrateMessage(msg *tgbotapi.Message) app.Message {
|
|||||||
return app.Message{
|
return app.Message{
|
||||||
Text: text,
|
Text: text,
|
||||||
Image: api.hydrateImage(msg.Photo),
|
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)
|
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 {
|
func (api *botAPI) hydrateCommand(msg *tgbotapi.Message) *app.Command {
|
||||||
if !msg.IsCommand() {
|
if !msg.IsCommand() {
|
||||||
return nil
|
return nil
|
||||||
@@ -105,6 +119,8 @@ func (api *botAPI) hydrateCommand(msg *tgbotapi.Message) *app.Command {
|
|||||||
switch msg.Command() {
|
switch msg.Command() {
|
||||||
case startCommand:
|
case startCommand:
|
||||||
cmd = app.StartCommand
|
cmd = app.StartCommand
|
||||||
|
case infoCommand:
|
||||||
|
cmd = app.InfoCommand
|
||||||
default:
|
default:
|
||||||
cmd = app.UnknownCommand
|
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{} {
|
func (api *botAPI) preparePhotos(message app.Message) []interface{} {
|
||||||
photo := tgbotapi.NewInputMediaPhoto(tgbotapi.FileID(message.Image.FileID))
|
photo := tgbotapi.NewInputMediaPhoto(tgbotapi.FileID(message.Image.FileID))
|
||||||
photo.Caption = message.Text
|
photo.Caption = message.Text
|
||||||
@@ -141,3 +167,10 @@ func (api *botAPI) preparePhotos(message app.Message) []interface{} {
|
|||||||
|
|
||||||
return photos
|
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}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user