I need help with a VK bot. It should take text from a file sent by the user, then receive an image, and finally send a post with the text and image to the group it is connected to. I can't get the file from attachments.
elif current_state[user_id] == 'text' and hasattr(event, 'attachments'):
for attach in event.attachments:
if attach[-5:] == '_type':
continue
tp = event.attachments[attach + '_type']
doc = vk.docs.getById(docs=event.attachments[attach] )[0]
if tp == 'doc' and doc['ext'] == 'txt':
file_url = doc['url']
file_path = download_file(file_url, f"temp_{user_id}.txt")
user_data[user_id]['text'] = get_text_from_file(file_path)
os.remove(file_path)
vk.messages.send(user_id=user_id, message="Файл получен! Теперь отправьте изображение.", random_id=0)
current_state[user_id] = 'image'
return
The error I get:
Traceback (most recent call last):
File "/Users/kamillasmidt/Newsletter service /VK/vk_bot.py", line 90, in <module>
main()
~~~~^^
File "/Users/kamillasmidt/Newsletter service /VK/vk_bot.py", line 87, in main
handle_messages(event, vk, GROUP_ID)
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
File "/Users/kamillasmidt/Newsletter service /VK/vk_bot.py", line 49, in handle_messages
doc = vk.docs.getById(docs=event.attachments[attach] )[0]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range