首页 > 解决方案 > pyTelegramBotAPI search in text of the message with if/elif/else construction

问题描述

I have question about behaviuor of pyTelegramBotAPI library with Python 3.6.5. It is a libray to work with Telegram API.

There is my code:

import telebot
from telebot.types import Message
from telebot import types

TOKEN = 'TOCKEN_HERE'
bot = telebot.TeleBot(TOKEN)

markup = types.ReplyKeyboardMarkup()
itembtn_privet = types.KeyboardButton('If')
itembtn_more = types.KeyboardButton('Elif')
itembtn_else = types.KeyboardButton('Else')
markup.row(itembtn_privet, itembtn_more, itembtn_else)
bot.send_message(chat_id_here, "Choose one option:", reply_markup=markup)


@bot.message_handler(commands=['start', 'help'])
def command_handler(message: Message):
    bot.reply_to(message, 'The is no answer, sorry(')


@bot.edited_channel_post_handler(content_types=['text'])
@bot.message_handler(content_types=['text'])
def echo_messages(message: Message):
    text = message.text
    if text == 'If' or 'if':
         bot.reply_to(message, 'If')
    elif text == 'Elif' or 'elif':
         bot.reply_to(message, f'Elif')
    else:
         bot.reply_to(message, 'Else')

bot.polling(timeout=60)

My question - I can choise any option, but qnswer always be "If". Why I can't manage answer with if/elif/else? How can I fix it? Do I need to use regexp (please no)?

Thank you for your attention.

标签: pythontelegram-botpy-telegram-bot-api

解决方案


这很简单,只需更改:

if text == 'If' or 'if':

if text == 'If':

推荐阅读