n3d1117 / chatgpt-telegram-bot

🤖 A Telegram bot that integrates with OpenAI's official ChatGPT APIs to provide answers, written in Python
GNU General Public License v2.0
2.99k stars 1.37k forks source link

`sent_message` is always None, it seems that the check should be skipped here? #336

Closed hsiaoping-nthu closed 1 year ago

hsiaoping-nthu commented 1 year ago

Hello, I trace the code and find some that seem a bit odd.

In line 603 of file bot/telegram_bot.py(line 16 in following code section), I think sent_message is always None because it does only initialize but not update. Maybe it can skip i==0 case? Thanks for your response. :relaxed:

1. if self.config['stream']:
2.     stream_response = self.openai.get_chat_response_stream(chat_id=user_id, query=query)
3.     i = 0
4.     prev = ''
5.     sent_message = None
6.     backoff = 0
7.     async for content, tokens in stream_response:
8.         if len(content.strip()) == 0:
9.             continue
10.   
11.         cutoff = get_stream_cutoff_values(update, content)
12.         cutoff += backoff
13.   
14.         if i == 0:
15.             try:
16.                 if sent_message is not None:
17.                     await edit_message_with_retry(context, chat_id=None,
18.                                                   message_id=inline_message_id,
19.                                                   text=f'{query}\n\n{answer_tr}:\n{content}',
20.                                                   is_inline=True)
21.             except:
22.                 continue
n3d1117 commented 1 year ago

Hey @hsiaoping-nthu, good catch and thanks for reporting! Looks we can remove these two lines safely:

https://github.com/n3d1117/chatgpt-telegram-bot/blob/1433207416578c7473546c106155f07158b7eb54/bot/telegram_bot.py#L592

and

https://github.com/n3d1117/chatgpt-telegram-bot/blob/1433207416578c7473546c106155f07158b7eb54/bot/telegram_bot.py#L603

Could you maybe test it and open a PR if everything looks ok? EDIT: done in https://github.com/n3d1117/chatgpt-telegram-bot/commit/fdd76ee8458b73add40f78d305596b204cc25546