/
survey.go
65 lines (55 loc) · 1.3 KB
/
survey.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
package main
import (
"fmt"
"strconv"
"time"
"burgerking/client"
"burgerking/utils"
tb "gopkg.in/tucnak/telebot.v2"
"strings"
)
func getValCodeOnline(id string) (message string) {
if !utils.ValidateNumber(id) {
return "Invalid code."
}
c := client.BurgerkingClient{}
err := c.StartSurvey(id)
if err != nil {
return "Online Mode:\nSurvey starts failed."
}
message, err = c.DoSurvey()
if err != nil {
return "Online Mode:\nSurvey failed."
}
return fmt.Sprintf("Online Mode:\nSurvey Code: %s\nValCode: %s", id, message)
}
func getValCodeOffline(id string) (message string) {
valCode, _ := utils.CalcValCode(id)
message = fmt.Sprintf("Offline Mode:\nSurvey Code: %s\nValCode: %s", id, valCode)
return
}
func getValCode(text string) (message string) {
if strings.HasPrefix(text, "online") || strings.HasPrefix(text, "Online") {
message = getValCodeOnline(text[6:])
} else {
message = getValCodeOffline(text)
}
return
}
func main() {
b, err := tb.NewBot(tb.Settings{
Token: "TOKEN HERE",
Poller: &tb.LongPoller{Timeout: 1 * time.Second},
})
if err != nil {
fmt.Println(err)
return
}
b.Handle("/id", func(m *tb.Message) {
b.Send(m.Sender, strconv.FormatInt(m.Chat.ID, 10))
})
b.Handle(tb.OnText, func(m *tb.Message) {
b.Send(m.Sender, getValCode(m.Text))
})
b.Start()
}