M
M
MyrrhTree2021-06-13 12:33:24
Python
MyrrhTree, 2021-06-13 12:33:24

How to attach a photo to a list item in python?

I am writing a chatbot for telegram. When you click on the button, a random element of the list is displayed (prediction for the day using Tarot cards). It is necessary that not only the list element is displayed, but also the map photo. What team can do this?
60c5d0da6f5de291655844.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-06-13
@MyrrhTree

firstly, you don’t need to name variables with PL keywords ( list )
secondly, in this case, the data structure is better suited - a dictionary

from pprint import pprint
cards = {'card_' + str(x): {'name': x, 'desc': x, 'photo': x} for x in range(1,22)}
cards['card_21']['name'] = 'Мир'
cards['card_21']['desc'] = 'Завершенность бла бла бла'
cards['card_21']['photo'] = 'C:/Tools/tmp/card_21.png'
pprint(cards)
{'card_1': {'desc': 1, 'name': 1, 'photo': 1},
 'card_10': {'desc': 10, 'name': 10, 'photo': 10},
 'card_11': {'desc': 11, 'name': 11, 'photo': 11},
 'card_12': {'desc': 12, 'name': 12, 'photo': 12},
 'card_13': {'desc': 13, 'name': 13, 'photo': 13},
 'card_14': {'desc': 14, 'name': 14, 'photo': 14},
 'card_15': {'desc': 15, 'name': 15, 'photo': 15},
 'card_16': {'desc': 16, 'name': 16, 'photo': 16},
 'card_17': {'desc': 17, 'name': 17, 'photo': 17},
 'card_18': {'desc': 18, 'name': 18, 'photo': 18},
 'card_19': {'desc': 19, 'name': 19, 'photo': 19},
 'card_2': {'desc': 2, 'name': 2, 'photo': 2},
 'card_20': {'desc': 20, 'name': 20, 'photo': 20},
 'card_21': {'desc': 'Завершенность бла бла бла',
             'name': 'Мир',
             'photo': 'C:/Tools/tmp/card_21.png'},
 'card_3': {'desc': 3, 'name': 3, 'photo': 3},
 'card_4': {'desc': 4, 'name': 4, 'photo': 4},
 'card_5': {'desc': 5, 'name': 5, 'photo': 5},
 'card_6': {'desc': 6, 'name': 6, 'photo': 6},
 'card_7': {'desc': 7, 'name': 7, 'photo': 7},
 'card_8': {'desc': 8, 'name': 8, 'photo': 8},
 'card_9': {'desc': 9, 'name': 9, 'photo': 9}}

As you can see from the code above, the 'photo' key and the path to the file value are added to the dictionary.
and then you need to transfer it through the bot after reading the file
with open(cards['card_21']['photo'], "rb") as file:
    data = file.read()
bot.send_photo(message.from_user.id, photo=data)

but before these you need to write a function that will take an argument - the card ID and substitute it in open ()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question