AttributeError:“更新程序”对象没有属性“调度程序”

Abd*_*tan 4 python python-telegram-bot

当我运行这段代码时:

from telegram.ext import *
import keys
    
print('Starting a bot....')
     
def start_commmand(update, context):
    update.message.reply_text('Hello! Welcome To Store!')

if __name__ == '__main__':
    updater = Updater(keys.token, True)
    dp = updater.dispatcher

    # Commands
    dp.add.handler(CommandHandler('start', start_commmand))

    # Run bot
    updater.start_polling(1.0)
    updater.idle()
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Traceback (most recent call last):
  File "C:\Users\pc\PycharmProjects\telegram\main.py", line 11, in <module>
    dp = updater.dispatcher
AttributeError: 'Updater' object has no attribute 'dispatcher'
Run Code Online (Sandbox Code Playgroud)

我尝试通过更新库来解决此问题,但错误仍然存​​在。

小智 9

您可能找到了 v13 的示例,但几天后 python-telegram-bot 的 v20 已经发布了。现在您必须以不同的方式构建应用程序,并且必须使用异步函数。

这应该有效:

from telegram.ext import *
import keys
    
print('Starting a bot....')
     
async def start_commmand(update, context):
    await update.message.reply_text('Hello! Welcome To Store!')

if __name__ == '__main__':
    application = Application.builder().token(keys.token).build()

    # Commands
    application.add_handler(CommandHandler('start', start_commmand))

    # Run bot
    application.run_polling(1.0)
Run Code Online (Sandbox Code Playgroud)

另外,这里还有一些 python-telegram-bot 库的好例子。