N
N
Nikolay Kravchenko2021-07-05 22:35:48
Python
Nikolay Kravchenko, 2021-07-05 22:35:48

There are 2 blocks, for the weather and for talking. How can I make it so that if I select "Weather" then it shows the weather without a second request?

@bot.message_handler(commands=['start'])
def start_message(message):
    keyboard = telebot.types.ReplyKeyboardMarkup(True)
    keyboard.row('Погода', 'Поговори со мной')
    bot.send_message(message.chat.id, 'Привет!', reply_markup=keyboard) 

@bot.message_handler(content_types = ["text"])
def send_msg(message):
  if message.text == 'Погода':
    bot.send_message(message.chat.id, "Введите город");
    bot.register_next_step_handler(message, send_weather);

  elif message.text == 'Поговори со мной':
    bot.send_message(message.chat.id, "Напиши мне");
    bot.register_next_step_handler(message, send_echo);

def send_weather(message):
  observation = mgr.weather_at_place (message.text)
  w = observation.weather
  temp = w.temperature('celsius')["temp"]
  wind = w.wind()["speed"] 
  answer = "В городе " + message.text + " сейчас " + w.detailed_status + ".\n" "За окном " + str(int(temp)) + " градусов.\n" "Скорость ветра " + str(wind) + " м/с."
  bot.send_message(message.chat.id, answer)

def send_echo (message):
  bot.send_message (message.chat.id, message.text)
  
bot.polling (none_stop = True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-07-06
@Nicky03

Store state for the user.
Those. for each user, some data structure is needed that will store information - whether the user requested the weather (and if so, in which city), whether the user is on a call, and so on.
Also, you need to decide - do you want the bot to remember this between restarts? If you can limit yourself to memorizing within the framework of one working session, then the matter is simplified. If not, then you will need to deal with databases.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question