I am doing my first Telebot tutorial, however, I realised that there are some changes with the python telegram bot library and I don't understand the error which I am currently facing.
I tried to send "Hello" to my telebot but it is not responding "Hey" and there is an error in the console. However, when I do /start and /help, it works and the telebot is able to reply me with the intended messages as shown in my code
Here is my code snippet:
from typing import Final
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
TOKEN: Final = '6596845905:AAEjzfN0ipLaxcB7Gm6-KnImBVs69p3kPeQ'
BOT_USERNAME: Final = '@langXator_bot'
# Commands
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Hello! I am a bot')
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Hello! I am a help')
async def custom_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Hello! I am a custom')
# Responses
def handle_response(text: str) -> str:
if "Hello" in text:
return "Hey"
elif "Bye" in text:
return "Bye"
else:
return "Huh"
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
message_type: str = update.message.chat.type
text: str = update.message.text.strip()
print(f'User ({update.message.chat.id}) in {message_type}: "{text}"')
# differentiate talking in gc and talking in private
if 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} case error {context.error}')
if __name__ == '__main__':
print('Starting bot ....')
app = Application.builder().token(TOKEN).build()
# Commands (Telegram)
app.add_handler(CommandHandler('start', start_command))
app.add_handler(CommandHandler('help', help_command))
app.add_handler(CommandHandler('custom', custom_command))
# Messages (Telegram)
app.add_handler(MessageHandler(filters.Text, handle_message))
# Errors
app.add_error_handler(error)
# Check updates
print('Polling...')
app.run_polling(poll_interval=3) # check every 3 seconds for new messages
Here is the error I received when I sent "Hello" in my telegram bot:
Update Update(message=Message(channel_chat_created=False, chat=Chat(first_name='cxl❄️', id=854959559, type=\<ChatType.PRIVATE\>, username='dobongkimchi'), date=datetime.datetime(2023, 8, 31, 6, 58, 2, tzinfo=datetime.timezone.utc), delete_chat_photo=False, from_user=User(first_name='cxl❄️', id=854959559, is_bot=False, language_code='en', username='dobongkimchi'), group_chat_created=False, message_id=102, supergroup_chat_created=False, text='Hello'), update_id=177020891) case error check_update() missing 1 required positional argument: 'update'
I tried with just one if else statement in handle_response but it doesn't work as well
I suspect it's probably the MessageHandler or the handle_response function
filters.Text
is a subclass of BaseFilter
, filters.TEXT
is an instance of that class and hence an instance of BaseFilter
. The first argumet of MessageHandler
expects an instance of BaseFilter
, not a subclass.
Disclaimer: I'm currently the maintainer of python-telegram-bot
.