Telebot InlineKeyboardMarkup回调

时间:2017-09-23 13:11:09

标签: python callback telegram-bot

我想通过InlineKeyboardMarkup在函数中发送数据:

@bot.callback_query_handler(func=lambda c: 'room_' in c.data)
def call_back_room(c):
    ...
    foo(c.message)

def foo(message):
    photo_mess = bot.send_message(message.chat.id, text='Send a photo')
    photo_id = photo_mess.photo[-1].file_id #Error NonType...

2 + ((4 / 2) * 3)

为什么register_next_step_handler或第二个变体在回调函数中不起作用? 我怎样才能发送像id这样的数据?

3 个答案:

答案 0 :(得分:1)

from telebot import types

@bot.message_handler(commands=['start'])
def any_msg(message):
    keyboard = types.InlineKeyboardMarkup()
    callback_button = types.InlineKeyboardButton(text="1", callback_data="1")
    keyboard.add(callback_button)
    bot.send_message(message.chat.id, "Я – сообщение из обычного режима", reply_markup=keyboard)

@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    if call.message:
        if call.data == "1":
            keyboard = types.InlineKeyboardMarkup(row_width=3)
            a = types.InlineKeyboardButton(text=" ", callback_data="2")
            b = types.InlineKeyboardButton(text=" ", callback_data="2")
            c = types.InlineKeyboardButton(text=" ", callback_data="2")
            d = types.InlineKeyboardButton(text=" ", callback_data="2")
            e = types.InlineKeyboardButton(text=" ", callback_data="2")
            f = types.InlineKeyboardButton(text=" ", callback_data="2")
            g = types.InlineKeyboardButton(text=" ", callback_data="2")
            h = types.InlineKeyboardButton(text=" ", callback_data="2")
            i = types.InlineKeyboardButton(text=" ", callback_data="2")
            keyboard.add(a, b, c, d, e, f, g, h, i)
            bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="X", reply_markup=keyboard)
        if call.data == "2":
            keyboard = types.InlineKeyboardMarkup(row_width=3)
            a = types.InlineKeyboardButton(text="X", callback_data="3")
            b = types.InlineKeyboardButton(text=" ", callback_data="3")
            c = types.InlineKeyboardButton(text=" ", callback_data="3")
            d = types.InlineKeyboardButton(text=" ", callback_data="3")
            e = types.InlineKeyboardButton(text=" ", callback_data="3")
            f = types.InlineKeyboardButton(text=" ", callback_data="3")
            g = types.InlineKeyboardButton(text=" ", callback_data="3")
            h = types.InlineKeyboardButton(text=" ", callback_data="3")
            i = types.InlineKeyboardButton(text=" ", callback_data="3")
            keyboard.add(a, b, c, d, e, f, g, h, i)
            bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="0", reply_markup=keyboard)
        if call.data == "3":
            keyboard = types.InlineKeyboardMarkup(row_width=3)
            a = types.InlineKeyboardButton(text=" ", callback_data="1")
            b = types.InlineKeyboardButton(text=" ", callback_data="1")
            c = types.InlineKeyboardButton(text=" ", callback_data="1")
            d = types.InlineKeyboardButton(text=" ", callback_data="1")
            e = types.InlineKeyboardButton(text="0", callback_data="1")
            f = types.InlineKeyboardButton(text=" ", callback_data="1")
            g = types.InlineKeyboardButton(text=" ", callback_data="1")
            h = types.InlineKeyboardButton(text=" ", callback_data="1")
            i = types.InlineKeyboardButton(text=" ", callback_data="1")
            keyboard.add(a, b, c, d, e, f, g, h, i)
            bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="X", reply_markup=keyboard)

答案 1 :(得分:0)

我没有找到回调解决方案。 我的解决方案:

def choosing_room(message):
    ...
    message = bot.send_message(message.chat.id, text=rooms_str)
    bot.register_next_step_handler(message, room_selected)

def room_selected(message):
    photo_mess = bot.send_message(message.chat.id, text='Send photo')
    # Use functools.partial
    bot.register_next_step_handler(photo_mess, partial(add_player_photo, room_id))

def add_player_photo(room_id, message):
    ...

答案 2 :(得分:0)

这是我“希望我正在回答您的问题”应用于我的漫游器的内容:

#Start_command/s for your bot 
@bot.message_handler(commands=['start', 'help', 'hi', ... ]) #...Anycommand_text_etc..._you_want])
def cmd_start(message):
    chat_id = message.chat.id
    bot.send_message(
            chat_id,\
            '''X Bot At Your Sevice''', parse_mode='HTML') 
# Some bot functionality of your choice
@bot.message_handler(func=lambda message: True) #...[You can also do content type here]
def do_do_sth(message):
    bot.send_message(
            chat_id,\
            '''X Bot At Your Sevice''', parse_mode='HTML', reply_markup=x_keyboard())

    # or ... Do your thing.

# Callback_Handler
@bot.callback_query_handler(func=lambda call: True)
def callback_handler(call):
    if call.data == 'do_sth':
        do_do_sth(call.message)

# Your inline keyboard
def x_keyboard():
    markup = types.InlineKeyboardMarkup()
    btn = types.InlineKeyboardButton(\
    'xxx', callback_data='do_sth')
    markup.row(btn, btn1)
    return markup


Run your bot.