mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2026-01-04 05:25:37 +01:00
added disallowed message and removeed refresh command
This commit is contained in:
2
main.py
2
main.py
@@ -23,7 +23,7 @@ def main():
|
||||
'allowed_chats': os.environ['ALLOWED_TELEGRAM_CHAT_IDS'].split(',')
|
||||
}
|
||||
|
||||
gpt3_bot = ChatGPT3Bot(config=chatgpt_config)
|
||||
gpt3_bot = ChatGPT3Bot(config=chatgpt_config, debug=True)
|
||||
telegram_bot = ChatGPT3TelegramBot(config=telegram_config, gpt3_bot=gpt3_bot)
|
||||
telegram_bot.run()
|
||||
|
||||
|
||||
@@ -9,15 +9,19 @@ class ChatGPT3TelegramBot:
|
||||
def __init__(self, config, gpt3_bot):
|
||||
self.config = config
|
||||
self.gpt3_bot = gpt3_bot
|
||||
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"
|
||||
|
||||
# Help menu
|
||||
async def help(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
await update.message.reply_text("/start - Start the bot\n/reset - Reset conversation\n/help - Help menu")
|
||||
await update.message.reply_text("/start - Start the bot\n/reset - Reset conversation\n/help - Help menu\n\n"
|
||||
"Open source at https://github.com/n3d1117/chatgpt-telegram-bot")
|
||||
|
||||
# Start the bot
|
||||
async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
if not self.is_allowed(update):
|
||||
logging.info(f'User {update.message.from_user.name} is not allowed to start the bot')
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text=self.disallowed_message)
|
||||
return
|
||||
|
||||
logging.info('Bot started')
|
||||
@@ -27,26 +31,18 @@ class ChatGPT3TelegramBot:
|
||||
async def reset(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
if not self.is_allowed(update):
|
||||
logging.info(f'User {update.message.from_user.name} is not allowed to reset the bot')
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text=self.disallowed_message)
|
||||
return
|
||||
|
||||
logging.info('Resetting the conversation...')
|
||||
self.gpt3_bot.reset_chat()
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text="Done!")
|
||||
|
||||
# Refresh session
|
||||
async def refresh(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
if not self.is_allowed(update):
|
||||
logging.info(f'User {update.message.from_user.name} is not allowed to refresh the session')
|
||||
return
|
||||
|
||||
logging.info('Refreshing session...')
|
||||
self.gpt3_bot.refresh_session()
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text="Done!")
|
||||
|
||||
# React to messages
|
||||
async def prompt(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
if not self.is_allowed(update):
|
||||
logging.info(f'User {update.message.from_user.name} is not allowed to use the bot')
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text=self.disallowed_message)
|
||||
return
|
||||
|
||||
logging.info(f'New message received from user {update.message.from_user.name}')
|
||||
@@ -79,9 +75,8 @@ class ChatGPT3TelegramBot:
|
||||
application.add_handler(CommandHandler('start', self.start))
|
||||
application.add_handler(CommandHandler('reset', self.reset))
|
||||
application.add_handler(CommandHandler('help', self.help))
|
||||
application.add_handler(CommandHandler('refresh', self.refresh))
|
||||
application.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), self.prompt))
|
||||
|
||||
application.add_error_handler(self.error_handler)
|
||||
|
||||
application.run_polling()
|
||||
application.run_polling(poll_interval=2.0, timeout=20)
|
||||
|
||||
Reference in New Issue
Block a user