mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2026-01-08 23:46:00 +01:00
Merge pull request #76 from n3d1117/feature/handle-responses-longer-than-telegram-message-limit
Handle responses longer than telegram message limit
This commit is contained in:
@@ -28,7 +28,6 @@ A [Telegram bot](https://core.telegram.org/bots/api) that integrates with OpenAI
|
||||
|
||||
## Additional features - help needed!
|
||||
- [ ] Add stream support ([#43](https://github.com/n3d1117/chatgpt-telegram-bot/issues/43))
|
||||
- [ ] Handle responses longer than telegram message limit ([#44](https://github.com/n3d1117/chatgpt-telegram-bot/issues/44))
|
||||
|
||||
PRs are always welcome!
|
||||
|
||||
|
||||
104
telegram_bot.py
104
telegram_bot.py
@@ -159,7 +159,7 @@ class ChatGPT3TelegramBot:
|
||||
|
||||
if self.is_group_chat(update) and self.config['ignore_group_transcriptions']:
|
||||
logging.info(f'Transcription coming from group chat, ignoring...')
|
||||
return
|
||||
return
|
||||
|
||||
chat_id = update.effective_chat.id
|
||||
await context.bot.send_chat_action(chat_id=chat_id, action=constants.ChatAction.TYPING)
|
||||
@@ -167,8 +167,17 @@ class ChatGPT3TelegramBot:
|
||||
filename = update.message.effective_attachment.file_unique_id
|
||||
filename_mp3 = f'{filename}.mp3'
|
||||
|
||||
media_file = await context.bot.get_file(update.message.effective_attachment.file_id)
|
||||
await media_file.download_to_drive(filename)
|
||||
try:
|
||||
media_file = await context.bot.get_file(update.message.effective_attachment.file_id)
|
||||
await media_file.download_to_drive(filename)
|
||||
except Exception as e:
|
||||
logging.exception(e)
|
||||
await context.bot.send_message(
|
||||
chat_id=chat_id,
|
||||
reply_to_message_id=update.message.message_id,
|
||||
text=f'Failed to download audio file: {str(e)}. Make sure the file is not too large. (max 20MB)'
|
||||
)
|
||||
return
|
||||
|
||||
# detect and extract audio from the attachment with pydub
|
||||
try:
|
||||
@@ -177,7 +186,7 @@ class ChatGPT3TelegramBot:
|
||||
logging.info(f'New transcribe request received from user {update.message.from_user.name}')
|
||||
|
||||
except Exception as e:
|
||||
logging.info(f'Unsupported file type recceived from {update.message.from_user.name}')
|
||||
logging.exception(e)
|
||||
await context.bot.send_message(
|
||||
chat_id=update.effective_chat.id,
|
||||
reply_to_message_id=update.message.message_id,
|
||||
@@ -200,23 +209,34 @@ class ChatGPT3TelegramBot:
|
||||
transcript = self.openai.transcribe(filename_mp3)
|
||||
|
||||
# add transcription seconds to usage tracker
|
||||
self.usage[user_id].add_transcription_seconds(audio_track.duration_seconds, self.config['transcription_price'])
|
||||
transcription_price = self.config['transcription_price']
|
||||
self.usage[user_id].add_transcription_seconds(audio_track.duration_seconds, transcription_price)
|
||||
|
||||
# add guest chat request to guest usage tracker
|
||||
allowed_user_ids = self.config['allowed_user_ids'].split(',')
|
||||
if str(user_id) not in allowed_user_ids:
|
||||
self.usage["guests"].add_transcription_seconds(audio_track.duration_seconds, self.config['transcription_price'])
|
||||
self.usage["guests"].add_transcription_seconds(audio_track.duration_seconds, transcription_price)
|
||||
|
||||
if self.config['voice_reply_transcript']:
|
||||
# Send the transcript
|
||||
await context.bot.send_message(
|
||||
chat_id=chat_id,
|
||||
reply_to_message_id=update.message.message_id,
|
||||
text=f'_Transcript:_\n"{transcript}"',
|
||||
parse_mode=constants.ParseMode.MARKDOWN
|
||||
)
|
||||
|
||||
# Split into chunks of 4096 characters (Telegram's message limit)
|
||||
transcript_output = f'_Transcript:_\n"{transcript}"'
|
||||
chunks = self.split_into_chunks(transcript_output)
|
||||
|
||||
for index, transcript_chunk in enumerate(chunks):
|
||||
await context.bot.send_message(
|
||||
chat_id=chat_id,
|
||||
reply_to_message_id=update.message.message_id if index == 0 else None,
|
||||
text=transcript_chunk,
|
||||
parse_mode=constants.ParseMode.MARKDOWN
|
||||
)
|
||||
else:
|
||||
# Send the response of the transcript
|
||||
response, total_tokens = self.openai.get_chat_response(chat_id=chat_id, query=transcript)
|
||||
# Get the response of the transcript
|
||||
response = self.openai.get_chat_response(chat_id=chat_id, query=transcript)
|
||||
if not isinstance(response, tuple):
|
||||
raise Exception(response)
|
||||
|
||||
response, total_tokens = response
|
||||
|
||||
# add chat request to users usage tracker
|
||||
self.usage[user_id].add_chat_tokens(total_tokens, self.config['token_price'])
|
||||
@@ -224,12 +244,17 @@ class ChatGPT3TelegramBot:
|
||||
if str(user_id) not in allowed_user_ids:
|
||||
self.usage["guests"].add_chat_tokens(total_tokens, self.config['token_price'])
|
||||
|
||||
await context.bot.send_message(
|
||||
chat_id=chat_id,
|
||||
reply_to_message_id=update.message.message_id,
|
||||
text=f'_Transcript:_\n"{transcript}"\n\n_Answer:_\n{response}',
|
||||
parse_mode=constants.ParseMode.MARKDOWN
|
||||
)
|
||||
# Split into chunks of 4096 characters (Telegram's message limit)
|
||||
transcript_output = f'_Transcript:_\n"{transcript}"\n\n_Answer:_\n{response}'
|
||||
chunks = self.split_into_chunks(transcript_output)
|
||||
|
||||
for index, transcript_chunk in enumerate(chunks):
|
||||
await context.bot.send_message(
|
||||
chat_id=chat_id,
|
||||
reply_to_message_id=update.message.message_id if index == 0 else None,
|
||||
text=transcript_chunk,
|
||||
parse_mode=constants.ParseMode.MARKDOWN
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logging.exception(e)
|
||||
@@ -269,11 +294,22 @@ class ChatGPT3TelegramBot:
|
||||
if prompt.startswith(trigger_keyword):
|
||||
prompt = prompt[len(trigger_keyword):].strip()
|
||||
else:
|
||||
logging.warning(f'Message does not start with trigger keyword, ignoring...')
|
||||
logging.warning('Message does not start with trigger keyword, ignoring...')
|
||||
return
|
||||
|
||||
await context.bot.send_chat_action(chat_id=chat_id, action=constants.ChatAction.TYPING)
|
||||
response, total_tokens = self.openai.get_chat_response(chat_id=chat_id, query=prompt)
|
||||
|
||||
response = self.openai.get_chat_response(chat_id=chat_id, query=prompt)
|
||||
if not isinstance(response, tuple):
|
||||
await context.bot.send_message(
|
||||
chat_id=chat_id,
|
||||
reply_to_message_id=update.message.message_id,
|
||||
text=response,
|
||||
parse_mode=constants.ParseMode.MARKDOWN
|
||||
)
|
||||
return
|
||||
|
||||
response, total_tokens = response
|
||||
|
||||
# add chat request to users usage tracker
|
||||
self.usage[user_id].add_chat_tokens(total_tokens, self.config['token_price'])
|
||||
@@ -282,12 +318,16 @@ class ChatGPT3TelegramBot:
|
||||
if str(user_id) not in allowed_user_ids:
|
||||
self.usage["guests"].add_chat_tokens(total_tokens, self.config['token_price'])
|
||||
|
||||
await context.bot.send_message(
|
||||
chat_id=chat_id,
|
||||
reply_to_message_id=update.message.message_id,
|
||||
text=response,
|
||||
parse_mode=constants.ParseMode.MARKDOWN
|
||||
)
|
||||
# Split into chunks of 4096 characters (Telegram's message limit)
|
||||
chunks = self.split_into_chunks(response)
|
||||
|
||||
for index, chunk in enumerate(chunks):
|
||||
await context.bot.send_message(
|
||||
chat_id=chat_id,
|
||||
reply_to_message_id=update.message.message_id if index == 0 else None,
|
||||
text=chunk,
|
||||
parse_mode=constants.ParseMode.MARKDOWN
|
||||
)
|
||||
|
||||
async def inline_query(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
"""
|
||||
@@ -417,6 +457,12 @@ class ChatGPT3TelegramBot:
|
||||
logging.info(f'Group chat messages from user {update.message.from_user.name} are not allowed')
|
||||
return False
|
||||
|
||||
def split_into_chunks(self, text: str, chunk_size: int = 4096) -> list[str]:
|
||||
"""
|
||||
Splits a string into chunks of a given size.
|
||||
"""
|
||||
return [text[i:i + chunk_size] for i in range(0, len(text), chunk_size)]
|
||||
|
||||
async def post_init(self, application: Application) -> None:
|
||||
"""
|
||||
Post initialization hook for the bot.
|
||||
|
||||
Reference in New Issue
Block a user