mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2025-12-23 23:55:05 +01:00
added chat commands to initiate chat in groups
This commit is contained in:
@@ -5,7 +5,7 @@ import asyncio
|
||||
|
||||
import telegram
|
||||
from uuid import uuid4
|
||||
from telegram import constants
|
||||
from telegram import constants, BotCommandScopeAllGroupChats
|
||||
from telegram import Message, MessageEntity, Update, InlineQueryResultArticle, InputTextMessageContent, BotCommand, ChatMember
|
||||
from telegram.error import RetryAfter, TimedOut
|
||||
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, \
|
||||
@@ -51,6 +51,9 @@ class ChatGPTTelegramBot:
|
||||
BotCommand(command='stats', description='Get your current usage statistics'),
|
||||
BotCommand(command='resend', description='Resend the latest message')
|
||||
]
|
||||
self.group_commands = [
|
||||
BotCommand(command='chat', description='Chat with the bot!')
|
||||
] + self.commands
|
||||
self.disallowed_message = "Sorry, you are not allowed to use this bot. You can check out the source code at " \
|
||||
"https://github.com/n3d1117/chatgpt-telegram-bot"
|
||||
self.budget_limit_message = "Sorry, you have reached your monthly usage limit."
|
||||
@@ -61,10 +64,11 @@ class ChatGPTTelegramBot:
|
||||
"""
|
||||
Shows the help menu.
|
||||
"""
|
||||
commands = [f'/{command.command} - {command.description}' for command in self.commands]
|
||||
commands = self.group_commands if self.is_group_chat(update) else self.commands
|
||||
commands_description = [f'/{command.command} - {command.description}' for command in commands]
|
||||
help_text = 'I\'m a ChatGPT bot, talk to me!' + \
|
||||
'\n\n' + \
|
||||
'\n'.join(commands) + \
|
||||
'\n'.join(commands_description) + \
|
||||
'\n\n' + \
|
||||
'Send me a voice message or file and I\'ll transcribe it for you!' + \
|
||||
'\n\n' + \
|
||||
@@ -338,7 +342,7 @@ class ChatGPTTelegramBot:
|
||||
logging.info(f'New message received from user {update.message.from_user.name} (id: {update.message.from_user.id})')
|
||||
chat_id = update.effective_chat.id
|
||||
user_id = update.message.from_user.id
|
||||
prompt = update.message.text
|
||||
prompt = message_text(update.message)
|
||||
self.last_message[chat_id] = prompt
|
||||
|
||||
if self.is_group_chat(update):
|
||||
@@ -754,6 +758,7 @@ class ChatGPTTelegramBot:
|
||||
"""
|
||||
Post initialization hook for the bot.
|
||||
"""
|
||||
await application.bot.set_my_commands(self.group_commands, scope=BotCommandScopeAllGroupChats())
|
||||
await application.bot.set_my_commands(self.commands)
|
||||
|
||||
def run(self):
|
||||
@@ -774,6 +779,9 @@ class ChatGPTTelegramBot:
|
||||
application.add_handler(CommandHandler('start', self.help))
|
||||
application.add_handler(CommandHandler('stats', self.stats))
|
||||
application.add_handler(CommandHandler('resend', self.resend))
|
||||
application.add_handler(CommandHandler(
|
||||
'chat', self.prompt, filters=filters.ChatType.GROUP | filters.ChatType.SUPERGROUP)
|
||||
)
|
||||
application.add_handler(MessageHandler(
|
||||
filters.AUDIO | filters.VOICE | filters.Document.AUDIO |
|
||||
filters.VIDEO | filters.VIDEO_NOTE | filters.Document.VIDEO,
|
||||
|
||||
Reference in New Issue
Block a user