最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

python - vk-bot - get the file from attachments - Stack Overflow

matteradmin3PV0评论

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
Post a comment

comment list (0)

  1. No comments so far