-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
151 lines (123 loc) · 3.5 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
package main
import (
"encoding/json"
"github.com/gameraccoon/telegram-poll-bot/database"
"github.com/gameraccoon/telegram-poll-bot/dialogFactories"
"github.com/gameraccoon/telegram-poll-bot/processing"
"github.com/gameraccoon/telegram-poll-bot/telegramChat"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/nicksnyder/go-i18n/i18n"
"io/ioutil"
"log"
"strings"
"sync"
"time"
)
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
i18n.MustLoadTranslationFile("./data/strings/en-us.all.json")
i18n.MustLoadTranslationFile("./data/strings/ru-ru.all.json")
}
func getFileStringContent(filePath string) (content string, err error) {
fileContent, err := ioutil.ReadFile(filePath)
if err == nil {
content = strings.TrimSpace(string(fileContent))
}
return
}
func getApiToken() (token string, err error) {
return getFileStringContent("./telegramApiToken.txt")
}
func loadConfig(path string) (config processing.StaticConfiguration, err error) {
jsonString, err := getFileStringContent(path)
if err == nil {
dec := json.NewDecoder(strings.NewReader(jsonString))
err = dec.Decode(&config)
}
return
}
func updateTimers(staticData *processing.StaticProccessStructs, mutex *sync.Mutex) {
questions := staticData.Db.GetActiveQuestions()
mutex.Lock()
for _, questionId := range questions {
_, _, endTime := staticData.Db.GetQuestionRules(questionId)
if endTime > 0 {
staticData.Timers[questionId] = time.Unix(endTime, 0)
}
}
mutex.Unlock()
for {
currentTime := time.Now()
mutex.Lock()
for questionId, endTime := range staticData.Timers {
if endTime.Sub(currentTime).Seconds() < 0.0 {
delete(staticData.Timers, questionId)
processTimer(staticData, questionId)
}
}
mutex.Unlock()
time.Sleep(30 * time.Second)
}
}
func updateBot(bot *tgbotapi.BotAPI, staticData *processing.StaticProccessStructs, dialogManager *dialogFactories.DialogManager, mutex *sync.Mutex) {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Fatal(err.Error())
}
processors := Processors{
Main: makeUserCommandProcessors(),
Moderator: makeModeratorCommandProcessors(),
}
for update := range updates {
if update.Message == nil {
continue
}
mutex.Lock()
processUpdate(&update, staticData, dialogManager, &processors)
mutex.Unlock()
}
}
func main() {
apiToken, err := getApiToken()
if err != nil {
log.Fatal(err.Error())
}
config, err := loadConfig("./config.json")
if err != nil {
log.Fatal(err.Error())
}
trans, err := i18n.Tfunc(config.Language)
if err != nil {
log.Fatal(err.Error())
}
db := &database.Database{}
err = db.Connect("./polls-data.db")
defer db.Disconnect()
if err != nil {
log.Fatal("Can't connect database")
}
database.UpdateVersion(db)
userStates := make(map[int64]processing.UserState)
timers := make(map[int64]time.Time)
mutex := &sync.Mutex{}
chat, err := telegramChat.MakeTelegramChat(apiToken)
if err != nil {
log.Fatal(err.Error())
}
log.Printf("Authorized on account %s", chat.GetBotUsername())
chat.SetDebugModeEnabled(config.ExtendedLog)
dialogManager := &(dialogFactories.DialogManager{})
dialogManager.RegisterDialogFactory("ed", dialogFactories.MakeQuestionEditDialogFactory(trans))
staticData := &processing.StaticProccessStructs{
Chat: chat,
Db: db,
Config: &config,
Timers: timers,
Trans: trans,
UserStates: userStates,
}
go updateTimers(staticData, mutex)
updateBot(chat.GetBot(), staticData, dialogManager, mutex)
}