management_telegram_bot.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:SmartSlam 作者: Oneiroe 项目源码 文件源码
def on_callback(msg):
    """  Reaction to an interaction with an InLineKeyboard """
    query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query')
    logging.info('Got Callback Query:' + str(query_id) + ' Command:' + query_data + ' From ChatID:' + str(from_id))

    # SWITCH/CASE to identify the call and respond consequently
    if query_data == '/rectify':
        options = [i.split('% ')[1] for i in
                   msg['message']['text'].split('\n')[msg['message']['text'].split('\n').index('DETAILS: ') + 1:]]
        options_buttons = [[InlineKeyboardButton(text=i, callback_data='/rectify_' + i)] for i in options]
        options_buttons.append([InlineKeyboardButton(text='<< Go back', callback_data='/back')])
        keyboard = InlineKeyboardMarkup(
            inline_keyboard=options_buttons)
        bot.editMessageReplyMarkup((from_id, msg['message']['message_id']), reply_markup=keyboard)
    elif '/rectify_' in query_data:
        rectified_classification = query_data[len('/rectify_'):]
        new = msg['message']['text'].split('\n')
        new[2] = 'UPDATED: ' + rectified_classification
        new_message = ''.join('%s\n' % i for i in new)

        sample_name = new[1][6:] + '.wav'
        db_save_access(os.path.join(data['db_path'], 'smartSlamDB.sqlite'),
                       os.path.join(data['db_path'], 'Samples', sample_name),
                       rectified_classification)

        keyboard = InlineKeyboardMarkup(
            inline_keyboard=[[InlineKeyboardButton(text='Rectify classification?', callback_data='/rectify')]]
        )
        bot.editMessageText((from_id, msg['message']['message_id']), new_message, reply_markup=keyboard)

    elif query_data == '/back':
        keyboard = InlineKeyboardMarkup(
            inline_keyboard=[[InlineKeyboardButton(text='Rectify classification?', callback_data='/rectify')]]
        )
        bot.editMessageReplyMarkup((from_id, msg['message']['message_id']), reply_markup=keyboard)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号