-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
182 lines (172 loc) · 5.57 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"context"
"fmt"
"github.com/go-telegram-bot-api/telegram-bot-api"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
const (
MaxQueryLen = 150
LimiterDefaultTimeoutMillis = 10000
LimiterOpName = "request"
UnknownError = "Something went wrong."
InputTooLong = "Your request is too long."
NoInputProvided = "Please provide your query in plaintext."
TelegramKeyEnv = "TELEGRAM_KEY_CODING_GURU_BOT"
DevModeEnv = "DEV_MODE_CODING_GURU_BOT"
WebHookHostEnv = "WEB_HOOK_HOST_CODING_GURU_BOT"
WebHookListenPortEnv = "WEB_HOOK_LISTEN_PORT_CODING_GURU_BOT"
UseTLSEnv = "USE_TLS_ENCRYPTION_CODING_GURU_BOT"
TLSCertFileEnv = "TLS_CERT_FILE_CODING_GURU_BOT"
TLSKeyFileEnv = "TLS_KEY_FILE_CODING_GURU_BOT"
HelpString = `Just type your query and send!
Examples:
- c++ check if element in list
- schedule task java
- binary search go
- docker set env
`
)
func main() {
telegramKey := os.Getenv(TelegramKeyEnv)
if len(telegramKey) == 0 {
log.Fatalf("[FATAL] No Telegram Key provided. Please set %s environment variable.", TelegramKeyEnv)
}
bot, err := tgbotapi.NewBotAPI(telegramKey)
if err != nil {
log.Fatalf("[FATAL] Failed to register: %v", err)
}
log.Printf("[INFO] Bot authorized - %s", bot.Self.UserName)
var updatesChan tgbotapi.UpdatesChannel
if len(os.Getenv(DevModeEnv)) != 0 {
updatesChan = pollingChannel(bot)
} else {
updatesChan = webHookChannel(bot)
}
log.Printf("[INFO] Updates channel obtained")
processUpdates(bot, updatesChan)
}
func pollingChannel(bot *tgbotapi.BotAPI) tgbotapi.UpdatesChannel {
log.Printf("[INFO] Registering a polling-based updates channel...")
updateConfig := tgbotapi.NewUpdate(0)
channel, err := bot.GetUpdatesChan(updateConfig)
if err != nil {
log.Fatalf("[FATAL] Failed to get polling updates channel: %v", err)
}
return channel
}
func webHookChannel(bot *tgbotapi.BotAPI) tgbotapi.UpdatesChannel {
log.Printf("[INFO] Registering a webhook-based updates channel...")
link := os.Getenv(WebHookHostEnv)
if len(link) == 0 {
log.Fatalf("[FATAL] No host for webhook provided. Please set %s environment variable.", WebHookHostEnv)
}
port := os.Getenv(WebHookListenPortEnv)
if len(link) == 0 {
log.Fatalf("[FATAL] No port for webhook provided. Please set %s environment variable.", WebHookListenPortEnv)
}
address := fmt.Sprintf("0.0.0.0:%s", port)
var webHookConfig tgbotapi.WebhookConfig
var httpServeFunc func()
useTls := len(os.Getenv(UseTLSEnv)) > 0
if useTls {
certFile := os.Getenv(TLSCertFileEnv)
if len(certFile) == 0 {
log.Fatalf("[FATAL] No cert file provided. Please set %s environment variable.", TLSCertFileEnv)
}
keyFile := os.Getenv(TLSKeyFileEnv)
if len(keyFile) == 0 {
log.Fatalf("[FATAL] No cert key file provided. Please set %s environment variable.", TLSKeyFileEnv)
}
webHookConfig = tgbotapi.NewWebhookWithCert(link+bot.Token, certFile)
httpServeFunc = func() {
go http.ListenAndServeTLS(address, certFile, keyFile, nil)
}
} else {
webHookConfig = tgbotapi.NewWebhook(link + bot.Token)
httpServeFunc = func() {
go http.ListenAndServe(address, nil)
}
}
_, err := bot.SetWebhook(webHookConfig)
if err != nil {
log.Fatalf("[FATAL] Could not register web hook - %v", err)
}
info, err := bot.GetWebhookInfo()
if err != nil {
log.Fatalf("[FATAL] Could not get web hook info - %v", err)
}
if info.LastErrorDate != 0 {
log.Fatalf("[FATAL] Web hook callback failed - %v", info.LastErrorMessage)
}
channel := bot.ListenForWebhook("/" + bot.Token)
httpServeFunc()
return channel
}
func processUpdates(bot *tgbotapi.BotAPI, updates tgbotapi.UpdatesChannel) {
guru := NewGuru()
limiter := NewLimiter()
for update := range updates {
go func() {
if update.Message == nil {
return
}
username := update.Message.From.UserName
userId := strconv.Itoa(update.Message.From.ID)
chatId := update.Message.Chat.ID
allowed, dropLimit := limiter.Allow(LimiterOpName, userId, LimiterDefaultTimeoutMillis)
if !allowed {
log.Printf("[WARN] Request from user '%s'(id=%s) already in progress.", username, userId)
return
}
defer dropLimit()
if update.Message.IsCommand() {
command := update.Message.Command()
log.Printf("[INFO] Received command (from = %s, command = %s)", username, command)
switch command {
case "help":
respond(bot, chatId, HelpString)
default:
log.Printf("[WARN] Unknown command - %s", command)
}
return
}
query := strings.TrimSpace(strings.ToLower(update.Message.Text))
log.Printf("[INFO] Received query (from = %s, query = %s)", username, query)
inputLen := len(query)
if inputLen == 0 {
log.Printf("[ERROR] Empty query (username = %v)", username)
respond(bot, chatId, NoInputProvided)
return
}
if inputLen > MaxQueryLen {
log.Printf("[ERROR] Query is too long (username = %v, max = %v, actual = %v)",
username, MaxQueryLen, inputLen)
respond(bot, chatId, InputTooLong)
return
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(3*time.Second))
defer cancel()
result, err := guru.FindAnswer(ctx, query)
if err != nil {
log.Printf("[ERROR] Failed to run query: %v", err)
respond(bot, chatId, UnknownError)
return
}
respond(bot, chatId, result)
}()
}
}
func respond(bot *tgbotapi.BotAPI, chat int64, message string) error {
response := tgbotapi.NewMessage(chat, message)
_, err := bot.Send(response)
if err != nil {
return err
}
return nil
}