-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtelegram_bot.py
382 lines (332 loc) · 13.4 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!./venv/bin/python
import logging
from typing import Callable
import ffmpeg.stream
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import (
filters,
ApplicationBuilder,
ContextTypes,
CommandHandler,
MessageHandler,
CallbackQueryHandler,
)
from firebase import save_message as fb_save_message, get_last_messages, get_message
from md2tgmd import escape
import os
from dotenv import load_dotenv
from open_ai import (
ai_describe_image,
ai_retell,
ai_identify_parameters,
ai_retell,
ai_summarize,
ai_transcript_audio,
)
import base64
import ffmpeg
from tools import process_urls, extract_urls_from_message
load_dotenv()
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(
chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!"
)
async def save_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
if update == None or update.message == None:
return
image_description = await process_images(update, context)
fb_message_id = fb_save_message(update.message, image_description)
try:
message_text = (
update.message.text
if update.message.text != None
else (
update.message.caption
if update.message.caption != None
else image_description
)
)
if message_text == None:
return
urls = extract_urls_from_message(message_text)
if urls != None and len(urls) > 0:
chat_id = update.effective_chat.id
await context.bot.send_message(
chat_id=chat_id,
text="Actions:",
reply_to_message_id=update.message.message_id,
parse_mode="MarkdownV2",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
"Get URL summary",
callback_data="u:"
+ str(chat_id)
+ ":"
+ fb_message_id
+ ":"
+ str(update.message.message_id),
),
]
]
),
)
except Exception as error:
await context.bot.send_message(
chat_id=update.effective_chat.id, text=repr(error)
)
async def process_images(update: Update, context: ContextTypes.DEFAULT_TYPE) -> str:
image_description = None
if (
update != None
and update.message != None
and update.message.photo != None
and len(update.message.photo) > 0
):
highest_quality_photo_id = update.message.photo[-1].file_id
file = await context.bot.get_file(highest_quality_photo_id)
byte_array = await file.download_as_bytearray()
image_data = base64.b64encode(byte_array).decode("utf-8")
image_description = ai_describe_image(image_data)
return image_description
async def default_summarize(
update: Update, context: ContextTypes.DEFAULT_TYPE, ai_func: Callable
):
try:
limit = (
int(context.args[0])
if (len(context.args) > 0 and context.args[0] is not None)
else int(os.getenv("DEFAULT_HISTORY_LENGTH"))
)
params = {}
params["history_length"] = limit
params["language"] = os.getenv("DEFAULT_LANGUAGE")
params["tone"] = os.getenv("DEFAULT_TONE")
params["chat_id"] = str(update.message.chat.id)
messages = get_last_messages(str(update.message.chat.id), limit)
result = ai_func(messages, params, update.message.chat.type)
result = escape(result)
await context.bot.send_message(
chat_id=update.effective_chat.id, text=result, parse_mode="MarkdownV2"
)
except Exception as error:
await context.bot.send_message(
chat_id=update.effective_chat.id, text=repr(error)
)
async def summarize(update: Update, context: ContextTypes.DEFAULT_TYPE):
await default_summarize(update, context, ai_summarize)
async def retell(update: Update, context: ContextTypes.DEFAULT_TYPE):
await default_summarize(update, context, ai_retell)
async def get_chat_id(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(
chat_id=update.effective_chat.id, text="Chat id: " + str(update.message.chat.id)
)
async def bot_mention(update: Update, context: ContextTypes.DEFAULT_TYPE):
if update == None or update.message == None:
return
try:
params = ai_identify_parameters(update.message.text)
if params["history_length"] is None:
params["history_length"] = int(os.getenv("DEFAULT_HISTORY_LENGTH"))
if params["language"] is None:
params["language"] = os.getenv("DEFAULT_LANGUAGE")
if params["tone"] is None:
params["tone"] = os.getenv("DEFAULT_TONE")
params["chat_id"] = str(update.message.chat.id)
messages = get_last_messages(
str(update.message.chat.id), params["history_length"]
)
result = ai_retell(messages, params)
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=escape(result),
parse_mode="MarkdownV2",
)
except Exception as error:
await context.bot.send_message(
chat_id=update.effective_chat.id, text=repr(error)
)
async def process_voice_message(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
path_to_file = None
try:
new_file = await context.bot.get_file(update.message.voice.file_id)
path_to_file = await new_file.download_to_drive()
transcription = ai_transcript_audio(path_to_file)
fb_save_message(update.message, transcription)
if os.getenv("IS_ECHO_VOICE_MESSAGES") == "true":
reply_text = (
f"*{update.message.from_user.username}*: {escape(transcription)}"
)
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=reply_text,
reply_to_message_id=update.message.message_id,
parse_mode="MarkdownV2",
)
except:
pass
finally:
if path_to_file != None:
path_to_file.unlink()
async def process_video_message(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
path_to_video_file = None
audio_file_path = None
try:
new_file = await context.bot.get_file(update.message.video.file_id)
path_to_video_file = await new_file.download_to_drive()
audio_file_path = path_to_video_file.with_suffix(".mp3")
ffmpeg.input(path_to_video_file).output(audio_file_path.name).run()
transcription = ai_transcript_audio(audio_file_path)
fb_message_id = fb_save_message(update.message, transcription)
if os.getenv("IS_ECHO_VOICE_MESSAGES") == "true":
if os.getenv("ECHO_VOICE_MESSAGE_TYPE") == "BUTTON":
await context.bot.send_message(
chat_id=update.effective_chat.id,
text="Actions:",
reply_to_message_id=update.message.message_id,
parse_mode="MarkdownV2",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
"Get transcription",
callback_data="m:"
+ str(update.effective_chat.id)
+ ":"
+ fb_message_id
+ ":"
+ str(update.message.message_id),
),
]
]
),
)
else:
reply_text = (
f"*{update.message.from_user.username}*: {escape(transcription)}"
)
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=reply_text,
reply_to_message_id=update.message.message_id,
parse_mode="MarkdownV2",
)
except:
pass
finally:
if path_to_video_file != None:
path_to_video_file.unlink()
if audio_file_path != None:
audio_file_path.unlink()
async def process_video_note_message(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
path_to_video_file = None
audio_file_path = None
try:
new_file = await context.bot.get_file(update.message.video_note.file_id)
path_to_video_file = await new_file.download_to_drive()
audio_file_path = path_to_video_file.with_suffix(".mp3")
ffmpeg.input(path_to_video_file).output(audio_file_path.name).run()
transcription = ai_transcript_audio(audio_file_path)
fb_message_id = fb_save_message(update.message, transcription)
if os.getenv("IS_ECHO_VOICE_MESSAGES") == "true":
# if os.getenv("ECHO_VOICE_MESSAGE_TYPE") == "BUTTON":
# chat_id = update.effective_chat.id
# await context.bot.send_message(
# chat_id=chat_id,
# text="Actions:",
# reply_to_message_id=update.message.message_id,
# parse_mode="MarkdownV2",
# reply_markup=InlineKeyboardMarkup(
# [
# InlineKeyboardButton(
# "Get transcription",
# callback_data="t:"+str(chat_id)
# + ":"
# + fb_message_id
# + ":"
# + str(update.message.message_id),
# ),
# ]
# ),
# )
# else:
reply_text = (
f"*{update.message.from_user.username}*: {escape(transcription)}"
)
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=reply_text,
reply_to_message_id=update.message.message_id,
parse_mode="MarkdownV2",
)
except:
pass
finally:
if path_to_video_file != None:
path_to_video_file.unlink()
if audio_file_path != None:
audio_file_path.unlink()
async def process_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Parses the CallbackQuery and updates the message text."""
query = update.callback_query
await query.edit_message_text(text="🤖 Processing... 🍆💦")
type, chat_id, fb_message_id, reply_to_message_id = query.data.split(":")
text = None
if type == "u":
message = get_message(chat_id, fb_message_id).to_dict()
message_text = message["text"]
url_summaries = await process_urls(message_text)
if url_summaries == None or len(url_summaries) == 0:
return
for summary in url_summaries:
title = (
summary.metadata["title"] if "title" in summary.metadata else "Summary"
)
text = f"*{escape(title)}*: {escape(summary.page_content)}"
elif type == "m":
message = get_message(chat_id, fb_message_id).to_dict()
text = escape(message["text"])
# CallbackQueries need to be answered, even if no notification to the user is needed
# Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery
await query.answer()
await query.edit_message_text(text=text, parse_mode="MarkdownV2")
def start_bot():
bot_name = os.getenv("TELEGRAM_BOT_NAME")
application = ApplicationBuilder().token(os.getenv("TELEGRAM_TOKEN")).build()
start_handler = CommandHandler("start", start)
s_handler = CommandHandler("s", summarize)
summarize_handler = CommandHandler("summarize", summarize)
chat_id_handler = CommandHandler("chatid", get_chat_id)
mention_handler = MessageHandler(filters.Mention(bot_name), bot_mention)
voice_message_handler = MessageHandler(filters.VOICE, process_voice_message)
video_message_handler = MessageHandler(filters.VIDEO, process_video_message)
video_note_message_handler = MessageHandler(
filters.VIDEO_NOTE, process_video_note_message
)
save_message_handler = MessageHandler(
(filters.TEXT | filters.PHOTO)
& (~filters.COMMAND)
& (~filters.Mention(bot_name)),
save_message,
)
application.add_handler(start_handler)
application.add_handler(s_handler)
application.add_handler(summarize_handler)
application.add_handler(save_message_handler)
application.add_handler(chat_id_handler)
application.add_handler(mention_handler)
application.add_handler(voice_message_handler)
application.add_handler(video_message_handler)
application.add_handler(video_note_message_handler)
application.add_handler(CallbackQueryHandler(process_callback))
application.run_polling()