Answer the question
In order to leave comments, you need to log in
How to fix the problem of sending a voice message to VK using vk_api and requests?
Here is the code:
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
import vk_api
from urllib.request import urlretrieve, urlopen
from gtts import gTTS
import requests
import json
import tempfile
import subprocess
vk = vk_api.VkApi(token="токен группы")
vk._auth_token()
longpoll = VkBotLongPoll(vk, 185856063)
vk = vk.get_api()
vk2 = vk_api.VkApi(token='токен человека')
vk2._auth_token()
vk2 = vk2.get_api()
while True:
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
if event.object.peer_id != event.object.from_id:
print(event,'\n')
if event.object.text.lower() == "тест":
text2 = 'lol'
tts = gTTS(text=text2, lang='en')
name = "say.ogg"
tts.save(name)
audio = open('say.ogg','rb')
a=vk2.docs.getMessagesUploadServer(type='audio_message')
print(a,'\n')
b = requests.post(a['upload_url'], files={'file':audio}).json()
print(b,'\n')
c = vk2.docs.save(file=b['file'])
docum = 'doc%s_%s_%s'%(c['audio_message']['owner_id'],c['audio_message']['id'],c['audio_message']['access_key'])
print(docum,'\n')
vk2.messages.send(peer_id = (event.object.peer_id),random_id = (0),attachment='doc483977513_529842114_f37d87b7102d22013c',message='LOL')
requests.post('https://api.vk.com/method/messages.send',data={'peer_id':event.object.peer_id,'attachment':'doc483977513_529842114_f37d87b7102d22013c','random_id':0,'access_token':'токен','v':'5.64'})
requests.post('https://api.vk.com/method/messages.send',data={'peer_id':event.object.peer_id,'attachment':'doc483977513_529842114_f37d87b7102d22013c','random_id':0,'access_token':'токен','v':'5.64'})
vk2.messages.send(peer_id = (event.object.peer_id),random_id = (0),attachment='doc483977513_529842114_f37d87b7102d22013c',message='LOL')
Answer the question
In order to leave comments, you need to log in
I have this code that works:
vk_session = vk_api.VkApi(
token='токен группы')
vk = vk_session.get_api()
Alpha = vk.docs.getMessagesUploadServer(type='audio_message', peer_id='id моей страницы')
with open('MyMusic.ogg', 'rb') as file:
files = {
'file': file
}
file = requests.post(Alpha['upload_url'], files=files).json()['file']
audio_msg = vk.docs.save(file=file, title=time.time(), )
string = 'doc' + str(audio_msg['audio_message']['owner_id']) + '_' + str(
audio_msg['audio_message']['id'])
vk.messages.send(random_id=time.time(), peer_id=event, message='Держи', attachment=string)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question