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)
}
}