M
M
MRSalmone2020-07-06 23:57:51
Python
MRSalmone, 2020-07-06 23:57:51

Telegram bot uses the number entered by the user to solve problems. Can you help?

I’m trying to make a telegram bot capable of converting from grams to milliliters (flour, yeast, etc.), and I can’t find a way for the bot to ask the user how many grams of the substance he wants to translate and the received message from the person he saved and used to translate into milliliters.
The user writes, for example, 123, then selects flour and receives an answer in the form of 208 milliliters.

Here is the code itself (I'm a beginner, so don't hit me with slippers) I'm
using 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)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IgorDmitrow, 2020-07-07
@IgorDmitrow

pyTelegramBotAPI has a function
bot.register_next_step_handler(msg, process_text_step)
msg ​​- bot message after which the user must give a response
process_text_step - user response processing function

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question