1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package telegram
- import (
- "log"
- tg "github.com/go-telegram-bot-api/telegram-bot-api"
- "github.com/nchursin/formtgbot/config"
- )
- var key = config.Get().Token
- var chats = config.Get().AdminChats
- var botInstance Bot
- type Bot struct {
- isReady bool
- Bot *tg.BotAPI
- }
- func GetBot() *Bot {
- if !botInstance.isReady {
- tgBot, err := tg.NewBotAPI(key)
- if err != nil {
- log.Panic(err)
- }
- tgBot.Debug = true
- log.Printf("Authorized on account %s", tgBot.Self.UserName)
- botInstance.Bot = tgBot
- botInstance.isReady = true
- }
- return &botInstance
- }
- func (bot *Bot) SendMessage(data Messageable) {
- message, format := data.Message()
- keyboard := data.FullKeyboard()
- log.Printf("[%s] %s", "message", message)
- for _, chatId := range chats {
- msg := tg.NewMessage(chatId, message)
- msg.ParseMode = format
- msg.ReplyMarkup = keyboard
- bot.Bot.Send(msg)
- }
- }
- func (bot *Bot) SendTextMessage(message string) {
- log.Printf("[%s] %s", "message", message)
- for _, chatId := range chats {
- msg := tg.NewMessage(chatId, message)
- log.Printf("[telegram]: sending message: %+v", msg)
- bot.Bot.Send(msg)
- }
- }
- func (bot *Bot) PostToChannel(data Postable) {
- message, format := data.Message()
- keyboard := data.ChannelKeyboard()
- log.Printf("[%s] %s", "message", message)
- msg := tg.NewMessageToChannel(config.Get().Channel, message)
- msg.ParseMode = format
- msg.ReplyMarkup = keyboard
- log.Printf("[telegram]: posting: %+v", msg)
- bot.Bot.Send(msg)
- }
- func (bot *Bot) ListenForUpdates() tg.UpdatesChannel {
- u := tg.NewUpdate(0)
- u.Timeout = 60
- updates, err := bot.Bot.GetUpdatesChan(u)
- if nil != err {
- panic(err)
- }
- return updates
- }
|