L
L
lleballex2020-07-02 12:40:09
Python
lleballex, 2020-07-02 12:40:09

Why is the keyboard not sent to VK_API?

I'm trying to send the keyboard via vk_api , but no matter how much I try, no matter how I change it, an error about the wrong format constantly pops up. I have already read all the documentation of VK, nothing helped. The code is below (the bot class itself wrote).
Sending a message:

keyboard = {
  'one_time_': True,
  'buttons': [
    [{
      'action': {
        'type': 'text',
        'label': 'BUTTON',
      },
      'color': 'positive',
    }],
  ],
}
bot.send_message(event.user_id, 'MESSAGE', keyboard=keyboard)

The send function itself in the bot class:
def send_message(self, user_id, message, keyboard=None):
    context = {
      'user_id': user_id,
      'random_id': int(random() * 10000000000000000),
      'message': message,
    }
    if keyboard:
      context.update({'keyboard': keyboard})
    self.vk.method('messages.send', context)

The error itself:
vk_api.exceptions.ApiError: [100] One of the parameters specified was missing or invalid: keyboard contains incorrect JSON

Most likely some stupid mistake due to my inattention))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0ralo, 2020-07-02
@lleballex

You can use
the keyboard constructor from the vk_api library.
Yes, and your send function is very complicated, you can do it like this

def send_message(peer_id: Union[str, int], message: str = None, attachment=None, keyboard=None) -> None:
  vk.messages.send(peer_id=peer_id, message=message, random_id=random.randint(-2147483648, +2147483647),
           attachment=attachment, keyboard=keyboard)

S
soremix, 2020-07-02
@SoreMix

If you use the library, why not just take the keyboard from there?
https://github.com/python273/vk_api/blob/master/ex...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question