N
N
Nikita228Gym2021-07-10 23:00:44
Python
Nikita228Gym, 2021-07-10 23:00:44

How to send GS VK?

I don’t know how to send an mp3 file as a voice message to VK
, I have a folder with a file

, my example code is:

def primer():
    commands

for event in longpoll.listen():
    if event.type == VkEventType.MESSAGE_NEW and event.from_me:
        try:
            if event.text == "пример":
                commands ()


I want to do that I could send voice sms, please help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
slikkness, 2021-07-13
@Nikita228gym

Hold

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)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question