Search code examples
pythontelegrampy-telegram-bot-api

Telegram bot, Response to button click not sending


I'm making a telegram bot formula assistant. There is a problem that when you press the "Mechanics" button, the bot responds, but when you press "Electrical and magnetic phenomena" it does not, what should I do? I'm still a beginner if anything!

import telebot
import random
from telebot import types
import requests


token = ("this token")
bot = telebot.TeleBot(token)
@bot.message_handler(commands=["start"])
def start(message: types.Message):
    bot.send_message(
        message.chat.id,
        text="Привет, меня зовут Гена и я твой личный помощник по физике!".format(message.from_user))
    bot.send_message(
        message.chat.id,
        text="Пока что я умею только показывать нужные тебе формулы, но скоро я научусь чему-то новому! Используй команду /formuls !".format(message.from_user),reply_markup=types.ReplyKeyboardRemove())

@bot.message_handler(commands=["formuls"])
def formuls(message: types.Message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    btn1 = types.KeyboardButton("🔍Механика🔍")
    btn2 = types.KeyboardButton("⚡️Электрические и магнитные явления💡")
    markup.add(btn1, btn2)
    bot.send_message(
        message.chat.id,
        text= "Выбирай раздел!".format(message.from_user),reply_markup=markup)

@bot.message_handler(func=lambda msg: msg.text == "🔍Механика🔍")
def message(message: types.Message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    btn1 = types.KeyboardButton("️➡️Прямолинейное равномерное движение➡️")
    btn2 = types.KeyboardButton("➡️Прямолинейное равноускоренное движение➡️")
    btn3 = types.KeyboardButton("⬆️Движение вертикально вверх⬆️")
    btn4 = types.KeyboardButton("🔄Движение по окружности🔄")
    btn5 = types.KeyboardButton("🪶Свободное падение🪶")
    btn6 = types.KeyboardButton("👨🏻‍🎓Законы Ньютона👨🏻‍🎓")
    btn7 = types.KeyboardButton("🌎Закон всемирного тяготения🌎")
    markup.add(btn1,btn2,btn3,btn4,btn5,btn6,btn7)
    bot.send_message(
        message.chat.id,
        text = "Какие формулы именно вас инетерсуют?".format(message.from_user),reply_markup=markup
    )

@bot.message_handler(func=lambda msg: msg.text=="️⚡️Электрические и магнитные явления💡")
def messageElec(message: types.Message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    btn1 = types.KeyboardButton("🔌Электростатика🔌")
    btn2 = types.KeyboardButton("💡Законы постоянного тока💡")
    btn3 = types.KeyboardButton("🔌Электрический ток в постоянных средах🔌")
    btn4 = types.KeyboardButton("🧲Магнетизм🧲")
    btn5 = types.KeyboardButton("💡Электромагнитная индукция💡")
    markup.add(btn1, btn2, btn3, btn4, btn5)
    bot.send_message(
        message.chat.id,
        text = "Какие формулы именно вас инетерсуют?".format(message.from_user),reply_markup=markup
    )

@bot.message_handler(func=lambda msg: msg.text == "️➡️Прямолинейное равномерное движение➡️")
def get_user_photo(message: types.Message):
    bot.send_photo(message.chat.id, "https://imgur.com/vwJ5pTM")

@bot.message_handler(func=lambda msg: msg.text == "➡️Прямолинейное равноускоренное движение➡️")
def get_user_photo(message: types.Message):
    bot.send_photo(message.chat.id, "https://imgur.com/datM6mw")

@bot.message_handler(func=lambda msg: msg.text == "⬆️Движение вертикально вверх⬆️")
def get_user_photo(message: types.Message):
    bot.send_photo(message.chat.id, "https://imgur.com/z8JVbJX")

@bot.message_handler(func=lambda msg: msg.text == "🔄Движение по окружности🔄")
def get_user_photo(message: types.Message):
    bot.send_photo(message.chat.id, "https://imgur.com/AGhIy2b")

@bot.message_handler(func=lambda msg: msg.text == "🪶Свободное падение🪶")
def get_user_photo(message: types.Message):
    bot.send_photo(message.chat.id, "https://imgur.com/j1GhqCz")

@bot.message_handler(func=lambda msg: msg.text == "👨🏻‍🎓Законы Ньютона👨🏻‍🎓")
def get_user_photo(message: types.Message):
    bot.send_photo(message.chat.id, "https://imgur.com/UDmMPVS")

@bot.message_handler(func=lambda msg: msg.text == "🌎Закон всемирного тяготения🌎")
def get_user_photo(message: types.Message):
    bot.send_photo(message.chat.id, "https://imgur.com/T3mxRIp")



bot.polling(none_stop=True)

Of all my assumptions, it was to change the name of the function, but it did not bear fruit :(


Solution

  • I took string "️⚡️Электрические и магнитные явления💡" from KeyboardButton() and from msg.text==... and I used == to compare them and I got False - so you have two different strings.

    When I compare char-by-char then one string starts with invisible char with code 65039 and I can't remove it.

    I checked unicode 65039 in Google and it is some emoji variation selector - so all problem made emojis.

    The simplest solution: copy string from one place to another place.


    text1 = "⚡️Электрические и магнитные явления💡"
    text2 = "️⚡️Электрические и магнитные явления💡"
    
    for char1, char2 in zip(text1, text2):
        print(char1, char2, ord(char1), ord(char2), char1==char2)
    

    Result:

    ⚡ ️ 9889 65039 False
     ⚡ 65039 9889 False
    Э ️ 1069 65039 False
    л Э 1083 1069 False
    е л 1077 1083 False
    к е 1082 1077 False
    т к 1090 1082 False
    р т 1088 1090 False
    и р 1080 1088 False
    ч и 1095 1080 False
    е ч 1077 1095 False
    с е 1089 1077 False
    к с 1082 1089 False
    и к 1080 1082 False
    е и 1077 1080 False
      е 32 1077 False
    и   1080 32 False
      и 32 1080 False
    м   1084 32 False
    а м 1072 1084 False
    г а 1075 1072 False
    н г 1085 1075 False
    и н 1080 1085 False
    т и 1090 1080 False
    н т 1085 1090 False
    ы н 1099 1085 False
    е ы 1077 1099 False
      е 32 1077 False
    я   1103 32 False
    в я 1074 1103 False
    л в 1083 1074 False
    е л 1077 1083 False
    н е 1085 1077 False
    и н 1080 1085 False
    я и 1103 1080 False
    💡 я 128161 1103 False