Answer the question
In order to leave comments, you need to log in
How to make telegram bot catch media group?
I use the tgbotapi
library , I write the bot itself in golang. I want to make an echo bot with a simple function: sending exactly the same message as I sent to the bot. But if I send a message of two photos and one video (media group) or three photos at the same time (media group) or four videos at the same time (media group), then the bot throws me each element of the media group separately. How to solve this incident, the problem is in getting a media group, I can’t figure out how to catch and process it? Tell me please
package main
import (
"log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
func main() {
bot, err := tgbotapi.NewBotAPI("TOKEN")
if err != nil {
log.Panic(err)
}
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Fatal(err)
}
for update := range updates {
switch {
case update.Message != nil:
userName := update.Message.From.UserName
chatID := update.Message.Chat.ID
log.Printf("[%s] %d", userName, chatID)
sendMessage(bot, update.Message, chatID)
}
}
}
func sendMessage(bot *tgbotapi.BotAPI, message *tgbotapi.Message, chatID int64) {
var msg tgbotapi.Chattable
switch {
case message.Text != "":
msg = tgbotapi.NewMessage(chatID, message.Text)
case message.Photo != nil:
photoArray := *message.Photo
photoLastIndex := len(photoArray) - 1
photo := photoArray[photoLastIndex]
photoMsg := tgbotapi.NewPhotoShare(chatID, photo.FileID)
photoMsg.Caption = message.Caption
msg = photoMsg
case message.Voice != nil:
msg = tgbotapi.NewVoiceShare(chatID, message.Voice.FileID)
case message.Sticker != nil:
msg = tgbotapi.NewStickerShare(chatID, message.Sticker.FileID)
case message.Animation != nil:
msg = tgbotapi.NewAnimationShare(chatID, message.Animation.FileID)
case message.VideoNote != nil:
msg = tgbotapi.NewVideoNoteShare(chatID, message.VideoNote.Length, message.VideoNote.FileID)
case message.Video != nil:
videoMsg := tgbotapi.NewVideoShare(chatID, message.Video.FileID)
videoMsg.Caption = message.Caption
msg = videoMsg
case message.Audio != nil:
msg = tgbotapi.NewAudioShare(chatID, message.Audio.FileID)
case message.Document != nil:
msg = tgbotapi.NewDocumentShare(chatID, message.Document.FileID)
case message.Location != nil:
msg = tgbotapi.NewLocation(chatID, message.Location.Latitude, message.Location.Longitude)
case message.Contact != nil:
msg = tgbotapi.NewContact(chatID, message.Contact.PhoneNumber, message.Contact.FirstName)
default:
msg = tgbotapi.NewMessage(chatID, "Невозможно отправить")
}
_, err := bot.Send(msg)
if err != nil {
log.Println(err)
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question