R
R
recursy2021-12-02 14:07:31
Python
recursy, 2021-12-02 14:07:31

How to process user requests with a delay in Python telegram bot?

Good afternoon, I'm still a beginner programmer and have already gone through the basics and write a bot that uses the API
Here is my code:
I write a bot to which they enter the /lowprice command, then they enter the City in which the search will be performed and then, when the API request is processed, the bot asks enter the maximum number of hotels, and so I want to make it so that for each hotel displayed, the bot asks if it is necessary to display a photo of this hotel. The request itself works for me, it displays hotel prices, but the bot immediately displays n the number of times whether it is necessary to enter a photo without waiting for the user's response.

My question is how to make the bot wait for the user's response, and then display the necessary information, do not ask me to write the code for me, I would like to hear your opinion on this matter, for example, that you need to use generators or, for example, some bot function. And another mini question (To request some new information from the user in the function, you always need to use bot.register_next_step_handler(message, low_price_hotels) and write a new function:?) Thank you!

from decouple import config
import telebot
from bots_requests.low_price_req import Lowprice
from requests import get
bot = telebot.TeleBot(config('KEY'))


@bot.message_handler(content_types=['text'])
def get_text_messages(message):
    if message.text == "/lowprice":
        bot.send_message(message.from_user.id, "Введите город: ")
        bot.register_next_step_handler(message, low_price_hotels)
    else:
        bot.send_message(message.from_user.id, 'Я вас не понимаю')


def low_price_hotels(message):
    hotels_request = Lowprice(message.text)
    sorted_function = hotels_request.sorted_hotels()
    bot.reply_to(message, f'Введите количетсво отелей(max: {len(sorted_function)}): ')
    bot.register_next_step_handler(message, hotels_max, sorted_function)


def hotels_max(message, *args):
    maximum_hotels = int(message.text)
    for sorted_hotels in range(0, maximum_hotels):
        hotel_name, hotel_price, hotel_id = args[0][sorted_hotels].split(',')
        bot.send_message(message.from_user.id, hotel_name, hotel_price)
        bot.send_message(message.from_user.id, 'Хотите ли вы получить фото?')
        bot.register_next_step_handler(message, photo_funct, hotel_id)


def photo_funct(message, *args):
    if message.text == 'да':
        bot.send_photo(message.chat.id, get("https://exp.cdn-hotels.com/hotels/37000000/36790000/36789900/36789845/895ded4a.jpg").content)


if __name__ == '__main__':
    bot.polling(none_stop=True, interval=0)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-12-02
@recursy

I would just add an inline button. It is convenient for the user, and the work does not stop.

def hotels_max(message, *args):
    maximum_hotels = int(message.text)
    for sorted_hotel in args[0][:maximum_hotels]:
        hotel_name, hotel_price, hotel_id = sorted_hotel.split(',')
        kb = types.InlineKeyboardMarkup()
        kb.add(types.InlineKeyboardButton('Получить фото', callback_data=f'hotel_{hotel_id}'))
        bot.send_message(message.from_user.id, f'{hotel_name}, {hotel_price}', reply_markup=kb)

@bot.callback_query_handler(func=lambda call: call.data.startswith('hotel_'))
def hotel_photo(call):
    # Предположу, что фото можно будет получить по ID отеля, поэтому достаем его
    hotel_id = call.data.split('_')[-1]
    # ну и дальше уже получаем фото и отправляем

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question