在pyTelegramBotApi中,可以使用MessageHandler和CallbackQueryHandler来处理多个回调函数。下面是一个示例代码:
import telebot
from telebot import types
bot = telebot.TeleBot("YOUR_API_TOKEN")
@bot.message_handler(func=lambda message: True)
def handle_message(message):
# 处理普通消息
bot.reply_to(message, "这是一条普通消息")
@bot.callback_query_handler(func=lambda call: True)
def handle_callback(call):
if call.data == "button1":
# 处理第一个按钮的回调
bot.answer_callback_query(call.id, "你点击了按钮1")
elif call.data == "button2":
# 处理第二个按钮的回调
bot.answer_callback_query(call.id, "你点击了按钮2")
# 创建两个按钮
button1 = types.InlineKeyboardButton("按钮1", callback_data="button1")
button2 = types.InlineKeyboardButton("按钮2", callback_data="button2")
# 创建一个行内键盘,并将按钮添加到键盘上
keyboard = types.InlineKeyboardMarkup()
keyboard.add(button1, button2)
@bot.message_handler(commands=['start'])
def send_welcome(message):
# 发送一条带有行内键盘的消息
bot.send_message(message.chat.id, "欢迎使用", reply_markup=keyboard)
bot.polling()
在上述代码中,我们创建了两个回调函数,handle_message用于处理普通消息,handle_callback用于处理回调查询。在handle_callback函数中,我们根据回调查询的data属性来判断用户点击了哪个按钮,并作出相应的回应。
在send_welcome函数中,我们发送了一条带有行内键盘的消息,用户可以通过点击按钮来触发回调函数。
这样,我们就可以同时处理多个回调函数了。