-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtexter.go
302 lines (257 loc) · 9.53 KB
/
texter.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
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
package main
import (
"fmt"
"github.com/DarkFighterLuke/covidgraphs"
"github.com/NicoNex/echotron"
"log"
"os"
"sort"
"strings"
"time"
)
// TODO: Use inline keyboards instead of handwritten command
// Handles "report" textual command
func (b *bot) textReport(update *echotron.Update) {
usageMessage := "<b>Uso Corretto del Comando:</b>\n/reports <code>[file] nome_report</code>\nReport disponibili:{<code>" +
strings.Join(reports, ", ") + "</code>}\nDigita /help per visualizzare il manuale."
tokens := strings.Fields(update.Message.Text)
tokens = tokens[1:]
var fieldNames []string
var flagFile bool = false
if len(tokens) < 1 {
b.SendMessage(usageMessage, update.Message.Chat.ID, echotron.PARSE_HTML)
return
}
for i, _ := range tokens {
tokens[i] = strings.ToLower(tokens[i])
}
if tokens[0] == "file" {
flagFile = true
}
i := 0
if flagFile {
i = 1
}
for ; i < len(tokens); i++ {
if res := sort.SearchStrings(reports, tokens[i]); res < len(reports) {
fieldNames = append(fieldNames, tokens[i])
} else {
b.SendMessage(usageMessage, update.Message.Chat.ID, echotron.PARSE_HTML)
return
}
}
for _, v := range fieldNames {
if v == "generale" {
msg := setCaptionAndamentoNazionale() + "\n\n\n" + setCaptionTopRegions() + "\n" + setCaptionTopProvinces()
b.SendMessage(msg, update.Message.Chat.ID, echotron.PARSE_HTML)
if flagFile {
filename := "report generale-" + time.Now().Format("20060102T150405") + ".txt"
f, err := os.Create(filename)
if err != nil {
log.Println(err)
}
_, err = f.WriteString(msg)
if err != nil {
log.Println(err)
f.Close()
}
err = f.Close()
if err != nil {
log.Println(err)
}
b.DeleteMessage(update.Message.Chat.ID, update.Message.ID)
b.SendDocument(filename, "", update.Message.Chat.ID)
err = os.Remove(filename)
if err != nil {
log.Println("can't delete file " + filename)
}
}
}
}
}
// TODO: Use inline keyboards instead of handwritten command
// Handles "nazione" textual command
func (b *bot) textNation(update *echotron.Update) {
usageMessage := "<b>Uso Corretto del Comando:\n</b>/nazione <code>andamento</code>\nper ottenere l'andamento della nazione\n" +
"/nazione <code>nome_dei_campi</code>\nper ottenere un confronto tra campi a tua scelta\n" +
"Dati nazione disponibili:\n{<code>" + strings.Join(natregAttributes, ", ") + "</code>}\nDigita /help per visualizzare il manuale."
tokens := strings.Fields(update.Message.Text)
tokens = tokens[1:]
var fieldNames []string
if len(tokens) < 1 {
b.SendMessage(usageMessage, update.Message.Chat.ID, echotron.PARSE_HTML)
return
}
sort.Strings(natregAttributes)
if tokens[0] == "andamento" {
b.sendAndamentoNazionale(update.Message)
} else {
for i := 0; i < len(tokens); i++ {
if res := sort.SearchStrings(natregAttributes, tokens[i]); res < len(natregAttributes) {
fieldNames = append(fieldNames, tokens[i])
} else {
b.SendMessage(usageMessage, update.Message.Chat.ID, echotron.PARSE_HTML)
return
}
}
titleAttributes := make([]string, 0)
for i := 0; i < 3; i++ {
if i < len(fieldNames) {
titleAttributes = append(titleAttributes, fieldNames[i])
} else {
titleAttributes = append(titleAttributes, "")
}
}
dirPath := workingDirectory + imageFolder
titleForFilename := "Nazione" + fmt.Sprintf("%s_%s_%s", titleAttributes[0], titleAttributes[1], titleAttributes[2])
title := "Confronto dati nazione"
var filename string
var err error
filename = dirPath + covidgraphs.FilenameCreator(titleForFilename)
if !covidgraphs.IsGraphExisting(filename) {
err, filename = covidgraphs.VociNazione(&nationData, fieldNames, 0, title, filename)
if err != nil {
log.Println(err)
}
}
if err != nil {
log.Println(err)
b.SendMessage(usageMessage, update.Message.Chat.ID, echotron.PARSE_HTML)
return
}
b.SendPhoto(filename, setCaptionConfrontoNazione(len(nationData)-1, fieldNames), update.Message.Chat.ID, echotron.PARSE_HTML)
}
b.DeleteMessage(update.Message.Chat.ID, update.Message.ID)
}
// TODO: Use inline keyboards instead of handwritten command
// Handles "regione" textual command
func (b *bot) textRegion(update *echotron.Update) {
dirPath := workingDirectory + imageFolder
usageMessage := "<b>Uso Corretto del Comando:\n</b>/regione <code>nome_regione andamento</code>\nper ottenere l'andamento della regione scelta\n" +
"/regione <code>nome_regione nome_dei_campi</code>\nper ottenere un confronto tra campi a tua scelta sulla desiderata\n" +
"Dati regione disponibili:\n{<code>" + strings.Join(natregAttributes, ", ") + "</code>}\nDigita /help per visualizzare il manuale."
tokens := strings.Fields(update.Message.Text)
tokens = tokens[1:]
var fieldNames []string
if len(tokens) < 2 {
b.SendMessage(usageMessage, update.Message.Chat.ID, echotron.PARSE_HTML)
return
}
sort.Strings(natregAttributes)
tokens[0] = strings.Replace(tokens[0], "_", " ", -1)
regionId, err := covidgraphs.FindLastOccurrenceRegion(®ionsData, "denominazione_regione", tokens[0])
if err != nil {
log.Println(err)
return
}
if tokens[1] == "andamento" {
b.sendAndamentoRegionale(update.Message, regionId)
} else {
for i := 1; i < len(tokens); i++ {
if res := sort.SearchStrings(natregAttributes, tokens[i]); res < len(natregAttributes) {
fieldNames = append(fieldNames, tokens[i])
} else {
return
}
}
regionCode, err := covidgraphs.FindFirstOccurrenceRegion(®ionsData, "denominazione_regione", tokens[0])
if err != nil {
log.Println(err)
b.SendMessage(usageMessage, update.Message.Chat.ID, echotron.PARSE_HTML)
return
}
titleAttributes := make([]string, 0)
for i := 0; i < 3; i++ {
if i < len(fieldNames) {
titleAttributes = append(titleAttributes, fieldNames[i])
} else {
titleAttributes = append(titleAttributes, "")
}
}
titleForFilename := "Regione" + regionsData[regionCode].Denominazione_regione + fmt.Sprintf("%s_%s_%s", titleAttributes[0], titleAttributes[1], titleAttributes[2])
title := "Confronto dati regione"
var filename string
filename = dirPath + covidgraphs.FilenameCreator(titleForFilename)
if !covidgraphs.IsGraphExisting(filename) {
err, filename = covidgraphs.VociRegione(®ionsData, fieldNames, 0, regionCode, title, filename)
if err != nil {
log.Println(err)
}
}
if err != nil {
log.Println(err)
b.SendMessage(usageMessage, update.Message.Chat.ID, echotron.PARSE_HTML)
return
}
b.SendPhoto(filename, setCaptionConfrontoRegione(regionId, fieldNames), update.Message.Chat.ID, echotron.PARSE_HTML)
}
b.DeleteMessage(update.Message.Chat.ID, update.Message.ID)
}
// TODO: Use inline keyboards instead of handwritten command
// Handles "provincia" textual command
func (b *bot) textProvince(update *echotron.Update) {
dirPath := workingDirectory + imageFolder
usageMessage := "<b>Uso Corretto del Comando:\n</b>/provincia <code>nome_provincia totale_casi</code>" +
"\nper ottenere informazioni sul totale dei casi della provincia scelta\n" +
"/provincia <code>nome_provincia nuovi_positivi</code>\nper ottenere informazioni sui nuovi positivi della provincia scelta\nDigita /help per visualizzare il manuale."
tokens := strings.Fields(update.Message.Text)
tokens = tokens[1:]
if len(tokens) != 2 {
b.SendMessage(usageMessage, update.Message.Chat.ID, echotron.PARSE_HTML)
return
}
tokens[0] = strings.Replace(tokens[0], "_", " ", -1)
provinceId, err := covidgraphs.FindFirstOccurrenceProvince(&provincesData, "denominazione_provincia", tokens[0])
if err != nil {
log.Println(err)
return
}
if tokens[1] == "totale_casi" {
title := "Totale Contagi " + provincesData[provinceId].Denominazione_provincia
var filename string
filename = dirPath + covidgraphs.FilenameCreator(title)
if !covidgraphs.IsGraphExisting(filename) {
provinceIndexes := covidgraphs.GetProvinceIndexesByName(&provincesData, tokens[0])
err, filename = covidgraphs.TotalePositiviProvincia(&provincesData, provinceIndexes, title, filename)
if err != nil {
log.Println(err)
}
}
if err != nil {
log.Println(err)
b.SendMessage(usageMessage, update.Message.Chat.ID, echotron.PARSE_HTML)
return
}
provinceLastId, err := covidgraphs.FindLastOccurrenceProvince(&provincesData, "denominazione_provincia", tokens[0])
if err != nil {
log.Println(err)
b.SendMessage(usageMessage, update.Message.Chat.ID, echotron.PARSE_HTML)
return
}
b.SendPhoto(filename, setCaptionConfrontoProvincia(provinceLastId, []string{tokens[1]}), update.Message.Chat.ID, echotron.PARSE_HTML)
} else if tokens[1] == "nuovi_positivi" {
title := "Nuovi Positivi " + provincesData[provinceId].Denominazione_provincia
var filename string
filename = dirPath + covidgraphs.FilenameCreator(title)
if !covidgraphs.IsGraphExisting(filename) {
provinceIndexes := covidgraphs.GetProvinceIndexesByName(&provincesData, tokens[0])
err, filename = covidgraphs.NuoviPositiviProvincia(&provincesData, provinceIndexes, true, title, filename)
if err != nil {
log.Println(err)
}
}
if err != nil {
log.Println(err)
b.SendMessage(usageMessage, update.Message.Chat.ID, echotron.PARSE_HTML)
return
}
provinceLastId, err := covidgraphs.FindLastOccurrenceProvince(&provincesData, "denominazione_provincia", tokens[0])
if err != nil {
log.Println(err)
b.SendMessage(usageMessage, update.Message.Chat.ID, echotron.PARSE_HTML)
return
}
b.SendPhoto(filename, setCaptionConfrontoProvincia(provinceLastId, []string{tokens[1]}), update.Message.Chat.ID, echotron.PARSE_HTML)
}
b.DeleteMessage(update.Message.Chat.ID, update.Message.ID)
}