-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg.go
52 lines (43 loc) · 1.01 KB
/
tg.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
40
41
42
43
44
45
46
47
48
49
50
51
52
package tgBotVkPostSendler
import (
"log"
"time"
tgbotapi "gopkg.in/telegram-bot-api.v4"
)
func (h *Handler) GetRecipients(input []string) {
h.recipients = make(map[string]int64, len(input))
for _, recipients := range input {
h.recipients[recipients] = 0
}
}
func (h *Handler) StartBot(in <-chan Message) {
if err := h.Telegram.createBot(); err != nil {
h.ErrChan <- err
return
}
bot := h.Telegram.bot
channelName := h.Telegram.ChannelName
updates := bot.ListenForWebhook("/")
for {
select {
case msg := <-in:
h.handlePosts(msg, channelName)
case <-time.After(h.TimeOut):
h.handleFailedPosts(channelName)
case update := <-updates:
h.handleMessages(update.Message, channelName)
}
}
}
func (tg *Telegram) createBot() error {
bot, err := tgbotapi.NewBotAPI(tg.BotToken)
if err != nil {
return err
}
if _, err := bot.SetWebhook(tgbotapi.NewWebhook(tg.WebHookURL)); err != nil {
return err
}
tg.bot = bot
log.Printf("Authorized on account %s\n", bot.Self.UserName)
return nil
}