用按钮的电信电报消息

发布于 2025-02-12 15:31:07 字数 318 浏览 1 评论 0原文

我正在尝试创建一个新闻电报机器人。但是II无法发送带有交互式按钮的消息。

我之所以使用按钮是为了制作菜单,如果您可以显示一个互动菜单而不是仅添加它的示例,我将不胜感激。

使用语言:Python 3.9& 像这样的电信库

“在此处输入图像描述”

I'm trying create a news telegram bot. But iI can't send messages with interactive buttons.

The reason why I use the buttons is to make a menu, I would appreciate if you could show an example of making an interactive menu instead of just adding it.

Using Language : Python 3.9 & Telebot Library

Like this:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无所谓啦 2025-02-19 15:31:07

telebot 为InlineKeyboard提供以下类型:


一个基本示例看起来

from telebot import TeleBot
from telebot import types

bot = TeleBot("859163076:A......")
chat_id = 12345

button_foo = types.InlineKeyboardButton('Foo', callback_data='foo')
button_bar = types.InlineKeyboardButton('Bar', callback_data='bar')

keyboard = types.InlineKeyboardMarkup()
keyboard.add(button_foo)
keyboard.add(button_bar)

bot.send_message(chat_id, text='Keyboard example', reply_markup=keyboard)

将导致以下消息发送到chat_id

”输入图像描述在这里”

Telebot provides the following types for InlineKeyboard:


An basic example would look like

from telebot import TeleBot
from telebot import types

bot = TeleBot("859163076:A......")
chat_id = 12345

button_foo = types.InlineKeyboardButton('Foo', callback_data='foo')
button_bar = types.InlineKeyboardButton('Bar', callback_data='bar')

keyboard = types.InlineKeyboardMarkup()
keyboard.add(button_foo)
keyboard.add(button_bar)

bot.send_message(chat_id, text='Keyboard example', reply_markup=keyboard)

Which will result in the following message send to chat_id:

enter image description here

So尛奶瓶 2025-02-19 15:31:07

在此示例中,我们创建一个带有两个按钮的菜单,“按钮1”和“按钮2”,然后分别处理每个按钮。当用户选择一个按钮时,机器人发送相应的消息。

    import telebot
    from telebot import types
    
    bot = telebot.TeleBot('YOUR_BOT_TOKEN')
    
    #Command /start
    @bot.message_handler(commands=['start'])
    def start(message):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item1 = types.KeyboardButton("Bottom 1")
        item2 = types.KeyboardButton("Bottom 2")
        markup.add(item1, item2)
    
        bot.send_message(message.chat.id, "Select option:", reply_markup=markup)
    
    #Bottom 1
    @bot.message_handler(func=lambda message: message.text == "Bottom 1")
    def button1(message):
        bot.send_message(message.chat.id, "U select bottom 1")
    
    #Bottom 2
    @bot.message_handler(func=lambda message: message.text == "Bottom 2")
    def button2(message):
        bot.send_message(message.chat.id, "U select bottom 2")
    
    #Start bot
    if __name__ == '__main__':
        bot.polling(none_stop=True)

In this example, we create a menu with two buttons, "Button 1" and "Button 2", and process each button separately. When the user selects one of the buttons, the bot sends the corresponding message.

    import telebot
    from telebot import types
    
    bot = telebot.TeleBot('YOUR_BOT_TOKEN')
    
    #Command /start
    @bot.message_handler(commands=['start'])
    def start(message):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item1 = types.KeyboardButton("Bottom 1")
        item2 = types.KeyboardButton("Bottom 2")
        markup.add(item1, item2)
    
        bot.send_message(message.chat.id, "Select option:", reply_markup=markup)
    
    #Bottom 1
    @bot.message_handler(func=lambda message: message.text == "Bottom 1")
    def button1(message):
        bot.send_message(message.chat.id, "U select bottom 1")
    
    #Bottom 2
    @bot.message_handler(func=lambda message: message.text == "Bottom 2")
    def button2(message):
        bot.send_message(message.chat.id, "U select bottom 2")
    
    #Start bot
    if __name__ == '__main__':
        bot.polling(none_stop=True)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文