繁体   English   中英

class“更新程序”的未解析属性引用“调度程序”

[英]Unresolved attribute reference 'dispatcher' for class 'Updater'

我尝试创建 Telegram 机器人,但无法使用属性更新 class。

import logging
from multiprocessing import Queue

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler

def main():
    update_queue = Queue()
    updater = Updater("API KEY", use_context=True, update_queue=update_queue)

    updater.dispatcher.add_handler(CommandHandler('start', start))
    updater.dispatcher.add_handler(CallbackQueryHandler(button))

    updater.start_polling()
    updater.idle()


if __name__ == '__main__':
    main()

空闲说更新 class 中没有调度程序。尝试更新电报 api,没有帮助。 找不到其他更新机器人的方法

从 20.0 版开始, Updater仅用于从文档中获取更新:

版本 20.0 中的更改:

  • 删除了参数和属性 user_sig_handler

  • 唯一的 arguments 和属性现在是botupdate_queue ,因为现在这个 class 的唯一目的是获取更新 PTB 应用程序的入口点现在是telegram.ext.Application


因此,如果您想添加处理程序,请使用Application

一个例子,取自echo bot example

async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    """Echo the user message."""
    await update.message.reply_text(update.message.text)


def main() -> None:
    """Start the bot."""
    # Create the Application and pass it your bot's token.
    application = Application.builder().token("TOKEN").build()

    # on different commands - answer in Telegram
    application.add_handler(CommandHandler("start", start))
    application.add_handler(CommandHandler("help", help_command))

    # on non command i.e message - echo the message on Telegram
    application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))

    # Run the bot until the user presses Ctrl-C
    application.run_polling()


if __name__ == "__main__":
    main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM