错误是,当我选择:Samsung,Xiaomi或Huawei时,会出现这样的NameError错误(“名称'message'未定义”)。我试图更改他们的顺序,结果发现我选择了最后一个以外的东西时出错。问题是这部分代码无法正常运行“ bot.edit_message_text(chat_id = call.message。 chat.id,message_id = call.message.message_id,text =“ Scegliete cosa vi interessa:”,reply_markup = None)“
import telebot
import config
from telebot import types
bot = telebot.TeleBot(config.TOKEN)
@bot.message_handler(commands=['start'])
def welcome(message):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton("Catalogo")
item2 = types.KeyboardButton("Regole")
markup.add(item1, item2)
bot.send_message(message.chat.id, "Benvenuto, {0.first_name}!\nIo sono - <b>{1.first_name}</b>.".format(message.from_user, bot.get_me()),
parse_mode='html', reply_markup=markup)
@bot.message_handler(content_types=['text'])
def risposta(message):
if message.chat.type == 'private':
if message.text == "Catalogo":
markup = types.InlineKeyboardMarkup(row_width = 1)
item1 = types.InlineKeyboardButton("iPhone", callback_data = "iPhone")
item2 = types.InlineKeyboardButton("Samsung", callback_data = "Samsung")
item3 = types.InlineKeyboardButton("Huawei", callback_data = "Huawei")
item4 = types.InlineKeyboardButton("Xiaomi", callback_data = "Xiaomi")
markup.add(item1, item2, item3, item4)
bot.send_message(message.chat.id, "Scegliete cosa vi interessa:", reply_markup=markup)
elif message.text == "Regole":
bot.send_message(message.chat.id, " 1)Dovete vivere a Roma.\n2)Il pagamento viene effettuato dopo aver scielto il telefono.\n3)")
else :
bot.send_message(message.chat.id, "Scusate non ho capito")
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
try:
if call.message:
if call.data == "Samsung" :
markup = types.InlineKeyboardMarkup(row_width = 3)
item1 = types.InlineKeyboardButton("Model", callback_data = "Model")
item2 = types.InlineKeyboardButton("Model", callback_data = "Model")
item3 = types.InlineKeyboardButton("Model", callback_data = "Model")
item4 = types.InlineKeyboardButton("Model", callback_data = "Model")
item5 = types.InlineKeyboardButton("Model", callback_data = "Model")
item6 = types.InlineKeyboardButton("Model", callback_data = "Model")
item7 = types.InlineKeyboardButton("Model", callback_data = "Model")
item8 = types.InlineKeyboardButton("Model", callback_data = "Model")
item9 = types.InlineKeyboardButton("Model", callback_data = "Model")
item10 = types.InlineKeyboardButton("Model", callback_data = "Model")
item11 = types.InlineKeyboardButton("Model", callback_data = "Model")
item12 = types.InlineKeyboardButton("Model", callback_data = "Model")
item13 = types.InlineKeyboardButton("Model", callback_data = "Model")
item14 = types.InlineKeyboardButton("Model", callback_data = "Model")
item15 = types.InlineKeyboardButton("Model", callback_data = "Model")
item16 = types.InlineKeyboardButton("Model", callback_data = "Model")
item17 = types.InlineKeyboardButton("Model", callback_data = "Model")
item18 = types.InlineKeyboardButton("Model", callback_data = "Model")
markup.add(item1, item2, item3, item4, item5, item6, item7, item8, item9, item10, item11, item12, item13, item14, item15, item16, item17, item18)
bot.send_message(call.message.chat.id, "Scegliete il modello:", reply_markup=markup)
if call.data == "Xiaomi":
markup = types.InlineKeyboardMarkup(row_width = 3)
item1 = types.InlineKeyboardButton("Model", callback_data = "Model")
item2 = types.InlineKeyboardButton("Model", callback_data = "Model")
item3 = types.InlineKeyboardButton("Model", callback_data = "Model")
item4 = types.InlineKeyboardButton("Model", callback_data = "Model")
item5 = types.InlineKeyboardButton("Model", callback_data = "Model")
item6 = types.InlineKeyboardButton("Model", callback_data = "Model")
item7 = types.InlineKeyboardButton("Model", callback_data = "Model")
item8 = types.InlineKeyboardButton("Model", callback_data = "Model")
item9 = types.InlineKeyboardButton("Model", callback_data = "Model")
item10 = types.InlineKeyboardButton("Model", callback_data = "Model")
markup.add(item1, item2, item3, item4, item5, item6, item7, item8, item9, item10)
bot.send_message(call.message.chat.id, "Scegliete il modello:", reply_markup=markup)
if call.data == "Huawei":
markup = types.InlineKeyboardMarkup(row_width = 3)
item1 = types.InlineKeyboardButton("Model", callback_data = "Model")
item2 = types.InlineKeyboardButton("Model", callback_data = "Model")
item3 = types.InlineKeyboardButton("Model", callback_data = "Model")
item4 = types.InlineKeyboardButton("Model", callback_data = "Model")
item5 = types.InlineKeyboardButton("Model", callback_data = "Model")
item6 = types.InlineKeyboardButton("Model", callback_data = "Model")
item7 = types.InlineKeyboardButton("Model", callback_data = "Model")
item8 = types.InlineKeyboardButton("Model", callback_data = "Model")
item9 = types.InlineKeyboardButton("Model", callback_data = "Model")
item10 = types.InlineKeyboardButton("Model", callback_data = "Model")
markup.add(item1, item2, item3, item4, item5, item6, item7, item8, item9, item10)
bot.send_message(call.message.chat.id, "Scegliete il modello:", reply_markup=markup)
if call.data == 'iPhone':
markup = types.InlineKeyboardMarkup(row_width = 3)
item1 = types.InlineKeyboardButton("iPhone 11 Pro Max", callback_data = "iPhone 11 Pro Max")
item2 = types.InlineKeyboardButton("iPhone 11 Pro", callback_data = "iPhone 11 Pro")
item3 = types.InlineKeyboardButton("iPhone 11", callback_data = "iPhone 11")
item4 = types.InlineKeyboardButton("iPhone XS Max", callback_data = "iPhone XS Max")
item5 = types.InlineKeyboardButton("iPhone XS", callback_data = "iPhone XS")
item6 = types.InlineKeyboardButton("iPhone XR", callback_data = "iPhone XR")
item7 = types.InlineKeyboardButton("iPhone X", callback_data = "iPhone X")
item8 = types.InlineKeyboardButton("iPhone 8 Plus", callback_data = "iPhone 8 Plus")
item9 = types.InlineKeyboardButton("iPhone 8", callback_data = "iPhone 8")
item10 = types.InlineKeyboardButton("iPhone 7 Plus", callback_data = "iPhone 7 Plus")
item11 = types.InlineKeyboardButton("iPhone 7", callback_data = "iPhone 7")
markup.add(item1, item2, item3, item4, item5, item6, item7, item8, item9, item10, item11)
bot.send_message(call.message.chat.id, "Scegliete il modello:", reply_markup=markup)
else :
bot.send_message(call.message.chat.id, "Scusate non ho capito")
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Scegliete cosa vi interessa:",
reply_markup=None)
except Exception as e:
print(repr(e))
bot.polling(none_stop=True)
答案 0 :(得分:0)
我认为该错误实际上是针对iphone条件的else块中的bot.send_message(message.chat.id, "Scusate non ho capito")
行。对象message
不存在,它会产生您正在接收的NameError。将其更改为call.message.chat.id
以匹配代码的其他部分应该可以解决该问题。