0

My bot passes a simple string (query.query) through a function that returns a "big" dictionary with many subdictionaries (each representing "card" data) inside it. As it works right now, all keys and values for all returned subdictionaries get displayed in-line (until pagination limit). So for example, writing "blue" will return the key:value pairs for all subdictionaries whose card name == "blue".

I want the bot to first display inline results for card names and, after the user chooses a card, for the bot to display the rest of the card data, so the user can choose what part to send in chat. This would look like:

  1. User queries the word "blue" and makes card titles for "blue blob", "blue drake", "blue beard" appear in-line
  2. User select "blue beard" from that menu, making bot then display available data for "blue-beard" in-line (stats, attack, etc.)

Here's a snippet of said code. Check the # comment to see where I think it all breaks down

@bot.inline_handler(lambda query: len(query.query) > 3)
def query_card(inline_query):
    temp_names_list = []
    results_list = []
    try:
        sound_dict = scrape(inline_query.query)
        for key, sub_dict in sound_dict.items():
            temp_names_list.append(types.InlineQueryResultArticle(id=key, title=sub_dict['Name'], input_message_content=types.InputTextMessageContent(sub_dict['Name'])))
        bot.answer_inline_query(inline_query.id, temp_names_list, cache_time=1)

#The code breaks down here since I haven't found a way of passing the result to ^ this answer_inline_query into the next part of the loop:

        for key, sub_dict in sound_dict.items():
            for k, v in sub_dict.items():
                if k == ['message']['text']:
                    results_list.append(types.InlineQueryResultArticle(id=key+k, title=k, input_message_content=types.InputTextMessageContent(sub_dict['Name']+"\'s ["+k+"] bit:\n"+v))) 
                #results_list.append(types.InlineQueryResultVoice(id=k, voice_url=v, title="^ "+sound_dict['Name']+"\'s ["+k+"] bit:\n"+v, caption=sound_dict['Name']+"\'s ["+k+"] bit"))
        bot.answer_inline_query(inline_query.id, results_list, cache_time=1)
    except Exception as e:
            print(e)
Mike R
  • 1
  • 2
  • `if k == ['message']['text']:` can't be right. It should result in `TypeError: list indices must be integers, not str` – jDo Aug 08 '17 at 23:38
  • Yes. That's exactly what happens. I'm thinking there should be a way to change the variable into something output by the chosen result to answer_inline_query – Mike R Aug 08 '17 at 23:48
  • The first part, `['message']`, creates a list on the fly containing a single element, i.e. the string "message", and the second part, `['text']`, attempts to retrieve an element from this list using a string/key. This fails since lists can only be accessed by index/numbers. Hence, `list indices must be integers, not str`. What should `if k == ['message']['text']:` actually do? Is it supposed to be a dictionary lookup in `sub_dict`? – jDo Aug 09 '17 at 00:00
  • `if k == ['message']['text']:` is meant to match the user's chosen card to the key in the matching sub_dictionary. In other words, what I need is to find something that replaces ['message']['text'] and is checked by k, to return the corresponding variables in the `results_list.append()` line – Mike R Aug 09 '17 at 00:11
  • so I imagine it must be something along the lines of `ChosenInlineResult.query` that should go there – Mike R Aug 09 '17 at 00:13
  • I have no idea what all those custom (telegram-specific?) objects in your code do. I just know that `if k == ['message']['text']:` is not valid python and will always fail. It would work as a dictionary lookup, e.g. `my_dict['message']['text']` – jDo Aug 09 '17 at 00:19

0 Answers0