这是我正在测试的 Telegram 机器人的代码:
from typing import Final
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
import sys
# function that implements the message handler
TOKEN: Final = 'BOT_TOKEN'
BOT_USERNAME: Final = '@flissi_bot'
# command
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Hi! I'm pizza_bot . I can help you with your shopping needs.")
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Hi! I'm pizza_bot. tape somthing so i can help you.")
async def custom_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("the a custon command.")
# reponses
def handel_responses(text: str) -> str:
processed: str = text.lower()
if 'hello' in processed:
return 'hello'
if 'how are you' in processed:
return 'I am fine, thank you'
if 'goodbye' in processed:
return 'goodbye'
if 'thank you' in processed:
return 'you are welcome'
return 'I do not understand you'
async def handel_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 == 'group':
if BOT_USERNAME in text:
new_text: str = text.replace(BOT_USERNAME, '').strip()
response: str = handel_responses(new_text)
else:
return
else:
response: str = handel_responses(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()
# commands
app.add_handler(CommandHandler('start', start_command))
app.add_handler(CommandHandler('custom', custom_command))
app.add_handler(CommandHandler('help', help_command))
# Messages
app.add_handler(MessageHandler(filters.Text, handel_message))
# Errors
app.add_error_handler(error)
# Poll the bot
print('polling bot...')
app.run_polling(poll_interval=3)
我收到以下错误:
"Update(message=Message(channel_chat_created=False, chat=Chat(first_name='Flissi', id=6143267777, last_name='Hamed', type=<ChatType.PRIVATE>), date=datetime.datetime(2024, 5, 10, 2, 22, 59, tzinfo=datetime.timezone.utc), delete_chat_photo=False, from_user=User(first_name='Flissi', id=6143267777, is_bot=False, language_code='en', last_name='Hamed'), group_chat_created=False, message_id=28, supergroup_chat_created=False, text='hello'), update_id=589857240)" caused error "MessageFilter.check_update() missing 1 required positional argument: 'update'"
有人可以帮忙吗?
telegram.ext.MessageHandler
应使用子类 telegram.ext.BaseFilter
的类的实例和回调函数进行初始化。
telegram.ext.filters.Text
就是这样的类,可用于实例化 MessageHandler:
app.add_handler(MessageHandler(filters.Text(), handel_message))
或使用模块中提供的实例。
app.add_handler(MessageHandler(filters.TEXT, handel_message))