I use library vk_api

In the community formed a key of the form 6478d5d10afd6a8 ************* 4d331053206551 ********* a61e600e4be ******

 vk_session = vk_api.VkApi(token='6478d5d10afd6a8*************4d331053206551*********a61e600e4be******', scope='messages') vk = vk_session.get_api() 

Then I get the data from the request:

 peer_id = data['object']['peer_id'] message_id = data['object']['conversation_message_id'] 

And make a request to edit the message

 return vk.messages.edit(peer_id=peer_id, message_id=message_id, message="some text") 

The problem is that when calling messages.edit, VK returns an error - [15] Access denied

Why? I do not understand, because the following was described in the documentation:

You can get the community access key in one of these ways: In the community settings interface. To do this, simply open the “Community Management” section (“Manage the page” if you have a public page), select the “Work with API” tab and click “Create access key”.

And in the description of the method itself was required only:

enter image description here

The key is, I also received the access rights by specifying scope='messages'

Help please, what is my mistake? Below I will provide all the code and examples of requests.

  @csrf_exempt def vk(request): vk_session = vk_api.VkApi(token='6478d5d10afd6a8*************4d331053206551*********a61e600e4be******', scope='messages') vk = vk_session.get_api() try: data = json.loads(request.body.decode('utf-8')) print(data) if data['type'] == 'message_new' and data['secret'] == 'wqdh8q7wdh7qwu': try: user_id = data['object']['from_id'] peer_id = data['object']['peer_id'] message_id = data['object']['conversation_message_id'] text = data['object']['text'] try: payload = data['object']['payload'].replace('"', '') except KeyError: payload = "" except AttributeError: pass else: return HttpResponseForbidden() def edit_message(peer_id, message_id, message, keyboard=None): return vk.messages.edit(peer_id=user_id, message_id=message_id, message=message, keyboard=keyboard) def send_message(user_id, message, keyboard=None): return vk.messages.send(user_id=user_id, message=message, keyboard=keyboard) if_exist = BotUser.objects.filter(vk_chat_id=user_id).count() if if_exist != 0: user = BotUser.objects.get(vk_chat_id=user_id) if text == 'PING': user.step = 'edit' user.save() keyboard = VkKeyboard() keyboard.add_button(label='q', payload="a") vk.messages.send(user_id=user_id, message='PONG', keyboard=keyboard.get_keyboard()) return HttpResponse('OK') if user.step == 'edit' and text: edit_message(peer_id, message_id, "kekekekeke") return HttpResponse('OK') 

Accept outgoing request to the server:

 {'type': 'message_new', 'object': {'date': 1534675493, 'from_id': 370459341, 'id': 762, 'out': 0, 'peer_id': 370459341, 'text': 'qwerty', 'conversation_message_id': 763, 'fwd_messages': [], 'important': False, 'random_id': 0, 'attachments': [], 'is_hidden': False}, 'group_id': 170000027, 'secret': 'jjsdjkcsjkcsjkcsjk'} 
  • Why do you use data['object']['conversation_message_id'] rather than data['object']['id'] ? - Let's say Pie
  • @ Let'ssayPie I indicated both options, unfortunately the error did not change. The code by the time I understand it does not reach yet, because at the start it refuses access - Milkiweed Gtlt

0