I created a telegram bot using the pip python-telegram-bot. It was working smoothly after the first debugging ,but now when I try to change/add outputs or edit the code at all it doesn't affect the bot whatsoever . I tried to rerun the script hoping it had something to do with internet latency and telegram's servers which didn't help.
from typing import final
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
TOKEN: final = "(Redacted)"
BOT_USERNAME: final = '@ForsakenEarth_bot'
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('bot works')
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Help command works')
async def custom_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('custom command works')
def handle_response(text: str) -> str:
processed: str = text.lower()
if 'hello' in processed:
return 'hey there'
if 'how are you' in processed:
return 'Im fine'
if 'test' in processed:
return "Failed"
if 'select' in processed:
return start()
return 'I do not understand'
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
message_type: str = update.message.chat.type
text: str = update.message.text
print(f'User({update.message.chat.id}) in {message_type}: "{text}"')
if message_type == 'supergroup' or message_type == 'group':
if BOT_USERNAME in text:
new_text: str = text.replace(BOT_USERNAME, '').strip()
response: str = handle_response(new_text)
else:
return
else:
response: str = handle_response(text)
print('Bot:', response)
await update.message.reply_text(response)
async def error(update: Update, context: ContextTypes.DEFAULT_TYPE):
print(f'Update{update} caused error{context.error}')
if __name__ == '__main__':
print('Starting bot...')
app = Application.builder().token(TOKEN).build()
app.add_handler(CommandHandler('start', start_command))
app.add_handler(CommandHandler('help', help_command))
app.add_handler(CommandHandler('custom', custom_command))
app.add_handler(MessageHandler(filters.TEXT, handle_message))
app.add_error_handler(error)
print('polling...')
app.run_polling(poll_interval=3)