Держи
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
token = ""
session = requests.Session()
auth = vk_api.VkApi(token=token, api_version='5.131')
vk = auth.get_api()
long_poll = VkLongPoll(auth)
def upload_audio_msg(peer_ids):
upload = VkUpload(vk)
audio_msg = upload.audio_message('filename.mp3', peer_id=peer_ids)
attachment = 'doc{}_{}'.format(audio_msg['audio_message']['owner_id'], audio_msg['audio_message']['id'])
vk.messages.send(peer_id=peer_ids, attachment=attachment, random_id=get_random_id())
while True:
try:
for event in long_poll.listen():
if event.type == VkEventType.MESSAGE_NEW:
peer_id = event.peer_id
if event.from_me:
if event.text == 'test':
upload_audio_msg(peer_id)
except Exception as er:
print(er)