A
A
Astraithios2021-03-07 19:35:02
Python
Astraithios, 2021-03-07 19:35:02

How to make the bot "answer" when the user clicks on a certain element in the keyboard (telebot)?

We want the bot to respond when the 'Give me a random number, please' button is pressed on a certain message.
The difficulty is that I don't know how to get the bot to respond to a button click.
The code:

import telebot
import random

keyboard1 = telebot.types.ReplyKeyboardMarkup(True, True)
keyboard1.row('Give me a random number, please')
number = random.random()

bot = telebot.TeleBot("APIKEY")

@bot.message_handler(commands=['start'])
def send_welcome(message):
    bot.send_message(message.chat.id, "Hi, this is a bot which can send u a random number \nEnjoy!", reply_markup=keyboard1)
    if message.text.lower() == ("Give me a random number, please"):
        bot.send_message(message.chat.id, number)

bot.polling()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Astraithios, 2021-03-08
@Astraithios

Issue resolved, answer:
Condition converts incoming text to lowercase, but checks with different:
message.text.lower() == ("Give me a random number, please"):
Need
message.text.lower() == ( "give me a random number, please"):

A
Antonio Solo, 2021-03-07
@solotony

you need to display the button, hang the command on the button, add the handler for this command.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question