@MRSalmone

Телеграм бот использует введенное число пользователем для решения задач.Поможете?

Пробую сделать телеграм бота способного переводить из граммов в миллилитры (мука, дрожжи и т.д), и не могу найти способ, чтобы бот спрашивал у пользователя какое количество грамм вещества он хочет перевести и полученное сообщение от человека он сохранял и использовал для перевода в миллилитры.
Пользователь пишет к примеру 123, после выбирает муку и получает ответ в виде 208 миллилитров .

Вот сам код (я новичок, так что не бейте меня тапкам)
Использую pyTelegramBotAPI.
import telebot
import conf
from telebot import types


bot = telebot.TeleBot(conf.TOKEN)


@bot.message_handler(commands=['start'])
def welcome(message):
    sti = open('C:/Users/mvini/Downloads/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 pizza(message):
    if message.chat.type == 'private':
        if message.text == 'Мука':

            markup = types.InlineKeyboardMarkup(row_width=5)
            itam1 = types.InlineKeyboardButton("10", callback_data='10м')
            itam2 = types.InlineKeyboardButton("20", callback_data='20м')
            itam3 = types.InlineKeyboardButton("50", callback_data='50м')
            itam4 = types.InlineKeyboardButton("100", callback_data='100м')
            itam5 = types.InlineKeyboardButton("200", callback_data='200м')



            markup.add(itam1, itam2, itam3, itam4, itam5)

            bot.send_message(message.chat.id, 'Сколько грамм?', reply_markup=markup)

        elif message.text == 'Дрожжи':

            markup = types.InlineKeyboardMarkup(row_width=5)
            item1 = types.InlineKeyboardButton("10", callback_data='10д')
            item2 = types.InlineKeyboardButton("20", callback_data='20д')
            item3 = types.InlineKeyboardButton("50", callback_data='50д')
            item4 = types.InlineKeyboardButton("100", callback_data='100д')
            item5 = types.InlineKeyboardButton("200", callback_data='200д')



            markup.add(item1, item2, item3, item4, item5)

            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 == '10м':
                l = 10 * 1695 / 1000

                bot.send_message(call.message.chat.id, l)
            elif call.data == '20м':
                l = 20 * 169 // 100
                bot.send_message(call.message.chat.id, l)
            elif call.data == '50м':
                l = 50 * 169 // 100
                bot.send_message(call.message.chat.id, l)
            elif call.data == '100м':
                l = 100 * 169 // 100
                bot.send_message(call.message.chat.id, l)
            elif call.data == '200м':
                l = 200 * 169 // 100
                bot.send_message(call.message.chat.id, l)
            elif call.data == '20д':
                l = 20 * 100 // 63
                bot.send_message(call.message.chat.id, l)
            elif call.data == '50д':
                l = 50 * 100 // 63
                bot.send_message(call.message.chat.id, l)
            elif call.data == '100д':
                l = 100 * 100 // 63
                bot.send_message(call.message.chat.id, l)
            elif call.data == '200д':
                l = 200 * 100 // 63
                bot.send_message(call.message.chat.id, l)
            elif call.data =='10д':
                l = 10 * 100 // 63
                bot.send_message(call.message.chat.id, l)


            # 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="Отлично:)")

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


# RUN
bot.polling(none_stop=True)
  • Вопрос задан
  • 706 просмотров
Пригласить эксперта
Ответы на вопрос 1
@IgorDmitrow
В pyTelegramBotAPI есть функция
bot.register_next_step_handler(msg, process_text_step)
msg - сообщение бота после которого пользователь должен дать ответ
process_text_step - функция обработки ответа пользователя
Ответ написан
Комментировать
Ваш ответ на вопрос

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

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