首页 > 解决方案 > 在创建 Telegram Bot 时,我应该怎么做才能防止 python 给出“更新”的名称错误?

问题描述

当我试图从用户那里获取我的电报机器人的输入时,我遇到了这个错误消息。谢谢你的帮助。

Traceback (most recent call last):

bot.polling()

File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pytelegrambotapi-3.8.1-py3.9.egg\telebot\__init__.py", line 620, in polling

File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pytelegrambotapi-3.8.1-py3.9.egg\telebot\__init__.py", line 679, in __threaded_polling

File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pytelegrambotapi-3.8.1-py3.9.egg\telebot\__init__.py", line 642, in __threaded_polling

File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pytelegrambotapi-3.8.1-py3.9.egg\telebot\util.py", line 130, in raise_exceptions

File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\pytelegrambotapi-3.8.1-py3.9.egg\telebot\util.py", line 82, in run

File "C:\Users\Desktop\CalendarGo\Main.py", line 22, in set
    user_input = update.message.text

NameError: name 'update' is not defined
import os
import telebot
from telebot.types import Update
from telegram.ext import Updater, Filters, CommandHandler, MessageHandler


API_KEY = "API_KEY"
bot = telebot.TeleBot(API_KEY)

@bot.message_handler(commands=["Set"])
def set(message):
    bot.send_message(message.chat.id, "Please enter the word")
    user_input = update.message.text
    update.message.reply_text(do_something(user_input))

def do_something(user_input):
    answer = "You have wrote me " + user_input
    return answer
bot.polling()

标签: pythontelegram-botnameerror

解决方案


推荐阅读