как к кнопке в телеграм боте прикрутить выполнение команды
есть код, не пойму как прикрутить выполнение команды, при нажатии на кнопку
import sqlite3
import config
import datetime
import telebot
from telebot import types
today = datetime.date.today()
tomorrow = today + datetime.timedelta(days = 1)
today = today.strftime("%d-%m-%Y")
tomorrow = tomorrow.strftime("%d-%m-%Y")
bot = telebot.TeleBot(config.TOKEN)
keyboard_markup = types.ReplyKeyboardMarkup(row_width=2)
btn_today = types.KeyboardButton('today')
btn_tomorrow = types.KeyboardButton('tomorrow')
keyboard_markup.add(btn_today, btn_tomorrow)
@bot.message_handler(commands=["today"])
def add_user_handler(message):
con = sqlite3.connect('schedule.db')
cursor = con.cursor()
sql = ("SELECT * FROM 'schedule' WHERE date=?")
today_sql = (today,)
cursor.execute(sql, today_sql)
today_schedule = cursor.fetchone()
clear_schedule = ''
while today_schedule is not None:
clear_schedule += ("text" + '\n' +
"text: " + str(today_schedule[0]) + '\n' +
"text: "+ str(today_schedule[1]) + '\n' +
"text: " + str(today_schedule[3]) + '\n' +
"text: " + str(today_schedule[4]) + '-' + (today_schedule[5]) + '\n' +
"text: " + str(today_schedule[6]))
today_schedule = cursor.fetchone()
bot.send_message(message.chat.id, clear_schedule, reply_markup=keyboard_markup)
bot.polling(none_stop=True)
Ответы (2 шт):
Автор решения: D. Violet
→ Ссылка
при нажатии на types.KeyboardButton() в чат от имени пользователя отправляется значение этой кнопки. бот выполняет команды через /, например /start, поэтому:
@bot.message_handler(commands=['start'])
def start(message):
keyboard_markup = types.ReplyKeyboardMarkup(row_width=2)
btn_today = types.KeyboardButton('/today')
btn_tomorrow = types.KeyboardButton('tomorrow')
keyboard_markup.add(btn_today, btn_tomorrow)
bot.send_message(message.chat.id, 'clear_schedule', reply_markup=keyboard_markup)
@bot.message_handler(commands=["today"])
def add_user_handler(message):
bot.send_message(message.chat.id, 'ну вот тебе и today')
либо отлавливать как текст:
@bot.message_handler(content_types=['text'])
def blabla(message):
if message.text == 'tomorrow':
bot.send_message(message.chat.id, 'ну вот тебе и tomorrow')
Автор решения: Владислав Студеникин
→ Ссылка
Можешь поменять меседж хендлер так чтобы он работал только при определенном тексте. Вот пример:
@bot.message_handler(func=lambda message: message.text == "Оставить почту и отзыв").
Reply кнопка "Оставить почту и отзыв"