-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtgbot.go
78 lines (62 loc) · 1.61 KB
/
tgbot.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"net/http"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/phuslu/log"
)
func newTgBot(cfg Config) (*tgbotapi.BotAPI, error) {
hc := &http.Client{
Timeout: time.Second * 3,
}
bot, err := tgbotapi.NewBotAPIWithClient(cfg.Token, tgbotapi.APIEndpoint, hc)
if err != nil {
return nil, fmt.Errorf("create tg bot failed: %w", err)
}
log.Info().Msgf("authorized on account %s", bot.Self.UserName)
if err := setupWebHook(bot, cfg); err != nil {
return nil, err
}
return bot, nil
}
func setupWebHook(bot *tgbotapi.BotAPI, cfg Config) error {
log.Info().Msgf("webhook url: %s", cfg.WebHookURL)
wh, _ := tgbotapi.NewWebhook(cfg.WebHookURL)
if _, err := bot.Request(wh); err != nil {
return fmt.Errorf("request with webhook config failed: %w", err)
}
return nil
}
func getUpdatesChan(cfg Config, bot *tgbotapi.BotAPI) (tgbotapi.UpdatesChannel, error) {
updates := bot.ListenForWebhook("/")
port := "8080"
if cfg.Port != "" {
port = cfg.Port
}
log.Info().Msgf("listen on port %s", port)
go func() {
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Error().Err(err).Msg("")
}
}()
return updates, nil
}
func shutdown(bot *tgbotapi.BotAPI) {
log.Info().Msg("shutdown")
if bot == nil {
return
}
bot.StopReceivingUpdates()
info, err := bot.GetWebhookInfo()
if err != nil {
log.Error().Err(err).Msg("get web hook failed")
}
if info.IsSet() {
resp, err := bot.MakeRequest("deleteWebhook", nil)
if err != nil {
log.Error().Err(err).Msgf("delete webhook failed: %+v", resp)
}
log.Info().Msg("webhook deleted")
}
}