我正在使用 python-telegram-bot V20.2 创建这样的东西:聊天和用户选择
当用户共享群组/频道聊天时,我在处理事件时遇到问题。
这是我的代码:
from telegram import Update, KeyboardButton, KeyboardButtonRequestChat, ReplyKeyboardMarkup
from telegram.ext import ApplicationBuilder, CommandHandler, CallbackContext, MessageHandler, filters
bot_token = "bot_token"
# python-telegram-bot setup
application = ApplicationBuilder().token(bot_token).build()
# python-telegram-bot command handler
async def start(update: Update, context: CallbackContext):
request_chat = KeyboardButtonRequestChat(1, True)
keyboard = [[KeyboardButton("Request Chat", request_chat=request_chat)]]
reply_markup = ReplyKeyboardMarkup(keyboard)
await update.message.reply_text('Please share your contact details', reply_markup=reply_markup)
async def request_contact(update: Update, context: CallbackContext):
chat = await update.chat_shared
print(chat.id)
application.add_handler(CommandHandler('start', start))
application.add_handler(MessageHandler(filters._Contact, request_contact)) #here is the problem!
# Running both libraries
print('Running...')
application.run_polling()
在我运行机器人、调用 \start 命令并分享聊天后,我遇到了这个错误:
No error handlers are registered, logging exception.
Traceback (most recent call last):
File "/home/amin/anaconda3/envs/telegramBot/lib/python3.11/site-packages/telegram/ext/_application.py", line 1099, in process_update
check = handler.check_update(update) # Should the handler handle this update?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/amin/anaconda3/envs/telegramBot/lib/python3.11/site-packages/telegram/ext/_messagehandler.py", line 99, in check_update
return self.filters.check_update(update) or False
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: MessageFilter.check_update() missing 1 required positional argument: 'update'
您使用了
filters._Contact
,它是filters
模块的受保护类。这些不是供用户使用的,而是供python-telegram-bot
库本身内部使用的。另见PEP8。
请选择 here 中记录的过滤器之一。我想你会想要使用filters.StatusUpdate.CHAT_SHARED
.
免责声明:我目前是
python-telegram-bot
.的维护者