Работаю с вк-апи, нужно сохранять на пк фото/голосовые сообщения и отправлять его юзеру.
Вот код. Заранее спасибо.
- *- coding: utf- 8 - *-
import vk_api
from vk_api import VkUpload
from vk_api.longpoll import VkLongPoll, VkEventType
from vk_api.utils import get_random_id
import requests
def write_message(sender, message):
authorize.method('messages.send', {'user_id': sender, 'message': message, 'random_id': get_random_id(),
'attachment': ','.join(attachments)})
attachments = []
authorize = vk_api.VkApi(
token=token, api_version='5.131')
vk = authorize.get_api()
longpoll = VkLongPoll(authorize)
upload = VkUpload(authorize)
for event in longpoll.listen():
if event.attachments is None or event.attachments != {}:
if event.type == VkEventType.MESSAGE_NEW and event.to_me and event.text:
reseived_message = event.text.lower()
sender = event.user_id
upload_image = upload.photo_messages(photos=image)[0]
b = vk.messages.getHistoryAttachments(peer_id=-200082811, media_type="photo")
url = vk.photos.getMessagesUploadServer(peer_id=743749117)
my_file = requests.get(url)
open('my_file.txt', 'wb').write(my_file.content)
attachments.append('photo{}_{}'.format(upload_image['owner_id'], upload_image['id']))
write_message(sender, '12')
if reseived_message == 'привет':
write_message(sender, "добр")
elif reseived_message == 'пока':
write_message(sender, "бобр")
else:
write_message(sender, 'я вас не понимаю')