@Heltes

Телеграм бот. Не работает получение сообщений. В чем ошибка?

import telebot
import requests
from telebot import types


bot = telebot.TeleBot('token')
@bot.message_handler(commands=["start"])
def start(m, res=False):
    bot.send_message(m.chat.id, 'Хотите узнать погоду?\nВведите свой город')

@bot.message_handler(content_types=["text"])
def handle_text(message):

    sg = {'Москва': 'Moscow','Самара': 'Samara'}
    a = sg[message.text.strip()]

    appid = 'token'
    url = 'http://api.openweathermap.org/data/2.5/find?q={}&type=like&units=metric&appid='+appid
    all_cities = []
    cities = [a]

    for city in cities:
        res = requests.get(url.format(city)).json()
        city_info = {
            'city': city,
            'temp': res["list"][0]["main"]["temp"],
            'rain': res["list"][0]["rain"],
            'clouds': res["list"][0]["weather"][0]["description"]
            }
        all_cities.append(city_info)
    bot.send_message(message.chat.id, 'Город установлен.')
    markup=types.ReplyKeyboardMarkup(resize_keyboard=True)
    item1=types.KeyboardButton("Текущая погода в городе")
    markup.add(item1)
    bot.send_message(message.chat.id, 'Узнать погоду?',reply_markup=markup)


    if message.text.strip() == 'Текущая погода в городе':
        if all_cities[0]["clouds"] == 'clear sky':
            cl = "Безоблачно"
        elif all_cities[0]["clouds"] == 'few clouds':
            cl = "Ясно"
        elif all_cities[0]["clouds"] == 'overcast clouds':
            cl = "Пасмурно"
        if all_cities[0]['rain'] != "None":
            dz = ""
        else:
            dz = " | Дождь"
        answer = "Сейчас в Самаре " + str(int(all_cities[0]["temp"])) + " градусов | "+cl + dz
        bot.send_message(message.chat.id, answer)

bot.polling(none_stop=True, interval=0)
  • Вопрос задан
  • 91 просмотр
Решения вопроса 1
Keffmen
@Keffmen
Изучаю Python , C# , HTML и многое другое
Смотри тебе нужно было использовать bot.register_next_step в своем коде , вот пример как он работает
import telebot
import requests
from telebot import types

global stairs

bot = telebot.TeleBot('token')
@bot.message_handler(commands=["start"])
def start(m, res=False):
    bot.send_message(m.chat.id, 'Хотите узнать погоду?\nВведите свой город')
    bot.register_next_step_handler_by_chat_id(m.chat.id, regist)
@bot.message_handler(content_types=['text'])
def main(m):
    if m.text.strip() == 'Текущая погода в городе':
        bot.send_message(m.chat.id, f'Сейчас в {stairs} - жарко')
def regist(message):
    global stairs
    stairs = message.text
    bot.send_message(message.chat.id, f'Твой город :{message.text}')
bot.polling(none_stop=True, interval=0)
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

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

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