Skip to content

Commit

Permalink
🐛 Bug: Fixing the bug in the reply where the file is not an image is …
Browse files Browse the repository at this point in the history
…caused by not checking if the image file is None.

✨ Feature: The supported reply message is a file, directly asking and answering the file in the history record.

💻 Code: Upgrade ModelMerge version to 0.7.10
  • Loading branch information
yym68686 committed Jun 25, 2024
1 parent 0f4646f commit 28a7f90
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
36 changes: 22 additions & 14 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ async def GetMesage(update_message, context):
reply_to_message_text = None
message = None
rawtext = None
reply_to_message_file_content = None

chatid = str(update_message.chat_id)
message_thread_id = update_message.message_thread_id
Expand All @@ -117,6 +118,10 @@ async def GetMesage(update_message, context):

if update_message.reply_to_message:
reply_to_message_text = update_message.reply_to_message.text
reply_to_message_file = update_message.reply_to_message.document
if reply_to_message_file:
reply_to_message_file_url = await get_file_url(reply_to_message_file, context)
reply_to_message_file_content = Document_extract(reply_to_message_file_url, reply_to_message_file_url, None)

if update_message.document:
file = update_message.document
Expand All @@ -135,19 +140,19 @@ async def GetMesage(update_message, context):
if update_message.caption:
message = rawtext = CutNICK(update_message.caption, update_message)

return message, rawtext, image_url, chatid, messageid, reply_to_message_text, message_thread_id, convo_id, file_url
return message, rawtext, image_url, chatid, messageid, reply_to_message_text, message_thread_id, convo_id, file_url, reply_to_message_file_content

async def GetMesageInfo(update, context):
if update.edited_message:
message, rawtext, image_url, chatid, messageid, reply_to_message_text, message_thread_id, convo_id, file_url = await GetMesage(update.edited_message, context)
message, rawtext, image_url, chatid, messageid, reply_to_message_text, message_thread_id, convo_id, file_url, reply_to_message_file_content = await GetMesage(update.edited_message, context)
update_message = update.edited_message
elif update.callback_query:
message, rawtext, image_url, chatid, messageid, reply_to_message_text, message_thread_id, convo_id, file_url = await GetMesage(update.callback_query.message, context)
message, rawtext, image_url, chatid, messageid, reply_to_message_text, message_thread_id, convo_id, file_url, reply_to_message_file_content = await GetMesage(update.callback_query.message, context)
update_message = update.callback_query.message
else:
message, rawtext, image_url, chatid, messageid, reply_to_message_text, message_thread_id, convo_id, file_url = await GetMesage(update.message, context)
message, rawtext, image_url, chatid, messageid, reply_to_message_text, message_thread_id, convo_id, file_url, reply_to_message_file_content = await GetMesage(update.message, context)
update_message = update.message
return message, rawtext, image_url, chatid, messageid, reply_to_message_text, update_message, message_thread_id, convo_id, file_url
return message, rawtext, image_url, chatid, messageid, reply_to_message_text, update_message, message_thread_id, convo_id, file_url, reply_to_message_file_content

# 定义一个缓存来存储消息
message_cache = defaultdict(lambda: [])
Expand All @@ -158,7 +163,7 @@ async def GetMesageInfo(update, context):
async def command_bot(update, context, language=None, prompt=translator_prompt, title="", has_command=True):
stop_event.clear()
print("update", update)
message, rawtext, image_url, chatid, messageid, reply_to_message_text, update_message, message_thread_id, convo_id, file_url = await GetMesageInfo(update, context)
message, rawtext, image_url, chatid, messageid, reply_to_message_text, update_message, message_thread_id, convo_id, file_url, reply_to_message_file_content = await GetMesageInfo(update, context)
print("\033[32m", update.effective_user.username, update.effective_user.id, rawtext, "\033[0m")

