Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
n3d1117 committed Mar 27, 2023
1 parent bc6a4e4 commit e4ea02a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
4 changes: 2 additions & 2 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dotenv import load_dotenv

from openai_helper import OpenAIHelper
from telegram_bot import ChatGPT3TelegramBot
from telegram_bot import ChatGPTTelegramBot


def main():
Expand Down Expand Up @@ -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()


Expand Down
62 changes: 44 additions & 18 deletions bot/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
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):
Expand Down Expand Up @@ -343,9 +343,10 @@ async def prompt(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
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 "..."
Expand Down Expand Up @@ -379,27 +380,19 @@ async def prompt(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
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

Expand Down Expand Up @@ -462,6 +455,39 @@ async def inline_query(self, update: Update, context: ContextTypes.DEFAULT_TYPE)

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.
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ services:
dockerfile: Dockerfile
volumes:
- .:/app
restart: always
restart: unless-stopped

0 comments on commit e4ea02a

Please sign in to comment.