Skip to content

Commit

Permalink
support async processing for multiple users
Browse files Browse the repository at this point in the history
  • Loading branch information
Biunovich committed Apr 17, 2024
1 parent e6a5188 commit 752a436
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
22 changes: 19 additions & 3 deletions Bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@
openai_client = openai.AsyncOpenAI(api_key=openai_api_key, timeout=30)


def append_message(messages, role, message):
def append_message(messages: list, role: str, message: str):
messages.append({"role": role, "content": message})


async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
async def process_message(context: ContextTypes.DEFAULT_TYPE):
update: Update = context.job.data
user_id = str(context.job.user_id)
name = update.message.from_user.first_name
message = update.message.text
user_id = str(update.message.from_user.id)

messages = context.user_data.setdefault("messages", [])
if (len(messages) == 0):
Expand Down Expand Up @@ -71,6 +72,21 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
logger.error(repr(e))
context.user_data["messages"].pop()
await update.message.reply_text("Some error happened, please try to send message again")
finally:
context.user_data["processing"] = False


async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.message.from_user.id
try:
if context.user_data.setdefault("processing", False):
await update.message.reply_text("The bot has not yet answered the previous message, please try to send a message again after the bot replies")
return
context.user_data["processing"] = True
context.job_queue.run_custom(process_message, {}, data=update, name=str(user_id), user_id=user_id)
except Exception as e:
logger.error(repr(e))
await update.message.reply_text("Some error happened, please try to send message again")


async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
aiolimiter==1.1.0
annotated-types==0.6.0
anyio==4.3.0
APScheduler==3.10.4
certifi==2024.2.2
distro==1.9.0
exceptiongroup==1.2.0
Expand All @@ -13,6 +14,9 @@ pydantic==2.6.4
pydantic_core==2.16.3
python-dotenv==1.0.1
python-telegram-bot==21.0.1
pytz==2024.1
six==1.16.0
sniffio==1.3.1
tqdm==4.66.2
typing_extensions==4.11.0
tzlocal==5.2

0 comments on commit 752a436

Please sign in to comment.