В общем решением этой проблемы послужило использование метода vk.messages.getById, вместо vk.messages.getHistoryAttachments.
Конечный рабочий код:
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
import requests
import os
import time
group_token = 'токен сообщества'
vk_session = vk_api.VkApi(token=group_token)
vk = vk_session.get_api()
current_dir = os.path.dirname(os.path.abspath(__file__))
id_path = os.path.join(current_dir, 'id.dat')
with open(id_path, 'r') as f:
target_user_id = f.read().strip()
def download_file(url, path):
response = requests.get(url)
with open(path, "wb") as f:
f.write(response.content)
def get_photo_link(photo):
for size in photo['sizes']:
if size['type'] == 'm':
return size['url']
def download_photo(photo, path):
url = get_photo_link(photo)
filename = os.path.basename(path)
current_dir = os.path.dirname(path)
extension = os.path.splitext(filename)[1]
photo_path = os.path.join(current_dir, f'file{extension}')
download_file(url, photo_path)
filename = 'messages.txt'
path = os.path.join(os.getcwd(), filename)
longpoll = VkLongPoll(vk_session)
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW and event.from_user and str(event.user_id) == target_user_id:
if 'ключевое_слово' in event.text:
attachments = vk.messages.getById(message_ids=event.message_id, extended=True, fields='attachments')['items'][0]['attachments']
if attachments:
attachment = attachments[0]
if attachment['type'] == 'photo':
photo = attachment['photo']
text = photo.get('text', '')
download_photo(photo, 'photo.jpg')
with open('messages.txt', 'a', encoding='utf-8') as file:
file.write(f'{text}\n')
vk.messages.send(peer_id=event.peer_id, random_id=0, message='Фото успешно загружено и текст сохранен в файле messages.txt')
else:
vk.messages.send(peer_id=event.peer_id, random_id=0, message='Найдено вложение другого типа')
else:
with open('messages.txt', 'w', encoding='utf-8') as file:
file.write(f'{event.text}\n')
vk.messages.send(peer_id=event.peer_id, random_id=0, message='Вложений не обнаружено, но текст сообщения сохранен в файле messages.txt')