if has_command == False or len(context.args) > 0:
Expand All @@ -174,10 +179,13 @@ async def command_bot(update, context, language=None, prompt=translator_prompt,
pass_history = False
message = prompt + message
if message:
if reply_to_message_text and update_message.reply_to_message.from_user.is_bot:
if reply_to_message_text and update_message.reply_to_message.from_user.is_bot and Users.get_config(convo_id, "TITLE") == True:
message = '\n'.join(reply_to_message_text.split('\n')[1:]) + "\n" + message
elif reply_to_message_text and not update_message.reply_to_message.from_user.is_bot:
message = reply_to_message_text + "\n" + message
elif not update_message.reply_to_message.from_user.is_bot:
if reply_to_message_text:
message = reply_to_message_text + "\n" + message
if reply_to_message_file_content:
message = reply_to_message_file_content + "\n" + message

robot, role = get_robot(convo_id)
if robot is None:
Expand Down Expand Up @@ -334,7 +342,7 @@ async def getChatGPT(update, context, title, robot, message, chatid, messageid,
@decorators.Authorization
async def button_press(update, context):
"""Function to handle the button press"""
message, rawtext, image_url, chatid, messageid, reply_to_message_text, update_message, message_thread_id, convo_id, file_url = await GetMesageInfo(update, context)
message, rawtext, image_url, chatid, messageid, reply_to_message_text, update_message, message_thread_id, convo_id, file_url, reply_to_message_file_content = await GetMesageInfo(update, context)
callback_query = update.callback_query
info_message = update_info_message(convo_id)
await callback_query.answer()
Expand Down Expand Up @@ -447,15 +455,15 @@ async def button_press(update, context):
@decorators.GroupAuthorization
@decorators.Authorization
async def info(update, context):
_, _, _, chatid, _, _, _, message_thread_id, convo_id, file_url = await GetMesageInfo(update, context)
_, _, _, chatid, _, _, _, message_thread_id, convo_id, file_url, reply_to_message_file_content = await GetMesageInfo(update, context)
info_message = update_info_message(convo_id)
message = await context.bot.send_message(chat_id=chatid, message_thread_id=message_thread_id, text=escape(info_message), reply_markup=InlineKeyboardMarkup(update_first_buttons_message(convo_id)), parse_mode='MarkdownV2', disable_web_page_preview=True)
await delete_message(update, context, message.message_id)

@decorators.GroupAuthorization
@decorators.Authorization
async def handle_file(update, context):
_, _, image_url, chatid, _, messageid, update_message, message_thread_id, convo_id, file_url = await GetMesageInfo(update, context)
_, _, image_url, chatid, _, messageid, update_message, message_thread_id, convo_id, file_url, reply_to_message_file_content = await GetMesageInfo(update, context)
robot, role = get_robot(convo_id)
engine = get_ENGINE(chatid)

Expand Down Expand Up @@ -513,7 +521,7 @@ async def inlinequery(update: Update, context) -> None:
@decorators.Authorization
async def reset_chat(update, context):
global target_convo_id
_, _, _, chatid, _, _, _, message_thread_id, convo_id, file_url = await GetMesageInfo(update, context)
_, _, _, chatid, _, _, _, message_thread_id, convo_id, file_url, reply_to_message_file_content = await GetMesageInfo(update, context)
target_convo_id = convo_id
stop_event.set()
message = None
Expand All @@ -532,7 +540,7 @@ async def reset_chat(update, context):

async def start(update, context): # 当用户输入/start时,返回文本
user = update.effective_user
_, _, _, _, _, _, _, _, convo_id, _ = await GetMesageInfo(update, context)
_, _, _, _, _, _, _, _, convo_id, _, reply_to_message_file_content = await GetMesageInfo(update, context)
message = (
f"Hi `{user.username}` ! I am an Assistant, a large language model trained by OpenAI. I will do my best to help answer your questions.\n\n"
# "我是人见人爱的 ChatGPT~\n\n"
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--index-url https://pypi.python.org/simple/
python-dotenv
md2tgmd==0.2.3
ModelMerge==0.7.9
ModelMerge==0.7.10
python-telegram-bot[webhooks,rate-limiter]==21.0.1

0 comments on commit 28a7f90

Please sign in to comment.