@marxes

Ошибка: NameError("name 'message' is not defined")?

import telebot
import config
import random
import pyowm

from telebot import types

bot = telebot.TeleBot(config.token)
owm = pyowm.OWM(config.api, language = "ru")
@bot.message_handler(commands=['start'])
def welcome(message):
    sti = open('static/welcome.webp', 'rb')
    bot.send_sticker(message.chat.id, sti)

    # keyboard
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    item1 = types.KeyboardButton(" Рандомное число")
    item2 = types.KeyboardButton(" Погода в городе")

    markup.add(item1, item2)

    bot.send_message(message.chat.id, "Добро пожаловать, {0.first_name}!\nЯ - <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 lalala(message):
    if message.chat.type == 'private':
        if message.text == ' Рандомное число':
            bot.send_message(message.chat.id, str(random.randint(0,100)))
        elif message.text == ' Погода в городе':

            markup = types.InlineKeyboardMarkup(row_width=2)
            item1 = types.InlineKeyboardButton("Да", callback_data='yes')
            item2 = types.InlineKeyboardButton("Нет", callback_data='no')

            markup.add(item1, item2)

            bot.send_message(message.chat.id, "Хочешь узнать погоду?", reply_markup=markup)
        else:
            bot.send_message(message.chat.id, 'Я не знаю что ответить ')

@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    try:
        if call.message:
            if call.data == 'yes':
                bot.send_message(call.message.chat.id, 'Введи название города')

                observation = owm.weather_at_place( message.text )
                w = observation.get_weather()
                temp = w.get_temperature('celsius')["temp"]

                answer = "В городе " + message.text + " сейчас " + w.get_detailed_status() + "\n"
                answer += "Температура сейчас в районе " + str(temp) + "\n\n"

            elif call.data == 'no':
                bot.send_message(call.message.chat.id, 'Бывает ')

            # remove inline buttons
            bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=" Как дела?",
                reply_markup=None)

            # show alert
            bot.answer_callback_query(callback_query_id=call.id, show_alert=False,
                text="ЭТО ТЕСТОВОЕ УВЕДОМЛЕНИЕ!!11")

    except Exception as e:
        print(repr(e))

# RUN
bot.polling(none_stop=True)
  • Вопрос задан
  • 2278 просмотров
Решения вопроса 1
fox_12
@fox_12 Куратор тега Python
Расставляю биты, управляю заряженными частицами
В функции callback_inline вы пытаетесь вывести
observation = owm.weather_at_place( message.text )
Хотя в функции нигде message не определен
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы