-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_service.go
53 lines (40 loc) · 912 Bytes
/
telegram_service.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
53
package main
import (
"gopkg.in/telegram-bot-api.v4"
"log"
"strings"
)
type TelegramBot struct {
*tgbotapi.BotAPI
}
func (bot *TelegramBot) ListenUpdates() error {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
return err
}
for update := range updates {
go bot.handleUpdate(update)
}
return nil
}
func (bot *TelegramBot) handleUpdate(update tgbotapi.Update) {
if update.Message != nil {
log.Printf("%v++", update.Message)
fields := strings.Fields(update.Message.Text)
switch fields[0] {
case "/ping":
telegramHandlePingMessage(bot, update.Message)
}
}
}
func NewTelegramBot(token string) (*TelegramBot, error) {
botapi, err := tgbotapi.NewBotAPI(token)
if err != nil {
return nil, err
}
botapi.Debug = true
log.Printf("Authorized on account %s", botapi.Self.UserName)
return &TelegramBot{BotAPI: botapi}, nil
}