导入错误:无法从“电报”(未知位置)导入名称“Bot”

时间:2021-02-16 17:49:44

标签: python telegram telegram-bot

我开始用 Python 编写一个电报机器人,并在第一个代码中存放:

from telegram.ext import Updater, CommandHandler

updater = Updater('1670111367:AAHEzzfNQUeWL9ylptnDyQPZjarACLNUcGc')


def start(bot,update):
    chat_id=update.message.chat_id
    bot.sendMessage(chat_id,"hello")

start_command=CommandHandler('start',start)

updater.dispatcher.add_handler(start_command)


updater.start_polling()
updater.idle()

但我收到此错误:

Traceback (most recent call last):
  File "/home/smjt2000/Programming/Projects/Telegram_Bot/1.py", line 1, in <module>
    from telegram.ext import Updater, CommandHandler
  File "/home/smjt2000/.local/lib/python3.8/site-packages/telegram/ext/__init__.py", line 21, in <module>
    from .basepersistence import BasePersistence
  File "/home/smjt2000/.local/lib/python3.8/site-packages/telegram/ext/basepersistence.py", line 25, in <module>
    from telegram import Bot
ImportError: cannot import name 'Bot' from 'telegram' (unknown location)

我阅读了文档但我无法修复它!!!

1 个答案:

答案 0 :(得分:0)

首先,您不应在此处发布您的令牌密钥。 我认为错误出在函数声明上。您还应该导入“CallbackContext”并使用“update.message.reply_text”(而不是bot.sendMessage)。看看项目主页中的这个“Hello World bot”。