-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
59 lines (44 loc) · 1.2 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
package telegram
import (
"log"
"os"
"encoding/json"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/pandacrew-net/diosteama/commands"
)
// Start initialized the bot and runs main loop
func Start() {
token := os.Getenv("TELEGRAM_BOT_TOKEN")
bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
log.Panic(err)
}
bot.Debug = false
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Panic(err)
}
for update := range updates {
j, _ := json.Marshal(update)
log.Printf("%s", j)
if update.Message == nil { // ignore any non-Message Updates
continue
}
response(update, bot)
log.Printf("[%s] %s (%v)", update.Message.From.UserName, update.Message.Text, update.Message.IsCommand())
}
}
func response(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
// Check if the message is forwarded and should be enqueued
if commands.EvalMessageToQueue(update) {
return
}
if len(update.Message.Text) > 0 && (string(update.Message.Text[0]) == "!" || string(update.Message.Text[0]) == "/") {
commands.Command(update, bot)
return
}
commands.Triggers(update, bot)
}