Y
Y
Yarik Yarik2022-03-06 15:08:20
Python
Yarik Yarik, 2022-03-06 15:08:20

How to make input in python (telegram bot)?

import telebot
import config
import random
 
from telebot import types
 
bot = telebot.TeleBot(config.TOKEN)
 
@bot.message_handler(commands=['start'])
def welcome(message):

    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    item1 = types.KeyboardButton("Cлучайное число(от 1 до 10)")
    item2 = types.KeyboardButton("Геометрическая Прогрессия")
 
    markup.add(item1, item2)
 
    bot.send_message(message.chat.id, "Добро пожаловать, {0.first_name}!\nЯ - <b>{1.first_name}</b>\nБот, который способен на все.".format(message.from_user, bot.get_me()),
        parse_mode='html', reply_markup=markup)
 
@bot.message_handler(content_types=['text'])
def main(message):
    if message.chat.type == 'private':
        if message.text == 'Cлучайное число(от 1 до 10)':
            bot.send_message(message.chat.id, str(random.randint(0,10)))
        elif message.text == 'Геометрическая Прогрессия':
            b1 = int(input('Первый член геометрической прогрессии(b1) = '))
            q = int(input('Знаменатель прогрессии(q) = '))
            n = int(input('Номер последнего члена(n) = '))
            bn = b1 * q**(n - 1)
            sn = (b1*(q**n - 1))/(q-1)
            bot.send_message(message.chat.id, str('Последний член геометрической прогрессии(bn) = '+str(bn)+'\nСумма геометрической прогрессии(SUMn) = '+str(sn)))         
        else:
            bot.send_message(message.chat.id, 'Я не знаю что ответить :(')

if name == '__main__':
    bot.skip_pending = True
    bot.polling(none_stop=True)


I'm making a geometric progression calculator for practice.
b1 = int(input('The first term of the geometric progression(b1) = '))
q = int(input('The denominator of the progression(q) = '))
n = int(input('The number of the last term(n) = ') )
I don’t know how to replace these lines ... Everything writes naturally in the console, but I don’t know how to make the bot ask in the telegram. I will be very glad for any hint.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2022-03-06
@Novichek0342

import telebot
import config
import random
 
from telebot import types
 
bot = telebot.TeleBot(config.TOKEN)
 
@bot.message_handler(commands=['start'])
def welcome(message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    item1 = types.KeyboardButton("Cлучайное число(от 1 до 10)")
    item2 = types.KeyboardButton("Геометрическая Прогрессия")
    markup.add(item1, item2)
    msg = bot.send_message(message.chat.id, "Добро пожаловать, {0.first_name}!\nЯ - <b>{1.first_name}</b>\nБот, который способен на все.".format(message.from_user, bot.get_me()),
        parse_mode='html', reply_markup=markup)
    bot.register_next_step_handler(msg, main)
 
@bot.message_handler(content_types=['text'])
def main(message):
    if message.text == 'Cлучайное число(от 1 до 10)':
     bot.send_message(message.chat.id, str(random.randint(0,10)))
    elif message.text == 'Геометрическая Прогрессия':
     msg = bot.send_message(message.chat.id, "Введите первый член геометрической прогрессии, затем знаменатель прогрессии и номер последнего члена (писать через пробел)")
     bot.register_next_step_handler(msg, calc)   
    else:
     bot.send_message(message.chat.id, 'Я не знаю что ответить :(')

def calc(message):
    a, b, c = message.text.split()
    b1 = int(a)
    q = int(b)
    n = int(c)
    bn = b1 * q**(n - 1)
    sn = (b1*(q**n - 1)) // (q-1)
    bot.send_message(message.chat.id, f'Последний член геометрической прогрессии(bn) = {bn}\nСумма геометрической прогрессии(SUMn) = {sn}')



bot.polling(none_stop=True)

Here is the code, not ideal, but it works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question