-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomNumberGo_bot.go
More file actions
399 lines (381 loc) · 11.8 KB
/
RandomNumberGo_bot.go
File metadata and controls
399 lines (381 loc) · 11.8 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package main
import (
"encoding/json"
"encoding/xml"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"os"
"strconv"
"strings"
tgbotapi "github.com/Syfaro/telegram-bot-api"
//botan "github.com/botanio/sdk/go"
)
var (
templatesMap map[int]*Dice
help = "help message:\n" +
"structure of queries: /command [parametr] [parametr] ....\n" +
"example: /setmin 10 - this command sets minimum border to 10\n\n" +
"commands:\n" +
"help - shows this message\n" +
"settemplate - shows list of templates\n" +
"setmin [number]- sets minimum border\n" +
"setmax [number] - sets maximum border\n" +
"setquantity [number] - sets number of random numbers\n" +
"setminmaxqua [min] [max] [quantity] - sets minimum, maximum and quantity\n" +
"setwords [word1;word2;word3] - sets words for random choice\n" +
"status - shows your current tamplate of rundom\n" +
"random\n" +
"roll - genertes random nuber/numbers via current template"
templates Templates
)
type Config struct {
TelegramBotToken string
BotanApiToken string
Shutdown string
}
type Templates struct {
Template []Dice `xml:"Template"`
}
type Dice struct {
Name string `xml:"Name"`
Min int `xml:"Min"`
Max int `xml:"Max"`
Quantity int `xml:"Quantity"`
Values []string `xml:"Values"`
ImagePath []string `xml:"ImagePath"`
}
type Message struct {
Text string
ChatId int64
}
func main() {
templatesMap = make(map[int]*Dice)
templates, _ = parseTemplates()
//var sync chan (bool)
config, err := parceConfig()
if err != nil {
log.Panic(err)
}
//botAnalit := botan.New(config.BotanApiToken)
bot, err := tgbotapi.NewBotAPI(config.TelegramBotToken)
if err != nil {
log.Panic(err)
}
bot.Debug = true
defer sendMessage(bot, int64(308882495), "bot offline")
log.Printf("Authorized on account %s", bot.Self.UserName)
ucfg := tgbotapi.NewUpdate(0)
ucfg.Timeout = 60
updates, err := bot.GetUpdatesChan(ucfg)
if err != nil {
log.Panic(err)
}
for update := range updates {
if update.InlineQuery != nil {
query := update.InlineQuery.Query
commands := strings.Split(query, " ")
// botAnalit.trackAsync(1, Message{Text: query, ChatId: update.InlineQuery.ID}, "CallbackQuery", func(ans botan.Answer, err []error) {
// log.Println(strconv.Itoa(int(chatID)) + ":" + strconv.Itoa(update.InlineQuery.ID) + ":" + query)
// sync <- true
// })
var msg string
if len(commands) > 3 {
min, err := strconv.Atoi(commands[1])
max, err := strconv.Atoi(commands[2])
if err != nil {
msg = "wrong format"
}
msg = strconv.Itoa(rand.Intn(max+1-min) + min)
} else {
msg = "wrong format"
}
var articals []interface{}
articals = append(articals, msg)
inlineConfigAnswer := tgbotapi.InlineConfig{
InlineQueryID: update.InlineQuery.ID,
IsPersonal: false,
CacheTime: 0,
Results: articals,
}
_, err := bot.AnswerInlineQuery(inlineConfigAnswer)
if err != nil {
log.Println(err)
}
}
if update.CallbackQuery != nil {
userID := update.CallbackQuery.From.ID
chatID := update.CallbackQuery.Message.Chat.ID
query := update.CallbackQuery.Data
// botAnalit.TrackAsync(1, Message{Text: query, ChatId: chatID}, "CallbackQuery", func(ans botan.Answer, err []error) {
// log.Println(strconv.Itoa(int(chatID)) + ":" + strconv.Itoa(userID) + ":" + query)
// sync <- true
// })
switch query {
case "roll":
roll(userID, chatID, query, bot)
default:
for _, template := range templates.Template {
if query == template.Name {
tmp := template
templatesMap[userID] = &tmp
break
}
}
sendMessage(bot, chatID, "Ok, I remember")
status(userID, chatID, query, bot)
}
}
if update.Message != nil {
userID := update.Message.From.ID
chatID := update.Message.Chat.ID
query := update.Message.Text
// botAnalit.TrackAsync(1, Message{Text: query, ChatId: chatID}, "Message", func(ans botan.Answer, err []error) {
// log.Println(strconv.Itoa(int(chatID)) + ":" + strconv.Itoa(userID) + ":" + query)
// sync <- true
// })
var command = ""
command = update.Message.Command()
if command == "" {
//for text
sendMessage(bot, chatID, "/help shows help message")
} else {
//for commands
switch command {
case "start":
sendMessage(bot, chatID, "Welcome to RandomNumber_bot\n"+
"/help shows help message")
case "settemplate":
msg := tgbotapi.NewMessage(chatID, "Select template")
buttons := tgbotapi.InlineKeyboardMarkup{}
for _, template := range templates.Template {
var row []tgbotapi.InlineKeyboardButton
btn := tgbotapi.NewInlineKeyboardButtonData(template.Name, template.Name)
row = append(row, btn)
buttons.InlineKeyboard = append(buttons.InlineKeyboard, row)
}
msg.ReplyMarkup = buttons
bot.Send(msg)
case "setquantity":
if _, ok := templatesMap[userID]; ok {
commands := strings.Split(query, " ")
if len(commands) > 1 {
templatesMap[userID].Quantity, err = strconv.Atoi(commands[1])
if err != nil {
sendMessage(bot, chatID, "wrong format")
}
status(userID, chatID, query, bot)
} else {
sendMessage(bot, chatID, "wrong format")
}
} else {
sendMessage(bot, chatID, "please use /settemplate before using "+query)
}
case "setmin":
if _, ok := templatesMap[userID]; ok {
commands := strings.Split(query, " ")
if len(commands) > 1 {
templatesMap[userID].Min, err = strconv.Atoi(commands[1])
if err != nil {
sendMessage(bot, chatID, "wrong format")
}
status(userID, chatID, query, bot)
} else {
sendMessage(bot, chatID, "wrong format")
}
} else {
sendMessage(bot, chatID, "please use /settemplate before using "+query)
}
case "setmax":
if _, ok := templatesMap[userID]; ok {
commands := strings.Split(query, " ")
if len(commands) > 1 {
templatesMap[userID].Max, err = strconv.Atoi(commands[1])
if err != nil {
sendMessage(bot, chatID, "wrong format")
}
status(userID, chatID, query, bot)
} else {
sendMessage(bot, chatID, "wrong format")
}
} else {
sendMessage(bot, chatID, "please use /settemplate before using "+query)
}
case "setwords":
if _, ok := templatesMap[userID]; ok {
commands := strings.Split(query, "/setwords ")
words := strings.Split(commands[1], ";")
for _, word := range words {
templatesMap[userID].Values = append(templatesMap[userID].Values, word)
}
status(userID, chatID, query, bot)
} else {
sendMessage(bot, chatID, "please use /settemplate before using "+query)
}
case "setminmaxqua":
if _, ok := templatesMap[userID]; ok {
commands := strings.Split(query, " ")
if len(commands) > 3 {
templatesMap[userID].Min, err = strconv.Atoi(commands[1])
templatesMap[userID].Max, err = strconv.Atoi(commands[2])
templatesMap[userID].Quantity, err = strconv.Atoi(commands[3])
if err != nil {
sendMessage(bot, chatID, "wrong format")
}
status(userID, chatID, query, bot)
} else {
sendMessage(bot, chatID, "wrong format")
}
} else {
sendMessage(bot, chatID, "please use /settemplate before using "+query)
}
case config.Shutdown:
return
case "random":
commands := strings.Split(query, " ")
if len(commands) > 3 {
min, err := strconv.Atoi(commands[1])
max, err := strconv.Atoi(commands[2])
if err != nil {
sendMessage(bot, chatID, "wrong format")
}
sendMessage(bot, chatID, strconv.Itoa(rand.Intn(max+1-min)+min))
} else {
sendMessage(bot, chatID, "wrong format")
}
case "status":
status(userID, chatID, query, bot)
case "roll":
roll(userID, chatID, query, bot)
case "serverip", "nextcloud":
var responseString string
for {
resp, err := http.Get("http://ifconfig.co")
if err != nil {
log.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
respData, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
os.Exit(1)
}
responseString = string(respData[:len(respData)-1])
if net.ParseIP(responseString) != nil {
break
}
}
if command == "nextcloud" {
responseString = "https://" + responseString + "/nextcloud"
}
sendMessage(bot, chatID, responseString)
default:
sendMessage(bot, chatID, help)
}
}
}
}
}
func status(userID int, chatID int64, query string, bot *tgbotapi.BotAPI) {
if val, ok := templatesMap[userID]; ok {
sendMessage(bot, chatID, "Your \"random\":\nTemplate: "+val.Name+"\nQuantity: "+strconv.Itoa(int(val.Quantity))+"\nMin: "+
strconv.Itoa(val.Min)+"\nMax: "+strconv.Itoa(val.Max)+"\nWords: "+strings.Join(val.Values, ";")+
"\n\n/help for help\n/roll for roll")
} else {
sendMessage(bot, chatID, "Template not found. Please use /settemplate before using "+query)
}
}
func roll(userID int, chatID int64, query string, bot *tgbotapi.BotAPI) {
if val, ok := templatesMap[userID]; ok {
if val.Max > val.Min || len(val.Values) > 0 {
sum := 0
msgText := "\n"
for i := 0; i < val.Quantity; i++ {
if len(val.Values) == 0 {
tmp := rand.Intn(val.Max+1-val.Min) + val.Min
sum += tmp
if val.Min == 1 && val.Quantity < 3 {
switch val.Max {
case 4:
if len(val.ImagePath) > tmp && val.ImagePath[tmp] != "" {
sendImage(bot, chatID, val.ImagePath[tmp])
}
case 6:
if len(val.ImagePath) > tmp && val.ImagePath[tmp] != "" {
sendImage(bot, chatID, val.ImagePath[tmp])
}
case 8:
if len(val.ImagePath) > tmp && val.ImagePath[tmp] != "" {
sendImage(bot, chatID, val.ImagePath[tmp])
}
case 10:
if len(val.ImagePath) > tmp && val.ImagePath[tmp] != "" {
sendImage(bot, chatID, val.ImagePath[tmp])
}
case 12:
if len(val.ImagePath) > tmp && val.ImagePath[tmp] != "" {
sendImage(bot, chatID, val.ImagePath[tmp])
}
case 20:
if len(val.ImagePath) > tmp && val.ImagePath[tmp] != "" {
sendImage(bot, chatID, val.ImagePath[tmp])
}
case 100:
}
} else {
msgText += strconv.Itoa(tmp) + "\n"
}
} else {
tmp := rand.Intn(len(val.Values))
msgText += val.Values[tmp] + "\n"
}
}
msgText += "sum= " + strconv.Itoa(sum) + "\navg= " + strconv.FormatFloat(float64(sum)/float64(val.Quantity), 'f', -4, 32) + "\n/roll again"
sendMessage(bot, chatID, msgText)
} else {
sendMessage(bot, chatID, "please use /setmin or /setmax to change numbers, because your max number less than min, before using "+query)
}
} else {
sendMessage(bot, chatID, "please use /settemplate before using "+query)
}
}
func parseTemplates() (Templates, error) {
file, err := os.Open("resources.xml")
if err != nil {
log.Panic(err)
}
defer file.Close()
byteValue, err := ioutil.ReadAll(file)
var tmpTemplates Templates
err = xml.Unmarshal(byteValue, &tmpTemplates)
if err != nil {
log.Println(err)
}
log.Println("length of templates slice=" + strconv.Itoa(len(tmpTemplates.Template)))
return tmpTemplates, err
}
func parceConfig() (Config, error) {
file, err := os.Open("config.json")
if err != nil {
log.Panic(err)
}
defer file.Close()
decoder := json.NewDecoder(file)
config := Config{}
err = decoder.Decode(&config)
if err != nil {
log.Panic(err)
}
return config, err
}
func sendMessage(bot *tgbotapi.BotAPI, chatID int64, text string) {
msg := tgbotapi.NewMessage(chatID, text)
bot.Send(msg)
}
func sendImage(bot *tgbotapi.BotAPI, chatID int64, imgPath string) {
msg := tgbotapi.NewPhotoUpload(chatID, imgPath) //NewInputMediaPhoto(media string)
bot.Send(msg)
}