我通过 replit 为基本 Telegram 机器人编写了 Python 代码。
在 shell 中我写道:
pip install python-telegram-bot==13.7
到目前为止,它对我有用,但从现在开始,它会给我写这样的内容:“更新”是未知的导入符号,“过滤器”是未知的导入符号,主要错误是:
Traceback (most recent call last):
File "/home/runner/PeachpuffSquareTraining/main.py", line 1, in <module>
from telegram import Update
ImportError: cannot import name 'Update' from 'telegram' (/home/runner/PeachpuffSquareTraining/.pythonlibs/lib/python3.10/site-packages/telegram/__init__.py)
这是我尝试过的代码,到目前为止,使用上面的点它对我有用:
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
# Replace 'YOUR_BOT_TOKEN' with the actual token you obtained from BotFather
TOKEN = 'the token is secret'
def start(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Hello! I am your bot.')
def echo(update: Update, context: CallbackContext) -> None:
update.message.reply_text(update.message.text)
def main() -> None:
updater = Updater(TOKEN)
dp = updater.dispatcher
# Command handlers
dp.add_handler(CommandHandler("start", start))
# Message handler for echoing messages
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
# Start the Bot
updater.start_polling()
# Run the bot until you send a signal to stop it
updater.idle()
if __name__ == '__main__':
main()
有什么问题吗?
你是不是把
Update
打错了Updater
?