【问题标题】:Python Telegram Bot (Telebot)Python电报机器人(Telebot)
【发布时间】:2021-06-18 22:24:39
【问题描述】:

我正在使用库 https://github.com/eternnoir/pyTelegramBotAPI 构建一个 Telegram 机器人

我试图让当我按下菜单按钮时,它会向用户发送一条消息。

我该怎么做?

import telebot
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton

TELEGRAM_TOKEN = '<TOKEN>'

bot = telebot.TeleBot(TELEGRAM_TOKEN)

def gen_markup():
    markup = InlineKeyboardMarkup()
    markup.row_width = 2
    markup.add(InlineKeyboardButton("Yes", callback_data="cb_yes"),
                               InlineKeyboardButton("No", callback_data="cb_no"))
    return markup

@bot.callback_query_handler(func=lambda call: True)
def callback_query(call):
    if call.data == "cb_yes":
        bot.answer_callback_query(call.id, "Answer is Yes")
    elif call.data == "cb_no":
        bot.answer_callback_query(call.id, "Answer is No")

@bot.message_handler(func=lambda message: True)
def message_handler(message):
    bot.send_message(message.chat.id, "Yes/no?", reply_markup=gen_markup())

bot.polling(none_stop=True)

【问题讨论】:

    标签: python bots telegram


    【解决方案1】:

    您可以使用call对象的数据作为发送者的用户ID(call.from_user.id)发送消息:

    if call.data == "cb_yes":
        bot.answer_callback_query(call.id, "Answer is Yes")
        bot.send_message(call.from_user.id,"Your answer was Yes!")
    elif call.data == "cb_no":
        bot.answer_callback_query(call.id, "Answer is No")
        bot.send_message(call.from_user.id,"Your answer was No!")
    

    【讨论】:

      猜你喜欢
      • 2020-10-28
      • 1970-01-01
      • 2017-08-18
      • 2022-01-01
      • 2021-02-21
      • 2022-12-18
      • 2017-09-11
      • 2020-09-25
      • 2018-02-27
      相关资源
      最近更新 更多