首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在通道中时出现python-telegram-bot错误

在通道中时出现python-telegram-bot错误
EN

Stack Overflow用户
提问于 2021-03-08 18:53:06
回答 1查看 327关注 0票数 0

我刚刚创建了我的第一个机器人,它在组中工作得很好,但是当我给它发消息时,当我把它添加到一个频道并给它所有权限时,它就不能工作了。echo message函数给出caused error 'NoneType' object has no attribute 'text'错误。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-09 20:01:56

对于频道帖子,它是update.channel_post,而不是update.message.text。或者,如果你不想区分频道帖子、消息和编辑过的消息/频道帖子,也可以使用update.effective_message。

代码语言:javascript
复制
def echo(update, context):
    context.bot.send_message(
    chat_id=update.effective_chat.id, text=update.effective_message)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66528468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档