pyTelegramBotAPI 使用 if/elif/else 结构在消息文本中搜索

问题描述 投票:0回答:2

我对 Python 3.6.5 中 pyTelegramBotAPI 库的行为有疑问。它是一个与 Telegram API 一起使用的库。

这是我的代码:

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)

我的问题 - 我可以选择任何选项,但 qnswer 始终是“如果”。为什么我无法使用 if/elif/else 来管理答案?我该如何修复它?我需要使用正则表达式吗(请不要)?

感谢您的关注。

python telegram-bot py-telegram-bot-api
2个回答
0
投票

非常简单,只需更改即可:

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

if text == 'If':

0
投票

?????????????????????????????????????????????????????????????????????????????? if (text == 'If') 或 (text == 'if'): 代码... elif (text == 'elif') 或 (text == 'Elif'): 代码...

هك ك ف , ق ق ف ف ه ك ف ف ف ف ف ف ف ف ف ف ف

© www.soinside.com 2019 - 2024. All rights reserved.