mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2025-12-20 14:14:52 +01:00
use reply_text and get_thread_id
This commit is contained in:
@@ -171,9 +171,10 @@ class ChatGPTTelegramBot:
|
|||||||
if chat_id not in self.last_message:
|
if chat_id not in self.last_message:
|
||||||
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id})'
|
logging.warning(f'User {update.message.from_user.name} (id: {update.message.from_user.id})'
|
||||||
f' does not have anything to resend')
|
f' does not have anything to resend')
|
||||||
await context.bot.send_message(chat_id=chat_id,
|
await update.effective_message.reply_text(
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
text=localized_text('resend_failed', self.config['bot_language']))
|
text=localized_text('resend_failed', self.config['bot_language'])
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Update message text, clear self.last_message and send the request to prompt
|
# Update message text, clear self.last_message and send the request to prompt
|
||||||
@@ -200,9 +201,10 @@ class ChatGPTTelegramBot:
|
|||||||
chat_id = update.effective_chat.id
|
chat_id = update.effective_chat.id
|
||||||
reset_content = message_text(update.message)
|
reset_content = message_text(update.message)
|
||||||
self.openai.reset_chat_history(chat_id=chat_id, content=reset_content)
|
self.openai.reset_chat_history(chat_id=chat_id, content=reset_content)
|
||||||
await context.bot.send_message(chat_id=chat_id,
|
await update.effective_message.reply_text(
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
text=localized_text('reset_done', self.config['bot_language']))
|
text=localized_text('reset_done', self.config['bot_language'])
|
||||||
|
)
|
||||||
|
|
||||||
async def image(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
async def image(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
"""
|
"""
|
||||||
@@ -215,9 +217,10 @@ class ChatGPTTelegramBot:
|
|||||||
chat_id = update.effective_chat.id
|
chat_id = update.effective_chat.id
|
||||||
image_query = message_text(update.message)
|
image_query = message_text(update.message)
|
||||||
if image_query == '':
|
if image_query == '':
|
||||||
await context.bot.send_message(chat_id=chat_id,
|
await update.effective_message.reply_text(
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
text=localized_text('image_no_prompt', self.config['bot_language']))
|
text=localized_text('image_no_prompt', self.config['bot_language'])
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
logging.info(f'New image generation request received from user {update.message.from_user.name} '
|
logging.info(f'New image generation request received from user {update.message.from_user.name} '
|
||||||
@@ -226,8 +229,7 @@ class ChatGPTTelegramBot:
|
|||||||
async def _generate():
|
async def _generate():
|
||||||
try:
|
try:
|
||||||
image_url, image_size = await self.openai.generate_image(prompt=image_query)
|
image_url, image_size = await self.openai.generate_image(prompt=image_query)
|
||||||
await context.bot.send_photo(
|
await update.effective_message.reply_photo(
|
||||||
chat_id=chat_id,
|
|
||||||
reply_to_message_id=self.get_reply_to_message_id(update),
|
reply_to_message_id=self.get_reply_to_message_id(update),
|
||||||
photo=image_url
|
photo=image_url
|
||||||
)
|
)
|
||||||
@@ -240,9 +242,8 @@ class ChatGPTTelegramBot:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception(e)
|
logging.exception(e)
|
||||||
await context.bot.send_message(
|
await update.effective_message.reply_text(
|
||||||
chat_id=chat_id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
|
||||||
reply_to_message_id=self.get_reply_to_message_id(update),
|
reply_to_message_id=self.get_reply_to_message_id(update),
|
||||||
text=f"{localized_text('image_fail', self.config['bot_language'])}: {str(e)}",
|
text=f"{localized_text('image_fail', self.config['bot_language'])}: {str(e)}",
|
||||||
parse_mode=constants.ParseMode.MARKDOWN
|
parse_mode=constants.ParseMode.MARKDOWN
|
||||||
@@ -272,9 +273,8 @@ class ChatGPTTelegramBot:
|
|||||||
await media_file.download_to_drive(filename)
|
await media_file.download_to_drive(filename)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception(e)
|
logging.exception(e)
|
||||||
await context.bot.send_message(
|
await update.effective_message.reply_text(
|
||||||
chat_id=chat_id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
|
||||||
reply_to_message_id=self.get_reply_to_message_id(update),
|
reply_to_message_id=self.get_reply_to_message_id(update),
|
||||||
text=(
|
text=(
|
||||||
f"{localized_text('media_download_fail', bot_language)[0]}: "
|
f"{localized_text('media_download_fail', bot_language)[0]}: "
|
||||||
@@ -293,9 +293,8 @@ class ChatGPTTelegramBot:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception(e)
|
logging.exception(e)
|
||||||
await context.bot.send_message(
|
await update.effective_message.reply_text(
|
||||||
chat_id=update.effective_chat.id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
|
||||||
reply_to_message_id=self.get_reply_to_message_id(update),
|
reply_to_message_id=self.get_reply_to_message_id(update),
|
||||||
text=localized_text('media_type_fail', bot_language)
|
text=localized_text('media_type_fail', bot_language)
|
||||||
)
|
)
|
||||||
@@ -329,9 +328,8 @@ class ChatGPTTelegramBot:
|
|||||||
chunks = self.split_into_chunks(transcript_output)
|
chunks = self.split_into_chunks(transcript_output)
|
||||||
|
|
||||||
for index, transcript_chunk in enumerate(chunks):
|
for index, transcript_chunk in enumerate(chunks):
|
||||||
await context.bot.send_message(
|
await update.effective_message.reply_text(
|
||||||
chat_id=chat_id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
|
||||||
reply_to_message_id=self.get_reply_to_message_id(update) if index == 0 else None,
|
reply_to_message_id=self.get_reply_to_message_id(update) if index == 0 else None,
|
||||||
text=transcript_chunk,
|
text=transcript_chunk,
|
||||||
parse_mode=constants.ParseMode.MARKDOWN
|
parse_mode=constants.ParseMode.MARKDOWN
|
||||||
@@ -354,9 +352,8 @@ class ChatGPTTelegramBot:
|
|||||||
chunks = self.split_into_chunks(transcript_output)
|
chunks = self.split_into_chunks(transcript_output)
|
||||||
|
|
||||||
for index, transcript_chunk in enumerate(chunks):
|
for index, transcript_chunk in enumerate(chunks):
|
||||||
await context.bot.send_message(
|
await update.effective_message.reply_text(
|
||||||
chat_id=chat_id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
|
||||||
reply_to_message_id=self.get_reply_to_message_id(update) if index == 0 else None,
|
reply_to_message_id=self.get_reply_to_message_id(update) if index == 0 else None,
|
||||||
text=transcript_chunk,
|
text=transcript_chunk,
|
||||||
parse_mode=constants.ParseMode.MARKDOWN
|
parse_mode=constants.ParseMode.MARKDOWN
|
||||||
@@ -364,9 +361,8 @@ class ChatGPTTelegramBot:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception(e)
|
logging.exception(e)
|
||||||
await context.bot.send_message(
|
await update.effective_message.reply_text(
|
||||||
chat_id=chat_id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
|
||||||
reply_to_message_id=self.get_reply_to_message_id(update),
|
reply_to_message_id=self.get_reply_to_message_id(update),
|
||||||
text=f"{localized_text('transcribe_fail', bot_language)}: {str(e)}",
|
text=f"{localized_text('transcribe_fail', bot_language)}: {str(e)}",
|
||||||
parse_mode=constants.ParseMode.MARKDOWN
|
parse_mode=constants.ParseMode.MARKDOWN
|
||||||
@@ -412,7 +408,10 @@ class ChatGPTTelegramBot:
|
|||||||
total_tokens = 0
|
total_tokens = 0
|
||||||
|
|
||||||
if self.config['stream']:
|
if self.config['stream']:
|
||||||
await context.bot.send_chat_action(chat_id=chat_id, action=constants.ChatAction.TYPING)
|
await update.effective_message.reply_chat_action(
|
||||||
|
action=constants.ChatAction.TYPING,
|
||||||
|
message_thread_id=self.get_thread_id(update)
|
||||||
|
)
|
||||||
|
|
||||||
stream_response = self.openai.get_chat_response_stream(chat_id=chat_id, query=prompt)
|
stream_response = self.openai.get_chat_response_stream(chat_id=chat_id, query=prompt)
|
||||||
i = 0
|
i = 0
|
||||||
@@ -436,9 +435,8 @@ class ChatGPTTelegramBot:
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
sent_message = await context.bot.send_message(
|
sent_message = await update.effective_message.reply_text(
|
||||||
chat_id=sent_message.chat_id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
|
||||||
text=content if len(content) > 0 else "..."
|
text=content if len(content) > 0 else "..."
|
||||||
)
|
)
|
||||||
except:
|
except:
|
||||||
@@ -453,9 +451,8 @@ class ChatGPTTelegramBot:
|
|||||||
if sent_message is not None:
|
if sent_message is not None:
|
||||||
await context.bot.delete_message(chat_id=sent_message.chat_id,
|
await context.bot.delete_message(chat_id=sent_message.chat_id,
|
||||||
message_id=sent_message.message_id)
|
message_id=sent_message.message_id)
|
||||||
sent_message = await context.bot.send_message(
|
sent_message = await update.effective_message.reply_text(
|
||||||
chat_id=chat_id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
|
||||||
reply_to_message_id=self.get_reply_to_message_id(update),
|
reply_to_message_id=self.get_reply_to_message_id(update),
|
||||||
text=content
|
text=content
|
||||||
)
|
)
|
||||||
@@ -500,18 +497,16 @@ class ChatGPTTelegramBot:
|
|||||||
|
|
||||||
for index, chunk in enumerate(chunks):
|
for index, chunk in enumerate(chunks):
|
||||||
try:
|
try:
|
||||||
await context.bot.send_message(
|
await update.effective_message.reply_text(
|
||||||
chat_id=chat_id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
|
||||||
reply_to_message_id=self.get_reply_to_message_id(update) if index == 0 else None,
|
reply_to_message_id=self.get_reply_to_message_id(update) if index == 0 else None,
|
||||||
text=chunk,
|
text=chunk,
|
||||||
parse_mode=constants.ParseMode.MARKDOWN
|
parse_mode=constants.ParseMode.MARKDOWN
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
try:
|
try:
|
||||||
await context.bot.send_message(
|
await update.effective_message.reply_text(
|
||||||
chat_id=chat_id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
|
||||||
reply_to_message_id=self.get_reply_to_message_id(update) if index == 0 else None,
|
reply_to_message_id=self.get_reply_to_message_id(update) if index == 0 else None,
|
||||||
text=chunk
|
text=chunk
|
||||||
)
|
)
|
||||||
@@ -524,9 +519,8 @@ class ChatGPTTelegramBot:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception(e)
|
logging.exception(e)
|
||||||
await context.bot.send_message(
|
await update.effective_message.reply_text(
|
||||||
chat_id=chat_id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
|
||||||
reply_to_message_id=self.get_reply_to_message_id(update),
|
reply_to_message_id=self.get_reply_to_message_id(update),
|
||||||
text=f"{localized_text('chat_fail', self.config['bot_language'])} {str(e)}",
|
text=f"{localized_text('chat_fail', self.config['bot_language'])} {str(e)}",
|
||||||
parse_mode=constants.ParseMode.MARKDOWN
|
parse_mode=constants.ParseMode.MARKDOWN
|
||||||
@@ -716,7 +710,8 @@ class ChatGPTTelegramBot:
|
|||||||
try:
|
try:
|
||||||
await context.bot.edit_message_text(
|
await context.bot.edit_message_text(
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
message_id=message_id,
|
message_id=int(message_id) if not is_inline else None,
|
||||||
|
inline_message_id=message_id if is_inline else None,
|
||||||
text=text
|
text=text
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -735,7 +730,9 @@ class ChatGPTTelegramBot:
|
|||||||
task = context.application.create_task(coroutine(), update=update)
|
task = context.application.create_task(coroutine(), update=update)
|
||||||
while not task.done():
|
while not task.done():
|
||||||
if not is_inline:
|
if not is_inline:
|
||||||
context.application.create_task(update.effective_chat.send_action(chat_action))
|
context.application.create_task(
|
||||||
|
update.effective_chat.send_action(chat_action, message_thread_id=self.get_thread_id(update))
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
await asyncio.wait_for(asyncio.shield(task), 4.5)
|
await asyncio.wait_for(asyncio.shield(task), 4.5)
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
@@ -746,9 +743,8 @@ class ChatGPTTelegramBot:
|
|||||||
Sends the disallowed message to the user.
|
Sends the disallowed message to the user.
|
||||||
"""
|
"""
|
||||||
if not is_inline:
|
if not is_inline:
|
||||||
await context.bot.send_message(
|
await update.effective_message.reply_text(
|
||||||
chat_id=update.effective_chat.id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
|
||||||
text=self.disallowed_message,
|
text=self.disallowed_message,
|
||||||
disable_web_page_preview=True
|
disable_web_page_preview=True
|
||||||
)
|
)
|
||||||
@@ -761,9 +757,8 @@ class ChatGPTTelegramBot:
|
|||||||
Sends the budget reached message to the user.
|
Sends the budget reached message to the user.
|
||||||
"""
|
"""
|
||||||
if not is_inline:
|
if not is_inline:
|
||||||
await context.bot.send_message(
|
await update.effective_message.reply_text(
|
||||||
chat_id=update.effective_chat.id,
|
message_thread_id=self.get_thread_id(update),
|
||||||
message_thread_id=update.effective_message.message_thread_id,
|
|
||||||
text=self.budget_limit_message
|
text=self.budget_limit_message
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
@@ -776,6 +771,14 @@ class ChatGPTTelegramBot:
|
|||||||
"""
|
"""
|
||||||
logging.error(f'Exception while handling an update: {context.error}')
|
logging.error(f'Exception while handling an update: {context.error}')
|
||||||
|
|
||||||
|
def get_thread_id(self, update: Update) -> int | None:
|
||||||
|
"""
|
||||||
|
Gets the message thread id for the update, if any
|
||||||
|
"""
|
||||||
|
if update.effective_message and update.effective_message.is_topic_message:
|
||||||
|
return update.effective_message.message_thread_id
|
||||||
|
return None
|
||||||
|
|
||||||
def get_stream_cutoff_values(self, update: Update, content: str) -> int:
|
def get_stream_cutoff_values(self, update: Update, content: str) -> int:
|
||||||
"""
|
"""
|
||||||
Gets the stream cutoff values for the message length
|
Gets the stream cutoff values for the message length
|
||||||
|
|||||||
Reference in New Issue
Block a user