/
bot.go
39 lines (33 loc) · 949 Bytes
/
bot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package telegram
import (
"github.com/pkg/errors"
"gopkg.in/telegram-bot-api.v4"
)
// NewBot creates new telegram bot
func NewBot(c BotConfig) (*Bot, error) {
bot, err := tgbotapi.NewBotAPI(c.Token)
if err != nil {
return nil, errors.Wrap(err, "could not create telegram bot")
}
bot.Debug = c.Debug
return &Bot{
Bot: bot,
Config: c,
}, nil
}
// GetUpdatesChannel gets new updates updates
func (b *Bot) GetUpdatesChannel() (tgbotapi.UpdatesChannel, error) {
updatesConfig := tgbotapi.NewUpdate(0)
if b.Config.UpdatesTimeout > 0 {
updatesConfig.Timeout = b.Config.UpdatesTimeout
}
return b.Bot.GetUpdatesChan(updatesConfig)
}
// SendMessage sends a telegram message
func (b *Bot) SendMessage(m Message) error {
msg := tgbotapi.NewMessage(int64(m.ChatID), m.Text)
msg.ReplyToMessageID = m.MessageID
msg.DisableWebPagePreview = true
_, err := b.Bot.Send(msg)
return errors.Wrapf(err, "could not send message: %v", m)
}