-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
190 lines (143 loc) · 3.8 KB
/
bot.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
183
184
185
186
187
188
189
190
package main
import (
"bytes"
"fmt"
"log"
"strconv"
telegramBotApi "gopkg.in/telegram-bot-api.v4"
"os"
"bufio"
)
type BotCommunicationInterface interface {
IsAdmin(username string) bool
AddRequest(requestString string) (string, *Request)
GetRequestsText() string
CloseRequest(rawRequestNum string) (string, *Request)
Shutdown()
SendReply(update telegramBotApi.Update, text string)
}
//Bot represents entity, associated with telegramBotApi.
//After creating an exemplar it's highly recommended to call #Init method.
//At the end of work it's highly recommended to call #FinishWork method.
//
//see also #GetTelegramBotApi
type Bot struct {
Requests []*Request
botAPI *telegramBotApi.BotAPI
admins []string
workFinished bool
}
func (bot *Bot) getUpdatesChan() <-chan telegramBotApi.Update {
u := telegramBotApi.NewUpdate(0)
u.Timeout = UpdatesTimeout
updates, err := bot.botAPI.GetUpdatesChan(u)
if err != nil {
log.Panic(err)
}
return updates
}
func (bot *Bot) SendReply(update telegramBotApi.Update, text string) {
replyMessage := telegramBotApi.NewMessage(update.Message.Chat.ID, text)
replyMessage.ReplyToMessageID = update.Message.MessageID
bot.botAPI.Send(replyMessage)
}
func (bot *Bot) AddRequest(requestString string) (string, *Request) {
if len(requestString) == 0 {
return "Empty request won't be added", nil
}
request := &Request{Name: requestString}
bot.Requests = append(bot.Requests, request)
return fmt.Sprintf("Request '%s' added", request), request
}
func (bot *Bot) GetRequestsText() string {
if !hasOpenRequests(bot) {
return "No active requests at the moment"
}
var buffer bytes.Buffer
for number, request := range bot.Requests {
if !request.Closed {
buffer.WriteString(fmt.Sprintf("%d: %s\n", number, request))
}
}
return buffer.String()
}
func hasOpenRequests(bot *Bot) bool {
for _, request := range bot.Requests {
if !request.Closed {
return true
}
}
return false
}
func (bot *Bot) CloseRequest(rawRequestNum string) (string, *Request) {
if len(rawRequestNum) == 0 {
return "Request number to close required", nil
}
requestNum, err := strconv.Atoi(rawRequestNum)
if err != nil {
return fmt.Sprintf("Request number to close required, but got error: %s", err.Error()), nil
}
if requestNum < 0 || requestNum > len(bot.Requests) {
return "Incorrect request number", nil
}
request := bot.Requests[requestNum]
if request.Closed {
return fmt.Sprintf("Request '%s' is already closed", request), nil
}
request.Closed = true
return fmt.Sprintf("Request '%s' closed", request), request
}
func (bot *Bot) FinishWork() {
if !bot.workFinished {
log.Println("Bot finishes it's work")
bot.workFinished = true
}
}
func (bot *Bot) Init() {
log.Println("Bot initialization")
bot.admins = bot.initAdminsInfo()
log.Println("admins list:", bot.admins)
}
func GetTelegramBotApi(token string) *telegramBotApi.BotAPI {
botAPI, err := telegramBotApi.NewBotAPI(token)
if err != nil {
log.Panic(err)
}
//botAPI.Debug = true
return botAPI
}
func getBot() *Bot {
log.Println("Trying to read 'token' file")
token := readTokenFile()
log.Println("Token acquired")
botApi := GetTelegramBotApi(token)
log.Printf("Authorized on account %s", botApi.Self.UserName)
bot := &Bot{botAPI: botApi}
return bot
}
func (bot *Bot) initAdminsInfo() []string {
file, err := os.Open("admins")
if err != nil {
log.Println("admins file reading error:", err)
return []string{}
}
defer file.Close()
admins := []string{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
admins = append(admins, scanner.Text())
}
return admins
}
func (bot *Bot) IsAdmin(username string) bool {
for _, adminUsername := range bot.admins {
if username == adminUsername {
return true
}
}
return false
}
func (bot *Bot) Shutdown() {
bot.FinishWork()
os.Exit(0)
}