-
Notifications
You must be signed in to change notification settings - Fork 0
/
telecom-tower-telegram-bot.go
262 lines (221 loc) · 6.73 KB
/
telecom-tower-telegram-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
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
// Copyright 2016 Jacques Supcik, HEIA-FR
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 2015-12-29 | JS | First version
// 2016-07-26 | JS | Add Firebase Database
// Infos for BotFather
/*
/setcommands
print - print message on tower
cancel - cancel current operation
status - display current message
/setdescription
This bot displays your message on the LED display of the telecom tower of the "Haute école d'ingénierie et d'architecture Fribourg"
Source code available on github: https://github.com/heia-fr/telecom-tower-telegram-bot
/setabouttext
By Jacques Supcik - Haute école d'ingénierie et d'architecture Fribourg - Filière télécommunications
*/
//
// Telegram bot
//
package main
import (
"encoding/json"
"flag"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/heia-fr/telecom-tower/rollrenderer"
"github.com/nats-io/nats"
"github.com/tucnak/telebot"
"github.com/vharitonsky/iniflags"
"github.com/BlueMasters/firebasedb"
"net/http"
"strings"
"time"
)
type session struct {
state stateFn
color string
anonymous bool
conversation telebot.Chat
sender telebot.User
message telebot.Message
}
const (
longPollTime = 300 * time.Second
pingPeriod = 30 * time.Second
maxMsgLen = 64 // The tower can show up to 21 characters (using 6x8 font).
maxSenderLen = 32 // The tower can show up to 21 characters (using 6x8 font).
)
// the bot will send notifications to these channels
var notificationChannels = [...]string{"telecom_tower_notifications"}
var bot *telebot.Bot
var sessions = make(map[string]*session) // the key is the telegram chat ID and the user ID
var fbase firebasedb.Reference
func dispatchMessage(sender, text, color string) {
msg := rollrenderer.TextMessage{
Introduction: []rollrenderer.Line{
{Text: "", Font: 6, Color: "#000000"}},
Conclusion: []rollrenderer.Line{
{Text: " // ", Font: 6, Color: "#0000FF"}},
Separator: []rollrenderer.Line{{
Text: " -- ", Font: 6, Color: "#FFFFFF"}},
}
if sender == "" {
msg.Body = []rollrenderer.Line{
{Text: text, Font: 6, Color: color},
}
} else {
msg.Body = []rollrenderer.Line{
{
Text: fmt.Sprintf("%s says: ", sender),
Font: 6, Color: "#FFFFFF"},
{
Text: text, Font: 6, Color: color},
}
}
saveMessage(msg)
bitmap := rollrenderer.RenderMessage(&msg)
if err := fbase.Ref("currentMessage").Set(&msg); err != nil {
log.Info(err)
}
if err := fbase.Ref("currentBitmap").Set(&bitmap); err != nil {
log.Info(err)
}
natsClient.conn.Publish(natsClient.subject, &bitmap)
}
func publish(w http.ResponseWriter, r *http.Request) {
var msg struct {
Sender string `json:"sender"`
Message string `json:"message"`
Color string `json:"color"`
}
err := json.NewDecoder(r.Body).Decode(&msg)
if err != nil {
http.Error(w, err.Error(), 403)
return
}
if len(msg.Sender) > maxSenderLen {
http.Error(w, "Sender too long", 403)
return
}
if len(msg.Message) > maxMsgLen {
http.Error(w, "Message too long", 403)
return
}
color, ok := colorNames[strings.ToLower(msg.Color)]
if !ok {
http.Error(w, "Invalid color", 403)
return
}
dispatchMessage(msg.Sender, msg.Message, color)
}
func stream(w http.ResponseWriter, r *http.Request) {
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { return true },
}
log.Debugln("Start streaming")
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Errorf("Error upgrading connection: %s", err)
return
}
defer conn.Close()
// readloop, just ignore all incoming websocket messages
go func(c *websocket.Conn) {
for {
if _, _, err := c.NextReader(); err != nil {
log.Errorf("Read error: %s. Closing", err)
c.Close()
break
}
}
}(conn)
if strings.ToLower(r.FormValue("skip")) != "true" {
msg, err := loadMessage()
if err == nil {
log.Debugln("Sending initial message")
conn.WriteJSON(rollrenderer.RenderMessage(&msg))
}
}
var messageChannel = make(chan *rollrenderer.BitmapMessage)
natsClient.conn.BindRecvChan(natsClient.subject, messageChannel)
ticker := time.NewTicker(pingPeriod)
defer ticker.Stop()
mainLoop:
for {
select {
case message := <-messageChannel:
log.Debugln("Sending message")
if err := conn.WriteJSON(message); err != nil {
log.Errorf("Error encoding message: %s", err)
break mainLoop
}
case <-ticker.C:
log.Debugln("Ping")
if err := conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
log.Errorf("Error sending ping: %s", err)
break mainLoop
}
}
}
log.Debugln("End of streaming")
}
var silent *bool
func main() {
var debug = flag.Bool("debug", false, "Debug mode")
silent = flag.Bool("silent", false, "Silent")
var token = flag.String("telegram-token", "", "Telegram Token")
var natsURL = flag.String("nats-url", nats.DefaultURL, "NATS URL")
var natsSubject = flag.String("nats-subject", "telecom-tower", "NATS Subject")
var dbName = flag.String("database", "./database.bolt", "Bolt database name")
var httpPort = flag.String("port", "8100", "Server port")
var fireBaseURL = flag.String("firebase-url", "https://telecom-tower.firebaseio.com", "Firebase URL")
var fireBaseToken = flag.String("firebase-token", "", "Firebase Token")
iniflags.Parse()
if *debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
log.Infoln("Starting Telegram bot")
if err := openDB(*dbName); err != nil {
log.Fatalf("Error opening Bolt database: %s", err)
}
var err error
if err := openNats(*natsURL, *natsSubject); err != nil {
log.Fatalf("Error connecting to NATS server: %s", err)
}
// Connect to Firebase Database
fbase = firebasedb.NewReference(*fireBaseURL)
if fbase.Error != nil {
log.Fatalf("Error opening firebase: %s", err)
}
fbase = fbase.Auth(firebasedb.Secret{Token:*fireBaseToken})
bot, err = telebot.NewBot(*token)
if err != nil {
log.Fatalf("Error creating bot: %s", err)
}
go processTelegramMessages(bot)
r := mux.NewRouter()
r.HandleFunc("/stream", stream)
r.HandleFunc("/publish", publish).Methods("POST")
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./html/")))
http.Handle("/", r)
http.ListenAndServe(fmt.Sprintf(":%s", *httpPort), nil)
log.Infoln("Terminated.")
}