-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (91 loc) · 2.76 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/99heitor/gecko-butler-go/commands"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
const debugID = -180312761
func main() {
bot := setupBot()
updates := bot.ListenForWebhook("/" + bot.Token)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
go http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
for update := range updates {
if update.Message == nil && update.InlineQuery == nil && update.ChosenInlineResult == nil {
continue
}
if update.ChosenInlineResult != nil {
chosenInlineResult := update.ChosenInlineResult
log.Printf("Chose chapinha: %v", chosenInlineResult.ResultID)
continue
}
if update.InlineQuery != nil {
inlinequery := update.InlineQuery
log.Printf("Inline Query: %v", inlinequery.Query)
switch {
case strings.EqualFold(inlinequery.Query, "proximochapinha"):
commands.ProximoChapinha(bot, update)
}
continue
}
if update.Message.Chat.IsGroup() || update.Message.Chat.IsSuperGroup() {
log.Printf("Request from chat: %s %d", update.Message.Chat.Title, update.Message.Chat.ID)
} else {
log.Printf("Request from user: %s %d", update.Message.Chat.UserName, update.Message.Chat.ID)
}
if bot.Debug {
log.Printf("Update: %v", update.Message.Text)
}
command := update.Message.Command()
switch {
case strings.EqualFold(command, "version"):
commands.Version(bot, update)
case strings.EqualFold(command, "describe"):
commands.Describe(bot, update)
case strings.EqualFold(command, "tldr"):
commands.Summarize(bot, update)
case strings.EqualFold(command, "paywall"):
commands.Paywall(bot, update)
case strings.EqualFold(command, "scihub"):
commands.SciHub(bot, update)
case strings.EqualFold(command, "addchapinhasmood"):
commands.AddChapinhasMood(bot, update)
case strings.EqualFold(command, "debug") && update.Message.Chat.ID == debugID:
rsp := fmt.Sprintf("Switching debug mode to %t", !bot.Debug)
log.Printf(rsp)
bot.Debug = !bot.Debug
msg := tgbotapi.NewMessage(update.Message.Chat.ID, rsp)
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
}
}
}
func setupBot() *tgbotapi.BotAPI {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_TOKEN"))
if err != nil {
log.Fatal(err)
}
bot.Debug = false
log.Printf("Authorized on account %s", bot.Self.UserName)
_, err = bot.SetWebhook(tgbotapi.NewWebhook(os.Getenv("APP_URL") + "/" + bot.Token))
if err != nil {
log.Fatal(err)
}
info, err := bot.GetWebhookInfo()
if err != nil {
log.Fatal(err)
}
if info.LastErrorDate != 0 {
log.Printf("Telegram callback failed: %s", info.LastErrorMessage)
}
return bot
}