Home python how to receive user entered text telegram bot (Python, Telebot)

how to receive user entered text telegram bot (Python, Telebot)

Author

Date

Category

tell me how you can accept the entered user text

import telebot
bot = telebot.TeleBot ('token')
keyboard = telebot.types.ReplyKeyboardMarkup (True, True)
keyboard1 = telebot.types.ReplyKeyboardMarkup (True)
keyboard.row ('SMS', 'Calls')
keyboard1.row ('Back')
phone = ''
@ bot.message_handler (commands = ['start'])
def start_messages (message):
  bot.send_message (message.from_user.id, "What type of message would you like to make?", reply_markup = keyboard)
@ bot.message_handler (content_types = ['text'])
def after_text (message):
  if message.text == 'SMS':
    input1 = message.text
    bot.send_message (message.from_user.id, 'Enter phone number:', reply_markup = keyboard1)
    bot.register_next_step_handler (message, after_text)
  elif message.text == 'Calls':
    bot.send_message (message.from_user.id, 'Coming soon ...', reply_markup = keyboard1)
  elif message.text == 'Back':
    bot.send_message (message.from_user.id, 'What type of message would you like to make?', reply_markup = keyboard)
  else:
    bot.send_message (message.from_user.id, 'I don't understand you')
bot.polling (none_stop = True, interval = 0)

I saw on the Internet they write that you need to use bot.register_next_step_handler (message, after_text) What’s next, how to make the phone variable take the value of the entered person’s number?


Answer 1, authority 100%

@ bot.message_handler (content_types = ['text'])
def after_text (message):
  if message.text == 'SMS':
    msg = bot.send_message (message.from_user.id, 'Enter phone number:', reply_markup = keyboard1)
    bot.register_next_step_handler (msg, after_text_2)
...
def after_text_2 (message):
  print ('the phone number entered by the user at the "sms" step:', message.text)

bot.register_next_step_handler takes two arguments:

  • to whom \ where / what to send
  • which step (function) to go to

after sending msg , waiting for input is performed, followed by “forwarding” to the function specified by the second argument.

this is if on your fingers.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions