-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
174 lines (153 loc) · 3.22 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"bufio"
"fmt"
"os"
"strings"
"sync"
)
var mutex sync.Mutex
func _check(err error) {
if err != nil {
panic(err)
}
}
var bot *BotApi
var tm *TaskManager
func commandDispatcher() {
updates := bot.getUpdatesChan()
data_folder := os.Getenv("DATA_FOLDER") + "/"
log_folder := data_folder + "logs/"
os.MkdirAll(log_folder, os.ModePerm)
task_folder := data_folder + "tasks/"
os.MkdirAll(task_folder, os.ModePerm)
logger := Logger{log_folder}
tm = &TaskManager{
taskDir: task_folder,
daily: make(map[int]Task),
dailyId: 0,
backlog: make(map[int]Task),
backlogId: 0,
}
for message := range updates {
splitted := strings.Split(message, " ")
command := splitted[0]
switch command {
case "/start":
bot.send("Hi, i'm a Voice of mind bot!")
case "/log":
if len(splitted) < 2 {
bot.send("Log category is not specified")
continue
}
file := splitted[1]
comment := ""
if len(splitted) > 2 {
comment = strings.Join(splitted[2:], " ")
}
logger.log(file, comment)
lines, err := logger.query(file, 7)
_check(err)
stat := strings.Join(lines, "\n")
bot.send(stat)
case "/logcat":
if len(splitted) < 2 {
bot.send("Log category is not specified")
continue
}
file := splitted[1]
lines, err := logger.query(file, 7)
if err != nil {
bot.send("File not found")
}
stat := strings.Join(lines, "\n")
bot.send(stat)
case "/backlog":
tm.load()
if len(splitted) < 2 {
bot.send(tm.backlogSumm())
continue
}
task_descr := strings.Join(splitted[1:], " ")
tm.addBacklog(task_descr)
summ_msg := tm.backlogSumm()
bot.send(summ_msg)
tm.dump()
case "/tasks":
tm.load()
summ_msg := tm.dailySumm()
bot.send(summ_msg)
case "/complete", "/giveup":
if len(splitted) != 2 {
bot.send("Id is not specified")
continue
}
id := splitted[1]
tm.load()
if !tm.completeDaily(parseInt(id)) {
bot.send("Unknown task ID")
continue
}
summ_msg := tm.dailySumm()
bot.send(summ_msg)
tm.dump()
case "/take":
if len(splitted) != 2 {
bot.send("Id is not specified")
continue
}
id := splitted[1]
tm.load()
if !tm.takeToDaily(parseInt(id), false) {
bot.send("Unknown task ID")
continue
}
summ_msg := tm.dailySumm()
bot.send(summ_msg)
tm.dump()
case "/pick":
if len(splitted) != 2 {
bot.send("Id is not specified")
continue
}
id := splitted[1]
tm.load()
if !tm.takeToDaily(parseInt(id), true) {
bot.send("Unknown task ID")
continue
}
summ_msg := tm.dailySumm()
bot.send(summ_msg)
tm.dump()
case "/stop":
bot.send("Sorry, stop is not implemented, keep going!")
default:
fmt.Println("Other command: " + command + " orig text: " + message)
}
}
}
func main() {
bot = &BotApi{
isDebug: false,
bot: nil,
chatID: 0,
}
if len(os.Args) == 2 && os.Args[1] == "debug" {
bot.isDebug = true
}
bot.init()
initCallbacks()
if bot.isDebug {
go func() {
defer close(bot.adapterChan)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
bot.adapterChan <- scanner.Text()
}
if scanner.Err() != nil {
fmt.Println(scanner.Err().Error())
}
}()
}
commandDispatcher()
}