-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (74 loc) · 2.21 KB
/
main.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
79
80
81
82
83
84
85
package main
import (
"log"
"net/http"
"os"
"github.com/alphatroya/redmine-helper-bot/storage"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
const (
storageURLEnvKey = "REDIS_URL"
storagePassphaseKey = "STORAGE_PASSPHRASE"
telegramBotKeyEnvKey = "TELEGRAM_BOT_KEY"
)
func main() {
redisClient, err := storage.NewStorageInstance(storageURLEnvKey, storagePassphaseKey)
if err != nil {
log.Panicf("Storage configuration failed with error: %s", err)
}
bot, err := tgbotapi.NewBotAPI(os.Getenv(telegramBotKeyEnvKey))
if err != nil {
log.Panicf("Connection to telegram bot is broken, error: %s", err)
}
log.Printf("Authorized on account %s", bot.Self.UserName)
handler := UpdateHandler{bot, redisClient}
if os.Getenv("DEBUG") == "true" {
bot.Debug = true
configureLongPolling(handler, bot)
} else {
configureWebHookObserving(handler, bot)
}
}
func configureLongPolling(handler UpdateHandler, bot *tgbotapi.BotAPI) {
updateConfig := tgbotapi.NewUpdate(0)
updateConfig.Timeout = 60
updates, err := bot.GetUpdatesChan(updateConfig)
if err != nil {
log.Panicf("Failed to obtain updates channel, error: %s", err)
}
handleUpdates(updates, handler)
}
func configureWebHookObserving(updateHandler UpdateHandler, bot *tgbotapi.BotAPI) {
port := os.Getenv("PORT")
log.Printf("Port value %s", port)
if _, err := bot.SetWebhook(tgbotapi.NewWebhook(os.Getenv("SERVER_URL") + ":443/" + bot.Token)); err != nil {
log.Panicf("Webhook setup failed with error; %s", err)
}
info, err := bot.GetWebhookInfo()
if err != nil {
log.Fatal(err)
}
if info.LastErrorDate != 0 {
log.Printf("Telegram callback failed: %v", info)
}
updates := bot.ListenForWebhook("/" + bot.Token)
go func() {
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal(err)
}
}()
handleUpdates(updates, updateHandler)
}
func handleUpdates(updates tgbotapi.UpdatesChannel, handler UpdateHandler) {
for update := range updates {
if update.Message == nil {
continue
}
if update.Message.IsCommand() {
go handler.Handle(update.Message.Command(), update.Message.CommandArguments(), update.Message.Chat.ID)
} else {
go handler.HandleMessage(update.Message.Text, update.Message.Chat.ID)
}
}
}