mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2025-12-18 13:14:59 +01:00
minor improvements
This commit is contained in:
@@ -4,7 +4,7 @@ import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from openai_helper import OpenAIHelper
|
||||
from telegram_bot import ChatGPT3TelegramBot
|
||||
from telegram_bot import ChatGPTTelegramBot
|
||||
|
||||
|
||||
def main():
|
||||
@@ -60,7 +60,7 @@ def main():
|
||||
|
||||
# Setup and run ChatGPT and Telegram bot
|
||||
openai_helper = OpenAIHelper(config=openai_config)
|
||||
telegram_bot = ChatGPT3TelegramBot(config=telegram_config, openai=openai_helper)
|
||||
telegram_bot = ChatGPTTelegramBot(config=telegram_config, openai=openai_helper)
|
||||
telegram_bot.run()
|
||||
|
||||
|
||||
|
||||
@@ -15,9 +15,9 @@ from openai_helper import OpenAIHelper
|
||||
from usage_tracker import UsageTracker
|
||||
|
||||
|
||||
class ChatGPT3TelegramBot:
|
||||
class ChatGPTTelegramBot:
|
||||
"""
|
||||
Class representing a Chat-GPT3 Telegram Bot.
|
||||
Class representing a ChatGPT Telegram Bot.
|
||||
"""
|
||||
|
||||
def __init__(self, config: dict, openai: OpenAIHelper):
|
||||
@@ -343,9 +343,10 @@ class ChatGPT3TelegramBot:
|
||||
if chunk != len(chunks) - 1:
|
||||
chunk += 1
|
||||
try:
|
||||
await context.bot.edit_message_text(chunks[-2], chat_id=sent_message.chat_id,
|
||||
message_id=sent_message.message_id,
|
||||
parse_mode=constants.ParseMode.MARKDOWN)
|
||||
await self.edit_message_with_retry(context, chat_id, sent_message.message_id, chunks[-2])
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
sent_message = await context.bot.send_message(
|
||||
chat_id=sent_message.chat_id,
|
||||
text=content if len(content) > 0 else "..."
|
||||
@@ -379,27 +380,19 @@ class ChatGPT3TelegramBot:
|
||||
prev = content
|
||||
|
||||
try:
|
||||
await context.bot.edit_message_text(content, chat_id=sent_message.chat_id,
|
||||
message_id=sent_message.message_id,
|
||||
parse_mode=constants.ParseMode.MARKDOWN)
|
||||
except telegram.error.BadRequest as e:
|
||||
if str(e).startswith("Message is not modified"):
|
||||
continue
|
||||
await context.bot.edit_message_text(content, chat_id=sent_message.chat_id,
|
||||
message_id=sent_message.message_id)
|
||||
await self.edit_message_with_retry(context, chat_id, sent_message.message_id, content)
|
||||
|
||||
except RetryAfter as e:
|
||||
logging.warning(str(e))
|
||||
backoff += 5
|
||||
await asyncio.sleep(e.retry_after)
|
||||
continue
|
||||
|
||||
except TimedOut as e:
|
||||
logging.warning(str(e))
|
||||
except TimedOut:
|
||||
backoff += 5
|
||||
await asyncio.sleep(0.5)
|
||||
continue
|
||||
|
||||
except Exception as e:
|
||||
logging.warning(str(e))
|
||||
except Exception:
|
||||
backoff += 5
|
||||
continue
|
||||
|
||||
@@ -462,6 +455,39 @@ class ChatGPT3TelegramBot:
|
||||
|
||||
await update.inline_query.answer(results)
|
||||
|
||||
async def edit_message_with_retry(self, context: ContextTypes.DEFAULT_TYPE, chat_id: int, message_id: int, text: str):
|
||||
"""
|
||||
Edit a message with retry logic in case of failure (e.g. broken markdown)
|
||||
:param context: The context to use
|
||||
:param chat_id: The chat id to edit the message in
|
||||
:param message_id: The message id to edit
|
||||
:param text: The text to edit the message with
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
await context.bot.edit_message_text(
|
||||
chat_id=chat_id,
|
||||
message_id=message_id,
|
||||
text=text,
|
||||
parse_mode=constants.ParseMode.MARKDOWN
|
||||
)
|
||||
except telegram.error.BadRequest as e:
|
||||
if str(e).startswith("Message is not modified"):
|
||||
return
|
||||
try:
|
||||
await context.bot.edit_message_text(
|
||||
chat_id=chat_id,
|
||||
message_id=message_id,
|
||||
text=text
|
||||
)
|
||||
except Exception as e:
|
||||
logging.warning(f'Failed to edit message: {str(e)}')
|
||||
raise e
|
||||
|
||||
except Exception as e:
|
||||
logging.warning(str(e))
|
||||
raise e
|
||||
|
||||
async def send_disallowed_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
"""
|
||||
Sends the disallowed message to the user.
|
||||
|
||||
@@ -6,4 +6,4 @@ services:
|
||||
dockerfile: Dockerfile
|
||||
volumes:
|
||||
- .:/app
|
||||
restart: always
|
||||
restart: unless-stopped
|
||||
|
||||
Reference in New Issue
Block a user