Skip to content

Commit

Permalink
fix bot action typing for non-related uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
brainboost committed Apr 1, 2024
1 parent fd8ba1c commit dbcb784
Showing 1 changed file with 39 additions and 44 deletions.
83 changes: 39 additions & 44 deletions lambda/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,39 +417,49 @@ async def process_voice_message(update: Update, context: ContextTypes.DEFAULT_TY


@send_typing_action
async def process_upload(
update: Update, context: ContextTypes.DEFAULT_TYPE, file_id: str, file_name: str
) -> None:
s3_bucket = read_ssm_param(param_name="BOT_S3_BUCKET")
file = await bot.get_file(file_id)
path = await upload_to_s3(file, s3_bucket, "att", file_name)
logging.info(f"File uploaded {path}")
user_id = int(update.effective_user.id)
config = user_config.read(user_id)
envelop = {
"type": "text",
"user_id": user_id,
"username": update.effective_user.name,
"update_id": update.update_id,
"message_id": update.effective_message.id,
"text": update.message.caption,
"chat_id": update.effective_chat.id,
"timestamp": update.effective_message.date.timestamp(),
"config": config,
"file": path,
}
# logging.info(envelop)
await __send_envelop(envelop, json.dumps(config["engines"]))


async def process_photo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if update.message.photo is None:
return
logging.info("File upload in 'process_photo'")
# logging.info(update.message)
caption = update.message.caption
if bot.name not in caption and "group" in update.message.chat.type:
if bot.name not in update.message.caption and "group" in update.message.chat.type:
return
s3_bucket = read_ssm_param(param_name="BOT_S3_BUCKET")
photo = max(update.message.photo, key=lambda x: x.file_size)
logging.info(photo)
file_id = photo.file_id
logging.info(file_id)
try:
file = await bot.get_file(file_id)
path = await upload_to_s3(file, s3_bucket, "att", f"{photo.file_unique_id}.jpg")
logging.info(f"File uploaded {path}")
user_id = int(update.effective_user.id)
config = user_config.read(user_id)
envelop = {
"type": "text",
"user_id": user_id,
"username": update.effective_user.name,
"update_id": update.update_id,
"message_id": update.effective_message.id,
"text": caption,
"chat_id": update.effective_chat.id,
"timestamp": update.effective_message.date.timestamp(),
"config": config,
"file": path,
}
# logging.info(envelop)
await __send_envelop(envelop, json.dumps(config["engines"]))
await process_upload(
update=update,
context=context,
file_id=file_id,
file_name=f"{photo.file_unique_id}.jpg",
)
except Exception as e:
logging.error(
msg="Exception occured during processing of the picture",
Expand All @@ -462,34 +472,19 @@ async def process_attachment(update: Update, context: ContextTypes.DEFAULT_TYPE)
if update.message is None:
return
logging.info(update.message)
caption = update.message.caption
if bot.name not in caption and "group" in update.message.chat.type:
if bot.name not in update.message.caption and "group" in update.message.chat.type:
return
attachment = update.message.effective_attachment
logging.info(attachment)
file_id = attachment.file_id
logging.info(file_id)
s3_bucket = read_ssm_param(param_name="BOT_S3_BUCKET")
try:
file = await bot.get_file(file_id)
path = await upload_to_s3(file, s3_bucket, "att", attachment.file_name)
logging.info(f"File uploaded {path}")
user_id = int(update.effective_user.id)
config = user_config.read(user_id)
envelop = {
"type": "text",
"user_id": user_id,
"username": update.effective_user.name,
"update_id": update.update_id,
"message_id": update.effective_message.id,
"text": caption,
"chat_id": update.effective_chat.id,
"timestamp": update.effective_message.date.timestamp(),
"config": config,
"file": path,
}
# logging.info(envelop)
await __send_envelop(envelop, json.dumps(config["engines"]))
await process_upload(
update=update,
context=context,
file_id=file_id,
file_name=attachment.file_name,
)
except Exception:
logging.error(
msg="Exception occured during processing of the attachment",
Expand Down

0 comments on commit dbcb784

Please sign in to comment.