-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (134 loc) · 3.2 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
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"log"
"os"
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"golang.org/x/text/encoding/charmap"
)
const (
curencyEndpoint string = "http://www.cbr.ru/scripts/XML_daily.asp"
metersDateFormat string = "02.01.2006"
)
func newBot(token string) bot {
api, err := tg.NewBotAPI(os.Getenv("BOT_TOKEN"))
if err != nil {
log.Fatal("unable to connect")
}
if os.Getenv("DEBUG") == "true" {
api.Debug = true
}
return bot{
api,
make(map[int64]handlerFunc),
}
}
type bot struct {
*tg.BotAPI
nextHandler map[int64]handlerFunc
}
func (b *bot) registerNextStepHandler(message *tg.Message, f handlerFunc) {
b.nextHandler[message.Chat.ID] = f
}
func (b *bot) nextStepHandler(message *tg.Message) handlerFunc {
f, ok := b.nextHandler[message.Chat.ID]
if ok {
delete(b.nextHandler, message.Chat.ID)
return f
}
return nil
}
func getAllowedUsers() []user {
users := []user{}
date := []byte(os.Getenv("ALLOWED_USERS"))
json.Unmarshal(date, &users)
return users
}
func isAllowed(message *tg.Message) bool {
users := getAllowedUsers()
for _, user := range users {
if user.ID == message.Chat.ID {
return true
}
}
return false
}
func newXMLDecoder(r io.Reader) *xml.Decoder {
d := xml.NewDecoder(r)
d.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
if charset == "windows-1251" {
return charmap.Windows1251.NewDecoder().Reader(input), nil
}
return nil, fmt.Errorf("unknown charset: %s", charset)
}
return d
}
func (b *bot) setCommands() error {
commands := tg.NewSetMyCommands(
tg.BotCommand{Command: "status", Description: "check bot status"},
tg.BotCommand{Command: "currency", Description: "chech current exchange rate"},
tg.BotCommand{Command: "meters", Description: "set meters"},
tg.BotCommand{Command: "lastmeters", Description: "show last meters"},
tg.BotCommand{Command: "whoami", Description: "show info about requesting user"},
)
r, err := b.Request(commands)
if err != nil {
return err
}
if !r.Ok {
return fmt.Errorf(string(r.Result))
}
return nil
}
func main() {
b := newBot(os.Getenv("BOT_TOKEN"))
log.Printf("authorized account: %s", b.Self.UserName)
u := tg.NewUpdate(0)
u.Timeout = 60
if err := b.setCommands(); err != nil {
log.Fatal(err)
}
updates := b.GetUpdatesChan(u)
for update := range updates {
message := update.Message
if message == nil {
continue
}
if handler := b.nextStepHandler(message); handler != nil {
handler(message)
continue
}
if !message.IsCommand() {
continue
}
switch isAllowed(message) {
case true:
switch update.Message.Command() {
case "status":
b.statusHandler(message)
case "currency":
b.currencyHandler(message)
case "meters":
// checkUser(handleMetersCommand)(message)
b.metersHandler(message)
case "whoami":
b.whoAmIHandler(message)
case "lastmeters":
b.lastmetersHandler(message)
default:
b.unsupportedHandler(message)
}
case false:
switch message.Command() {
case "whoami":
b.whoAmIHandler(message)
default:
b.unsupportedHandler(message)
}
}
// log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
}
}