Answer the question
In order to leave comments, you need to log in
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)
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
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
Either add a conditional statement, or make inline buttons with callbacks.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question