-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEurielecBot.py
335 lines (296 loc) · 12 KB
/
EurielecBot.py
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Eurielec Bot V1.0
# Copyright (C) 2017
# Jorge Díez de la Fuente <buker(at)stuker.es>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
from ffmpy import FFmpeg,FFRuntimeError
import flask
import logging
import queue
import re
import requests
import subprocess
import telebot
import threading
import time
from wit import Wit
API_TOKEN = '' # Telegram Bot API TOKEN
WIT_AI_KEY = '' # Wit.ai keys are 32-character uppercase alphanumeric strings
WEBHOOK_HOST = '' # Set domain
WEBHOOK_PORT = 0 # 443, 80, 88 or 8443 (port need to be 'open')
WEBHOOK_LISTEN = '0.0.0.0' # In some VPS you may need to put here the IP addr
IP_Port = '0.0.0.0:0' # Camera IP:Port
CamHTTP = 'http://%s/' % (IP_Port)
CamUSER = 'user' # Camera admin user
CamPASSWD = 'passwd' # Camera admin password
#CamAuthHTTP = 'http://%s:%s@%s/' % (CamUSER, CamPASSWD, IP_Port)
###LOCKS###
CamLock = threading.RLock()
PanLock = threading.RLock()
PanQueue = queue.Queue()
RecordLock = threading.RLock()
RecordQueue = queue.Queue()
RecordPanLock = threading.RLock()
RecordPanQueue = queue.Queue()
###Lists###
Blacklist = [] # Users banned from the service, identified by id
Whitelist = [] # Users allowed to use restricted services, identified by id
WEBHOOK_SSL_CERT = './webhook_cert.pem' # Path to the ssl certificate
WEBHOOK_SSL_PRIV = './webhook_pkey.pem' # Path to the ssl private key
# Quick'n'dirty SSL certificate generation:
#
# openssl genrsa -out webhook_pkey.pem 2048
# openssl req -new -x509 -days 3650 -key webhook_pkey.pem -out webhook_cert.pem
#
# When asked for "Common Name (e.g. server FQDN or YOUR name)" you should reply
# with the same value in you put in WEBHOOK_HOST
WEBHOOK_URL_BASE = 'https://%s:%s' % (WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = '/%s/' % (API_TOKEN)
logger = telebot.logger
telebot.logger.setLevel(logging.INFO)
bot = telebot.TeleBot(API_TOKEN) #create a new Telegram Bot object
wit_client = Wit(access_token=WIT_AI_KEY, actions=None)
commands = { # command description used in the "help" command
'start': 'Get used to the bot',
'help': 'Gives you information about the available commands',
'getImage': 'Request a picture of Eurielec',
'getSofa': 'Request a picture of the Coffee Club',
'getVideo': 'Request a video of Eurielec',
'getSalseo': 'Request a panning video of Eurielec',
'parseHTML': 'Returns formated text from HTML'
}
app = flask.Flask(__name__)
# Empty webserver index, return a non sense string, just http 200
@app.route('/', methods=['GET', 'HEAD'])
def index():
return "<h1 style='color:red'>IMMA CHARGIN MAH LAZER</h1>"
# Process webhook calls
@app.route(WEBHOOK_URL_PATH, methods=['POST'])
def webhook():
if flask.request.headers.get('content-type') == 'application/json':
json_string = flask.request.get_data().decode('utf-8')
update = telebot.types.Update.de_json(json_string)
bot.process_new_updates([update])
return ''
else:
flask.abort(403)
# only used for console output now
def listener(messages):
"""
When new messages arrive TeleBot will call this function.
"""
for m in messages:
if m.content_type == 'text':
# print the sent message to the console
print(str(m.from_user.first_name) + " " + str(m.from_user.last_name) + " [@" + str(m.from_user.username) + "] ["+ str(m.chat.type) + "/" + str(m.chat.id) + "]: " + m.text)
bot.set_update_listener(listener) # register listener
def chk_list(mylist, cid):
return cid in mylist
@bot.message_handler(commands=['start', 'help'])
def command_start_help(m):
cid = m.chat.id
if chk_list(Blacklist, cid):
return
help_text = "The following commands are available:\n"
for key in commands: # generate help text out of the commands dictionary defined at the top
help_text += "/" + key + ": "
help_text += commands[key] + "\n"
bot.send_message(cid, help_text)
@bot.message_handler(commands=['getImage', 'getimage'])
def command_image(m):
cid = m.chat.id
if chk_list(Blacklist, cid):
return
try:
CamLock.acquire()
image = requests.get('%ssnapshot.cgi' % (CamHTTP), auth=(CamUSER, CamPASSWD)).content
CamLock.release()
bot.send_photo(cid, image)
except IOError as e:
error = 'An error occurred while processing your request :('
bot.send_message(cid, error)
print (str(e))
@bot.message_handler(commands=['getSofa', 'getsofa'])
def command_sofa_image(m):
def panning_pic(queue, lock):
try:
CamLock.acquire()
requests.get('%sdecoder_control.cgi?loginuse=%s&loginpas=%s&onestep=0&command=%s' % (CamHTTP, CamUSER, CamPASSWD, '33'), auth=(CamUSER, CamPASSWD))
time.sleep(7)
image = requests.get('%ssnapshot.cgi' % (CamHTTP), auth=(CamUSER, CamPASSWD)).content
lock.acquire()
dest = []
while not queue.empty():
cid = queue.get()
if cid not in dest:
dest.append(cid)
bot.send_photo(cid, image)
lock.release()
requests.get('%sdecoder_control.cgi?loginuse=%s&loginpas=%s&onestep=0&command=%s' % (CamHTTP, CamUSER, CamPASSWD, '31'), auth=(CamUSER, CamPASSWD))
time.sleep(7)
CamLock.release()
except IOError as e:
requests.get('%sdecoder_control.cgi?loginuse=%s&loginpas=%s&onestep=0&command=%s' % (CamHTTP, CamUSER, CamPASSWD, '31'), auth=(CamUSER, CamPASSWD))
CamLock.release()
error = 'An error occurred while processing your request :('
dest = []
lock.acquire()
while not queue.empty():
cid = queue.get()
if cid not in dest:
dest.append(cid)
bot.send_message(cid, error)
lock.release()
print (str(e))
cid = m.chat.id
if chk_list(Blacklist, cid) or not chk_list(Whitelist, cid):
return
recording = 'Taking picture, please wait.'
bot.send_message(cid, recording)
PanLock.acquire()
if PanQueue.empty():
t = threading.Thread(target=panning_pic,args=(PanQueue, PanLock))
t.start()
PanQueue.put(cid)
PanLock.release()
# Record video and send it to a list
def record_send(queue, lock, ffmpeg):
try:
ffmpeg.cmd
CamLock.acquire()
video, stderr = ffmpeg.run(stdout=subprocess.PIPE)
CamLock.release()
dest = []
lock.acquire()
while not queue.empty():
cid = queue.get()
if cid not in dest:
dest.append(cid)
bot.send_video(cid, video)
lock.release()
except FFRuntimeError as e:
CamLock.release()
error = 'An error occurred while processing your request :('
dest = []
lock.acquire()
while not queue.empty():
cid = queue.get()
if cid not in dest:
dest.append(cid)
bot.send_message(cid, error)
lock.release()
print (str(e))
# Build ffmpeg object
def build_ffmpeg(timeleft):
return FFmpeg(
inputs={'%svideostream.cgi?loginuse=%s&loginpas=%s&resolution=32&rate=1' % (CamHTTP, CamUSER, CamPASSWD): '-r 20 -t %s -hide_banner -loglevel panic' % (timeleft)},
#inputs={'%slivestream.cgi?loginuse=%s&loginpas=%s&streamid=0' % (CamHTTP, CamUSER, CamPASSWD): '-re -hide_banner -loglevel panic'},
outputs={'pipe:1': '-movflags frag_keyframe+empty_moov -c:v h264 -f mp4'}
#outputs={'pipe:1': '-movflags frag_keyframe+empty_moov -c copy -f mp4 -t 5'}
)
@bot.message_handler(commands=['getVideo', 'getvideo'])
def command_video(m):
timeleft = 5
cid = m.chat.id
if chk_list(Blacklist, cid):
return
recording = 'Recording video, please wait.'
bot.send_message(cid, recording)
RecordLock.acquire()
if RecordQueue.empty():
ff = build_ffmpeg(timeleft)
t = threading.Thread(target=record_send,args=(RecordQueue, RecordLock, ff))
t.start()
RecordQueue.put(cid)
RecordLock.release()
@bot.message_handler(commands=['getBeta', 'getbeta', 'getSalseo', 'getsalseo'])
def command_panning_video(m):
def panning():
time.sleep(3)
requests.get('%sdecoder_control.cgi?loginuse=%s&loginpas=%s&onestep=0&command=%s' % (CamHTTP, CamUSER, CamPASSWD, '33'), auth=(CamUSER, CamPASSWD))
time.sleep(9)
requests.get('%sdecoder_control.cgi?loginuse=%s&loginpas=%s&onestep=0&command=%s' % (CamHTTP, CamUSER, CamPASSWD, '31'), auth=(CamUSER, CamPASSWD))
timeleft = 18
cid = m.chat.id
if chk_list(Blacklist, cid) or not chk_list(Whitelist, cid):
return
recording = 'Recording video, please wait.'
bot.send_message(cid, recording)
RecordPanLock.acquire()
if RecordPanQueue.empty():
ff = build_ffmpeg(timeleft)
t1 = threading.Thread(target=panning)
t2 = threading.Thread(target=record_send,args=(RecordPanQueue, RecordPanLock, ff))
t1.start()
t2.start()
RecordPanQueue.put(cid)
RecordPanLock.release()
@bot.message_handler(commands=['parseHTML', 'parsehtml'])
def command_parse(m):
cid = m.chat.id
if chk_list(Blacklist, cid):
return
#Should be a varaible dependant from bot.get_me()['username']
mtext = re.sub(r'^/[a-z0-9]+(@eurielecbot)*\s*', '', m.text, flags=re.IGNORECASE)
bot.send_message(cid, mtext, parse_mode='HTML')
@bot.message_handler(commands=['getID', 'getid'])
def command_getid(m):
cid = m.chat.id
if chk_list(Blacklist, cid):
return
bot.send_message(cid, 'This is your ID:')
bot.send_message(cid, cid)
# Handles voice files
@bot.message_handler(content_types=['voice']) # In theory can support also audio files, not tested yet ['audio', 'voice'])
def process_audio(m):
cid = m.chat.id
if chk_list(Blacklist, cid) or not chk_list(Whitelist, cid):
return
bot.send_message(cid, 'Audio received')
fileid = m.voice.file_id
audiofile_info = bot.get_file(fileid)
# Audio file downloaded for further debug, if needed. FFmpeg can handle directly the download, and process in streaming.
r_file = requests.get('https://api.telegram.org/file/bot%s/%s' % (API_TOKEN, audiofile_info.file_path))
if r_file.status_code == 200:
ff = FFmpeg(
inputs={'pipe:0': '-hide_banner -loglevel panic'},
outputs={'pipe:1': '-f mp3 -c:a libmp3lame'}
)
try:
ff.cmd
audio_mp3, stderr = ff.run(input_data=r_file.content, stdout=subprocess.PIPE)
wit_response = wit_client.speech(audio_mp3, None, {'Content-Type': 'audio/mpeg3'})
if '_text' in wit_response:
bot.send_message(cid, wit_response['_text'])
except FFRuntimeError as e:
print (str(e))
#Just for debug purposes
bot.send_voice(cid, r_file.content, caption='Original audio')
bot.send_audio(cid, audio_mp3, caption='MP3 audio')
if __name__ == "__main__":
# Remove webhook, it fails sometimes the set if there is a previous webhook
bot.remove_webhook()
print('Waiting for webhook')
time.sleep(1)
# Set webhook
bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH,
certificate=open(WEBHOOK_SSL_CERT, 'r'))
# Start flask server
app.run(host=WEBHOOK_LISTEN,
port=WEBHOOK_PORT,
ssl_context=(WEBHOOK_SSL_CERT, WEBHOOK_SSL_PRIV),
debug=True)