我刚刚创建了我的第一个机器人,它在组中工作得很好,但是当我给它发消息时,当我把它添加到一个频道并给它所有权限时,它就不能工作了。echo message函数给出caused error 'NoneType' object has no attribute 'text'错误。
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram import MessageEntity, InlineQueryResultArticle, InputTextMessageContent
def echo(update, context): # this is from the documentation
context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text)
def error(update, context):
print(f'Error {update} caused error {context.error}')
def main():
updater = Updater(API)
dp = updater.dispatcher
#Echo message
echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
dp.add_handler(echo_handler)
#Handle errors
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
main()发布于 2021-03-09 20:01:56
对于频道帖子,它是update.channel_post,而不是update.message.text。或者,如果你不想区分频道帖子、消息和编辑过的消息/频道帖子,也可以使用update.effective_message。
def echo(update, context):
context.bot.send_message(
chat_id=update.effective_chat.id, text=update.effective_message)https://stackoverflow.com/questions/66528468
复制相似问题