-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_socket.py
240 lines (169 loc) · 9.11 KB
/
telegram_socket.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
import os
import re
import time
import datetime
import random
import string
import sys
import traceback
from time import sleep
from collections import defaultdict
from time import time
import datetime
import pytz
from controllers.main import dialog_flow_engine
from utils import log,timed
import telegram
from telegram.ext.dispatcher import run_async
from telegram.ext import Updater, CommandHandler, Filters, MessageHandler
from telegram.error import NetworkError, Unauthorized
from threading import Thread, current_thread
#from messenger import Message
TIMEOUT_SECONDS = 3600
QUEUE_TIME_THRESHOLD = 2
class TelegramBot():
def __init__(self, token):
self.bot = telegram.Bot(token)
self.message_queues = {}
self.is_replying = {}
@timed
def send_message(self,user_id,text_response,keyboard,image):
log('DEBUG',f"Trying to send a message block to user id {user_id} ")
len_text_response_no_image = len([value for value in text_response if not re.match('image',value)])
i=0
log('DEBUG',text_response)
for res in text_response:
self.bot.sendChatAction(chat_id=user_id, action = telegram.ChatAction.TYPING)
#
#sleep(min(len(res)/25,1.5))
if not re.match('image',res):
i+=1
if i == len_text_response_no_image:
self.bot.send_message(chat_id=user_id, text=res, reply_markup = keyboard)
else:
self.bot.send_message(chat_id=user_id, text=res, reply_markup = telegram.ReplyKeyboardRemove())
else:
try:
if "png" in image.name:
self.bot.send_photo(chat_id=user_id, photo=image,timeout=10)
elif "gif" in image.name:
self.bot.send_animation(chat_id=user_id,animation=image, timeout=10)
except Exception as error:
log('ERROR',f"Image fail image: {image} to send to user id {user_id} with error {error}")
log('DEBUG',f"Image sent to user id {user_id}")
log('DEBUG',f"Delaying the next message")
if not i == len_text_response_no_image:
self.bot.sendChatAction(chat_id=user_id, action = telegram.ChatAction.TYPING)
if len(res)<25:
sleep(1)
elif len(res) > 25 and len(res)<75:
sleep(2)
elif len(res) > 75 and len(res)<125:
sleep(3)
else:
sleep(4)
log('DEBUG',f"Message block sent to user id {user_id}")
self.is_replying[user_id] = False
def get_keyboard(self,reply_markup):
if(reply_markup['type'] == 'choice'):
return telegram.ReplyKeyboardRemove()
elif(reply_markup['type']=='inlineButton'):
buttons = reply_markup['text'].split("|")
keyboards =[telegram.KeyboardButton(name) for name in buttons]
if (any([True if len(x)>20 else False for x in buttons])):
keyboards_formatted = [[x] for x in keyboards]
else:
keyboards_formatted = [ [x,y] for x,y in zip(keyboards[0::2], keyboards[1::2]) ] #cut the list to make sure two by two buttons appears
if len(keyboards)%2 ==1:
keyboards_formatted.append([keyboards[-1]]) # add the first button in one line
return telegram.ReplyKeyboardMarkup(keyboards_formatted, resize_keyboard= reply_markup['resize_keyboard'])
elif(reply_markup['type']=='default'):
return telegram.ReplyKeyboardRemove()
else:
return telegram.ReplyKeyboardRemove()
@run_async
def process_message(self, user_id, datetime_info,query):
"""
"""
response = dialog_flow_engine(user_id,datetime_info,user_message=query)
keyboard = self.get_keyboard(response['reply_markup'])
image = response['img']
#print("__________________________ Current thread ________" + str(current_thread()))
if len(response['response_list'])>0:
self.send_message(user_id,response['response_list'],keyboard,image)
@run_async
def callback_handler(self, update, context):
"""
Wrapper function to call the message handler
This function also act as an incoming message queue, for every user , if user sends several in a row,
it will be stored and considered as one message.
> This is to fix a bug where people would often send two, three messages to answer to only one message
This function will also catch and print out errors in the console
"""
#print("__________________________ Current thread ________" + str(current_thread()))
try:
if update.message is not None: # message was None sometime maybe due to https://github.com/python-telegram-bot/python-telegram-bot/issues/469
message = update.message
incoming_delta = datetime.datetime.utcnow() - message.date
datetime_info = {"incoming_delta":incoming_delta,"sent_datetime":message.date}
log('DEBUG',f"Message received and was sent at {message.date}, delay to receive is {incoming_delta.seconds} ")
log('TIME TOOK',f'Time delta for incoming telegram_message in file telegram_socket.py is {incoming_delta.seconds} s')
if message.chat_id not in self.is_replying:
self.is_replying[message.chat_id] = False
if message.chat_id in self.message_queues:
message_queue = self.message_queues[message.chat_id]
if not self.is_replying[message.chat_id] == True:
message_queue.append({'text':message.text,'date':datetime.datetime.utcnow()})
else:
log('INFO',f"We just completely ignored user's message {message.text}")
else :
self.message_queues[message.chat_id] = [{'text':message.text,'date':datetime.datetime.utcnow()}]
message_queue = self.message_queues[message.chat_id]
queue_size = len(message_queue)
#we implemented a queue because people would send message in a row
while message_queue:
sleep(0.1) # without this it may call the telegram api too much
if queue_size < len(message_queue):
break
#self.bot.sendChatAction(chat_id=message.chat_id, action = telegram.ChatAction.TYPING)
delta = datetime.datetime.utcnow() - message_queue[-1]['date'] # calculating UTC time delta
if delta.seconds > QUEUE_TIME_THRESHOLD and not self.is_replying[message.chat_id] == True :
final_message = ""
final_message = " ".join([element['text'] for element in message_queue])
self.is_replying[message.chat_id] = True
self.process_message(message.chat_id,datetime_info, final_message)
message_queue.clear()
else:
log('DEBUG',f"Update.message is None, here is a dump of update: {update} ")
except (BaseException,Exception) as error:
log('ERROR',''.join(traceback.TracebackException.from_exception(error).stack.format()))
except:
exc_info = sys.exc_info()
finally:
traceback.print_exception(*exc_info)
del exc_info
def error_callback(self,bot, update, error):
raise error
def run(self):
"""
Run the bot.
"""
updater = Updater(token,use_context = True)
dp = updater.dispatcher # Get the dispatcher to register handlers
dp.run_async(self.callback_handler)
dp.run_async(self.process_message)
dp.add_handler(CommandHandler("start", self.callback_handler))
dp.add_handler(MessageHandler(Filters.text, self.callback_handler))
dp.add_handler(CommandHandler("switch", self.callback_handler))
dp.add_error_handler(self.error_callback)
log('INFO',"Started telegram bot socket ... (Ctrl-C to exit)")
updater.start_polling()
#updater.idle()
if __name__ == '__main__':
# Telegram Bot Authorization Token Must be set in env variables
token = os.getenv("TELEGRAM_BOT_TOKEN")
if token is None:
log('ERROR','Not token has been found, telegram socket cannot be launched')
raise ValueError
bot = TelegramBot(token)
bot.run()