0

我有一种情况,我需要实现 2 个回调查询处理程序,但为了执行第二个回调查询,我需要将回调查询数据发送到第一个处理程序,任何人都可以帮忙我还将包括必要的函数以便更好地解释


def build_key(name):
    url = "https://api.covid19india.org/v4/min/data.min.json"
    re = requests.get(url).json()
    val = re[name]["districts"]
    print(type(val))

    data = list(val.items())
    keyboard = []
    for i in range(0, len(data), 2):
        try:
            keyboard = keyboard + \
                [[InlineKeyboardButton(data[i][0], callback_data=data[i][0]),
                  InlineKeyboardButton(data[i+1][0], callback_data=data[i+1][0])]]
        except IndexError:
            keyboard = keyboard + \
                [[InlineKeyboardButton(data[i][0], callback_data=data[i][0])]]

    return keyboard

# send the inline keyboard for state selection

@app.on_message(filters.command("dis") & filters.private)
def send(client, message):
    message.reply_text("Select State",
                       reply_markup=InlineKeyboardMarkup(Const.state_keyboard))

# send the inline keyboard for district selection based on the previous selection

@app.on_callback_query()
def state_answer(client, callback_query):
    state_name = callback_query.data # Here State name is the callback_query data of /dis command inline keyboard
    callback_query.edit_message_text("Select District",
                                     reply_markup=InlineKeyboardMarkup(build_key(state_name)))

# send the data for the selected district

@app.on_callback_query()
def dist_answer(client, callback_query):
    dist_name = callback_query.data
    url = "https://api.covid19india.org/v4/min/data.min.json"
    re = requests.get(url).json()
    val = re[state_name]["districts"][dist_name] # Here with the callbackquery_data of state_answer function I need state_name also which is the callback_query data of send Fuction
    callback_query.edit_message_text(val)

app.run()

'''---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'''

# Const.state_keyboard can be found here

https://pastebin.com/5tiSy0Tt



4

0 回答 0