S
S
Setaz2132020-12-30 15:27:56
Python
Setaz213, 2020-12-30 15:27:56

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'})

PS I consider the last two lines to be equivalent!
So, in any of the methods for sending a message (through the vk_api library, and through the POST method), if you use them from a group token, an empty message is sent or (on a smartphone) '_(content deleted)_'.
Line of code:
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'})

5e09ece140cbe247207508.jpeg
Everything would be fine, but when sending through the user - everything is sent without problems!
Line of code:
vk2.messages.send(peer_id = (event.object.peer_id),random_id = (0),attachment='doc483977513_529842114_f37d87b7102d22013c',message='LOL')

5e09ed0faf544651528810.jpeg
Changing tokens, in all cases, the user sends the GS, and the group is empty!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2020-12-30
@Setaz213

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)

I made a kst translator for audio, sort of like you, so if you're interested, here's the code: https://pastebin.com/hCf8ZeDd

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question