mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2025-12-20 06:05:12 +01:00
add option to disable quoting in private chats
This commit is contained in:
@@ -28,7 +28,7 @@ A [Telegram bot](https://core.telegram.org/bots/api) that integrates with OpenAI
|
|||||||
- If you have access to the GPT-4 API, simply change the `OPENAI_MODEL` parameter to `gpt-4`
|
- If you have access to the GPT-4 API, simply change the `OPENAI_MODEL` parameter to `gpt-4`
|
||||||
|
|
||||||
## Additional features - help needed!
|
## Additional features - help needed!
|
||||||
- [ ] Add session persistence ([#70](https://github.com/n3d1117/chatgpt-telegram-bot/issues/70), [#71](https://github.com/n3d1117/chatgpt-telegram-bot/issues/71))
|
If you'd like to help, check out the [issues](https://github.com/n3d1117/chatgpt-telegram-bot/issues) section and contribute!
|
||||||
|
|
||||||
PRs are always welcome!
|
PRs are always welcome!
|
||||||
|
|
||||||
@@ -52,6 +52,7 @@ Customize the configuration by copying `.env.example` and renaming it to `.env`,
|
|||||||
### Optional configuration
|
### Optional configuration
|
||||||
| Parameter | Description | Default value |
|
| Parameter | Description | Default value |
|
||||||
|------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------|
|
|------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------|
|
||||||
|
| `ENABLE_QUOTING` | Whether to enable message quoting in private chats | true |
|
||||||
| `ENABLE_IMAGE_GENERATION` | Whether to enable image generation via the `/image` command | true |
|
| `ENABLE_IMAGE_GENERATION` | Whether to enable image generation via the `/image` command | true |
|
||||||
| `ENABLE_TRANSCRIPTION` | Whether to enable transcriptions of audio and video messages | true |
|
| `ENABLE_TRANSCRIPTION` | Whether to enable transcriptions of audio and video messages | true |
|
||||||
| `MONTHLY_USER_BUDGETS` | A comma-separated list of $-amounts per user from list `ALLOWED_TELEGRAM_USER_IDS` to set custom usage limit of OpenAI API costs for each. **Note**: by default, *no limits* for anyone (`*`) | `*` |
|
| `MONTHLY_USER_BUDGETS` | A comma-separated list of $-amounts per user from list `ALLOWED_TELEGRAM_USER_IDS` to set custom usage limit of OpenAI API costs for each. **Note**: by default, *no limits* for anyone (`*`) | `*` |
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ def main():
|
|||||||
'token': os.environ['TELEGRAM_BOT_TOKEN'],
|
'token': os.environ['TELEGRAM_BOT_TOKEN'],
|
||||||
'admin_user_ids': os.environ.get('ADMIN_USER_IDS', '-'),
|
'admin_user_ids': os.environ.get('ADMIN_USER_IDS', '-'),
|
||||||
'allowed_user_ids': os.environ.get('ALLOWED_TELEGRAM_USER_IDS', '*'),
|
'allowed_user_ids': os.environ.get('ALLOWED_TELEGRAM_USER_IDS', '*'),
|
||||||
|
'enable_quoting': os.environ.get('ENABLE_QUOTING', 'true').lower() == 'true',
|
||||||
'enable_image_generation': os.environ.get('ENABLE_IMAGE_GENERATION', 'true').lower() == 'true',
|
'enable_image_generation': os.environ.get('ENABLE_IMAGE_GENERATION', 'true').lower() == 'true',
|
||||||
'enable_transcription': os.environ.get('ENABLE_TRANSCRIPTION', 'true').lower() == 'true',
|
'enable_transcription': os.environ.get('ENABLE_TRANSCRIPTION', 'true').lower() == 'true',
|
||||||
'monthly_user_budgets': os.environ.get('MONTHLY_USER_BUDGETS', '*'),
|
'monthly_user_budgets': os.environ.get('MONTHLY_USER_BUDGETS', '*'),
|
||||||
|
|||||||
@@ -187,7 +187,7 @@ class ChatGPTTelegramBot:
|
|||||||
image_url, image_size = await self.openai.generate_image(prompt=image_query)
|
image_url, image_size = await self.openai.generate_image(prompt=image_query)
|
||||||
await context.bot.send_photo(
|
await context.bot.send_photo(
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
reply_to_message_id=update.message.message_id,
|
reply_to_message_id=self.get_reply_to_message_id(update),
|
||||||
photo=image_url
|
photo=image_url
|
||||||
)
|
)
|
||||||
# add image request to users usage tracker
|
# add image request to users usage tracker
|
||||||
@@ -201,7 +201,7 @@ class ChatGPTTelegramBot:
|
|||||||
logging.exception(e)
|
logging.exception(e)
|
||||||
await context.bot.send_message(
|
await context.bot.send_message(
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
reply_to_message_id=update.message.message_id,
|
reply_to_message_id=self.get_reply_to_message_id(update),
|
||||||
text=f'Failed to generate image: {str(e)}',
|
text=f'Failed to generate image: {str(e)}',
|
||||||
parse_mode=constants.ParseMode.MARKDOWN
|
parse_mode=constants.ParseMode.MARKDOWN
|
||||||
)
|
)
|
||||||
@@ -232,7 +232,7 @@ class ChatGPTTelegramBot:
|
|||||||
logging.exception(e)
|
logging.exception(e)
|
||||||
await context.bot.send_message(
|
await context.bot.send_message(
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
reply_to_message_id=update.message.message_id,
|
reply_to_message_id=self.get_reply_to_message_id(update),
|
||||||
text=f'Failed to download audio file: {str(e)}. Make sure the file is not too large. (max 20MB)',
|
text=f'Failed to download audio file: {str(e)}. Make sure the file is not too large. (max 20MB)',
|
||||||
parse_mode=constants.ParseMode.MARKDOWN
|
parse_mode=constants.ParseMode.MARKDOWN
|
||||||
)
|
)
|
||||||
@@ -249,7 +249,7 @@ class ChatGPTTelegramBot:
|
|||||||
logging.exception(e)
|
logging.exception(e)
|
||||||
await context.bot.send_message(
|
await context.bot.send_message(
|
||||||
chat_id=update.effective_chat.id,
|
chat_id=update.effective_chat.id,
|
||||||
reply_to_message_id=update.message.message_id,
|
reply_to_message_id=self.get_reply_to_message_id(update),
|
||||||
text='Unsupported file type'
|
text='Unsupported file type'
|
||||||
)
|
)
|
||||||
if os.path.exists(filename):
|
if os.path.exists(filename):
|
||||||
@@ -284,7 +284,7 @@ class ChatGPTTelegramBot:
|
|||||||
for index, transcript_chunk in enumerate(chunks):
|
for index, transcript_chunk in enumerate(chunks):
|
||||||
await context.bot.send_message(
|
await context.bot.send_message(
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
reply_to_message_id=update.message.message_id if index == 0 else None,
|
reply_to_message_id=self.get_reply_to_message_id(update) if index == 0 else None,
|
||||||
text=transcript_chunk,
|
text=transcript_chunk,
|
||||||
parse_mode=constants.ParseMode.MARKDOWN
|
parse_mode=constants.ParseMode.MARKDOWN
|
||||||
)
|
)
|
||||||
@@ -305,7 +305,7 @@ class ChatGPTTelegramBot:
|
|||||||
for index, transcript_chunk in enumerate(chunks):
|
for index, transcript_chunk in enumerate(chunks):
|
||||||
await context.bot.send_message(
|
await context.bot.send_message(
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
reply_to_message_id=update.message.message_id if index == 0 else None,
|
reply_to_message_id=self.get_reply_to_message_id(update) if index == 0 else None,
|
||||||
text=transcript_chunk,
|
text=transcript_chunk,
|
||||||
parse_mode=constants.ParseMode.MARKDOWN
|
parse_mode=constants.ParseMode.MARKDOWN
|
||||||
)
|
)
|
||||||
@@ -314,7 +314,7 @@ class ChatGPTTelegramBot:
|
|||||||
logging.exception(e)
|
logging.exception(e)
|
||||||
await context.bot.send_message(
|
await context.bot.send_message(
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
reply_to_message_id=update.message.message_id,
|
reply_to_message_id=self.get_reply_to_message_id(update),
|
||||||
text=f'Failed to transcribe text: {str(e)}',
|
text=f'Failed to transcribe text: {str(e)}',
|
||||||
parse_mode=constants.ParseMode.MARKDOWN
|
parse_mode=constants.ParseMode.MARKDOWN
|
||||||
)
|
)
|
||||||
@@ -400,7 +400,7 @@ class ChatGPTTelegramBot:
|
|||||||
message_id=sent_message.message_id)
|
message_id=sent_message.message_id)
|
||||||
sent_message = await context.bot.send_message(
|
sent_message = await context.bot.send_message(
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
reply_to_message_id=update.message.message_id,
|
reply_to_message_id=self.get_reply_to_message_id(update),
|
||||||
text=content
|
text=content
|
||||||
)
|
)
|
||||||
except:
|
except:
|
||||||
@@ -445,7 +445,7 @@ class ChatGPTTelegramBot:
|
|||||||
try:
|
try:
|
||||||
await context.bot.send_message(
|
await context.bot.send_message(
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
reply_to_message_id=update.message.message_id if index == 0 else None,
|
reply_to_message_id=self.get_reply_to_message_id(update) if index == 0 else None,
|
||||||
text=chunk,
|
text=chunk,
|
||||||
parse_mode=constants.ParseMode.MARKDOWN
|
parse_mode=constants.ParseMode.MARKDOWN
|
||||||
)
|
)
|
||||||
@@ -453,7 +453,7 @@ class ChatGPTTelegramBot:
|
|||||||
try:
|
try:
|
||||||
await context.bot.send_message(
|
await context.bot.send_message(
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
reply_to_message_id=update.message.message_id if index == 0 else None,
|
reply_to_message_id=self.get_reply_to_message_id(update) if index == 0 else None,
|
||||||
text=chunk
|
text=chunk
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -475,7 +475,7 @@ class ChatGPTTelegramBot:
|
|||||||
logging.exception(e)
|
logging.exception(e)
|
||||||
await context.bot.send_message(
|
await context.bot.send_message(
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
reply_to_message_id=update.message.message_id,
|
reply_to_message_id=self.get_reply_to_message_id(update),
|
||||||
text=f'Failed to get response: {str(e)}',
|
text=f'Failed to get response: {str(e)}',
|
||||||
parse_mode=constants.ParseMode.MARKDOWN
|
parse_mode=constants.ParseMode.MARKDOWN
|
||||||
)
|
)
|
||||||
@@ -733,6 +733,16 @@ class ChatGPTTelegramBot:
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def get_reply_to_message_id(self, update: Update):
|
||||||
|
"""
|
||||||
|
Returns the message id of the message to reply to
|
||||||
|
:param update: Telegram update object
|
||||||
|
:return: Message id of the message to reply to, or None if quoting is disabled
|
||||||
|
"""
|
||||||
|
if self.config['enable_quoting'] or self.is_group_chat(update):
|
||||||
|
return update.message.message_id
|
||||||
|
return None
|
||||||
|
|
||||||
def split_into_chunks(self, text: str, chunk_size: int = 4096) -> list[str]:
|
def split_into_chunks(self, text: str, chunk_size: int = 4096) -> list[str]:
|
||||||
"""
|
"""
|
||||||
Splits a string into chunks of a given size.
|
Splits a string into chunks of a given size.
|
||||||
|
|||||||
Reference in New Issue
Block a user