D
D
danilkapuh2017-10-06 14:43:43
Python
danilkapuh, 2017-10-06 14:43:43

Telegram bot python. How to do if exactly the message that you asked was sent so that the bot responds?

Here is the code -

import telebot
import config
from telebot import types

bot = telebot.TeleBot(config.token)

@bot.message_handler(commands=['start'])
def start(m):
     msg = bot.send_message(m.chat.id, 'Привет!')
     keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
     keyboard.add(*[types.KeyboardButton(name) for name in ['Шерлок Холмс', 'Доктор Ватсон']])
     bot.send_message(m.chat.id, 'Кого выбираешь?',
                      reply_markup=keyboard)
     bot.register_next_step_handler(msg, name)
<b>
def name(m):
     if m.text == 'Шерлок Холмс':
          bot.send_message(m.chat.id, '*Ты выбрал Шерлока Холмса', parse_mode='Markdown')
     elif m.text == 'Доктор Вастсон':
          bot.send_message(m.chat.id, '*Ты выбрал Доктора Ватсона', parse_mode='Markdown')</b>

bot.polling()

But when I click on the button, the text is displayed, but there is no response from the bot!
Where is the mistake?
If needed - t.me/castlewarsbot
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly, 2017-10-06
@danilkapuh

@bot.message_handler(content_types=["text"])
def messages(message):
  if 'шерлок' in message.text.lower() or 'холмс' in message.text.lower():
    bot.send_message(message.chat.id, 'Вы выбрали Шерлока')
  elif 'доктор' in message.text.lower() or 'ватсон' in message.text.lower():
    bot.send_message(message.chat.id, 'Вы выбрали Ватсона')

F
Fast_CD, 2018-02-01
@Fast_CD

Everything is much easier. You have an error in your code:

<b>
def name(m):

Instead of this: "<b>", there should be a handler: @bot.message_handler(content_types=["text"])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question