-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_bot.py
258 lines (214 loc) · 7.47 KB
/
telegram_bot.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
import telegram
import os
import constants.constants as cts
import json
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters
import re
from utils import get_class_from_string, download_page_in_pdf
from nlp.nlp import NLP as NLPClass
from voice.voicetotext import VoiceToText as VoiceToTextAbstract
import asyncio
from oauth2client.service_account import ServiceAccountCredentials
with open("config.json", "r") as f:
config = json.load(f)
VoiceToTextClass = get_class_from_string(config["voice"])
with open(os.path.join(cts.CREDENTIALS_FOLDER, cts.API_CREDENTIALS_FILE), 'r') as file:
api_credentials = json.load(file)
NLP = NLPClass()
VOICE: VoiceToTextAbstract = VoiceToTextClass()
TOKEN = api_credentials["telegram"]
SHEET_ID = api_credentials["spreadsheet_id"]
GID = api_credentials["spreadsheet_dashboard_guid"]
GSCREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name(
os.path.join(cts.CREDENTIALS_FOLDER, cts.CREDENTIALS_FILE),
['https://spreadsheets.google.com/feeds'],
)
bot = telegram.Bot(token=TOKEN)
def message_is_ready(message, previous_message):
"""
Check if the message is ready to be processed.
Parameters
----------
message : str
The current message.
previous_message : str
The previous message.
Returns
-------
bool
True if the message is ready to be processed, False otherwise.
"""
process_message = '[DONE]' not in message
process_message = process_message and message != previous_message
process_message = process_message and message != ''
return process_message
def escape_markdown_v2(text):
"""
Escape special characters for MarkdownV2.
Parameters
----------
text : str
The text to be escaped.
Returns
-------
str
The escaped text.
"""
escape_chars = r'_[]()~`>#+-=|{}.!'
new_text = re.sub(f'([{re.escape(escape_chars)}])', r'\\\1', text)
return new_text.replace('**', '*')
async def send_response_progressively(chat_id, sent_msg, question, parse_mode=None):
"""
Send the response progressively to the user.
Parameters
----------
chat_id : int
The chat ID to send the response to.
sent_msg : telegram.Message
The message object of the sent message.
question : str
The question to generate a response for.
parse_mode : str, optional
The parse mode for the message (default is None).
Returns
-------
None
"""
response_text = ""
previous_message = ""
num_new_characters = 50
new_characters = -1
for chunk in NLP.generate_response(question):
try:
if 'MarkdownV2' in parse_mode:
chunk = escape_markdown_v2(chunk)
response_text += chunk
except TypeError:
continue
try:
if message_is_ready(response_text, previous_message):
if (new_characters > num_new_characters):
await bot.send_chat_action(chat_id, "typing")
await bot.edit_message_text(
chat_id=chat_id,
message_id=sent_msg.message_id,
text=response_text,
parse_mode=parse_mode,
)
previous_message = response_text
new_characters = -1
# Pause for a moment to avoid hitting the rate limit
await asyncio.sleep(0.5)
new_characters += 1
except telegram.error.RetryAfter as e:
print(f"Waiting {e.retry_after} seconds...")
await asyncio.sleep(e.retry_after)
except telegram.error.BadRequest as e:
await asyncio.sleep(1)
continue
# Send the final message
while True:
try:
await bot.send_chat_action(chat_id, "typing")
await bot.edit_message_text(
chat_id=chat_id,
message_id=sent_msg.message_id,
text=response_text,
parse_mode=parse_mode,
)
break
except telegram.error.RetryAfter as e:
print(f"Waiting {e.retry_after} seconds...")
await asyncio.sleep(e.retry_after)
except telegram.error.BadRequest as e:
print(f"Error: {e}")
break
async def start(update: Update, context):
"""
Handle the /start command.
Parameters
----------
update : telegram.Update
The update object.
context : telegram.ext.CallbackContext
The callback context.
Returns
-------
None
"""
await update.message.reply_text(
"¡Hola! Soy un bot que puede responder tus preguntas."
"Puedes enviarme un mensaje de voz o texto y te responderé lo mejor que pueda."
)
async def handle_audio(update: Update, context):
"""
Handle audio messages.
Parameters
----------
update : telegram.Update
The update object.
context : telegram.ext.CallbackContext
The callback context.
Returns
-------
None
"""
chat_id = update.message.chat.id
file_id = update.message.voice.file_id if update.message.voice else update.message.audio.file_id
new_file = await context.bot.get_file(file_id)
await bot.send_chat_action(chat_id, "typing")
sent_msg = await bot.send_message(chat_id, escape_markdown_v2("..."), parse_mode="MarkdownV2")
audio_path = f"temp_audio.ogg"
await new_file.download_to_drive(audio_path)
transcribed_text = VOICE.transcribe(audio_path)
os.remove(audio_path)
await send_response_progressively(chat_id, sent_msg, transcribed_text, parse_mode="MarkdownV2")
print("Ready!")
async def handle_question(update: Update, context):
"""
Handle text messages.
Parameters
----------
update : telegram.Update
The update object.
context : telegram.ext.CallbackContext
The callback context.
Returns
-------
None
"""
question = update.message.text
chat_id = update.message.chat.id
message_id = update.message.message_id
await bot.send_chat_action(chat_id, "typing")
sent_msg = await bot.send_message(chat_id, escape_markdown_v2("..."), parse_mode="MarkdownV2")
await send_response_progressively(chat_id, sent_msg, question, parse_mode="MarkdownV2")
print("Ready!")
async def download_dashboard(update: Update, context):
"""
Download a dashboard in PDF format.
Parameters
----------
update : telegram.Update
The update object.
context : telegram.ext.CallbackContext
The callback context.
"""
chat_id = update.message.chat.id
await bot.send_chat_action(chat_id, "upload_document")
filename = download_page_in_pdf(GSCREDENTIALS, SHEET_ID, GID)
if filename:
await bot.send_document(chat_id, open(filename, 'rb'))
os.remove(filename)
print("Ready!")
else:
await bot.send_message(chat_id, "No se pudo descargar el dashboard.")
print("Ready! (Failed)")
if __name__ == '__main__':
application = Application.builder().token(TOKEN).build()
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("dashboard", download_dashboard))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_question))
application.add_handler(MessageHandler(filters.VOICE | filters.AUDIO, handle_audio))
application.run_polling()