A
A
Alexander2019-03-28 23:41:48
Python
Alexander, 2019-03-28 23:41:48

How to pull a telegram user's response to a bot and add it to a string type variable?

You need to write a bot-questionnaire in telegrams. When writing the /go command, the bot sends a message with a question and KeyBoardMarkup appears. The text that will be written to the variable depends on the pressed button.
Main.py

import const
import telebot
import markups
from telebot import types

arr_answers = []

bot = telebot.TeleBot(const.token)

@bot.message_handler(commands=['start'])
def start_handler(message):
    bot.send_message(message.chat.id, "Привет, я простой бот")

@bot.message_handler(commands=['go'])
def start_answer(message):
    chat_id = message.chat.id
    bot.send_message(message.chat.id, 'В каком районе ищите кв?', reply_markup=markups.source_markup)

markups.py
from telebot import types
source_markup = types.ReplyKeyboardMarkup(row_width=1)
source_markup_butt1 = types.KeyboardButton(text = "Голосіївський")
source_markup_butt2 = types.KeyboardButton(text = "Дарницький")
source_markup_butt3 = types.KeyboardButton(text = "Деснянський")
source_markup_butt4 = types.KeyboardButton(text = "Дніпровський")
source_markup_butt5 = types.KeyboardButton(text = "Оболонський")
source_markup_butt6 = types.KeyboardButton(text = "Печерський")
source_markup_butt7 = types.KeyboardButton(text = "Подільський")
source_markup_butt8 = types.KeyboardButton(text = "Святошинський")
source_markup_butt9 = types.KeyboardButton(text = "Солом'янський")
source_markup_butt10 = types.KeyboardButton(text = "Шевченківський")
source_markup.add(source_markup_butt1, source_markup_butt2, source_markup_butt3, source_markup_butt4, source_markup_butt5, source_markup_butt6,source_markup_butt7,source_markup_butt8,source_markup_butt9,source_markup_butt10)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2019-03-29
@AlexDolls

Add a handler for incoming text messages:

@bot.message_handler(content_types=["text"])
def i_read_your_message (message): #название функции не имеет значение
    if message.text == "Голосіївський":
        #do_something
    elif message.text == "Дарницький":
        #do_something_else

You can make the keyboard in the form of InlineKeyboard (that is, it is displayed not instead of the native keyboard, but directly below the message in the chat window). There the return is somewhat different. If necessary, I will write an example.
There is another option to register the entered text, and pressing the button == entering just the text that is written on the button, through the method
But it doesn't "start" for me. Asked a question here , but no answer yet.

M
Mikhail Trainin, 2019-03-29
@Stormx480

Either add a conditional statement, or make inline buttons with callbacks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question