T
T
The-TS2019-09-14 19:47:45
Python
The-TS, 2019-09-14 19:47:45

How to solve VK bot error?

When running the code below, it gives an error:
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\VK.py", line 97, in
write_msg(event.user_id, "I didn't understand your answer. ..")
TypeError: write_msg() missing 1 required positional argument: 'keyboard'
Here is the code:

import vk_api
import json
from vk_api.longpoll import VkLongPoll, VkEventType

def write_msg(peer_id, message, keyboard):
    keyboard = {
"one_time": False,
"buttons": [
    [{
        "action": {
            "type": "location",
            "payload": "{\"button\": \"1\"}"
        }
    }],
    [{
        "action": {
            "type": "open_app",
            "app_id": 6979558,
            "owner_id": -181108510,
            "hash": "sendKeyboard",
            "label": "Отправить клавиатуру"
        }
    }],
    [{
        "action": {
            "type": "vkpay",
            "hash": "action=transfer-to-group&group_id=181108510&aid=10"
        }
    }],
    [{
            "action": {
                "type": "text",
                "payload": "{\"button\": \"1\"}",
                "label": "Negative"
            },
            "color": "negative"
        },
        {
            "action": {
                "type": "text",
                "payload": "{\"button\": \"2\"}",
                "label": "Positive"
            },
            "color": "positive"
        },
        {
            "action": {
                "type": "text",
                "payload": "{\"button\": \"2\"}",
                "label": "Primary"
            },
            "color": "primary"
        },
        {
            "action": {
                "type": "text",
                "payload": "{\"button\": \"2\"}",
                "label": "Secondary"
            },
            "color": "secondary"
        }
    ]
]
}
    keyboard = json.dumps(keyboard)
    vk.method("messages.send", {"peer_id": peer_id, "message": message, "random_id": 0, "keyboard":keyboard})
    request = event.text.lower()

# API-ключ созданный ранее
token = "secret_token"

# Авторизуемся как сообщество
vk = vk_api.VkApi(token=token)

# Работа с сообщениями
longpoll = VkLongPoll(vk)


# Основной цикл
for event in longpoll.listen():

    # Если пришло новое сообщение
    if event.type == VkEventType.MESSAGE_NEW:

        # Если оно имеет метку для меня( то есть бота)
        if event.to_me:

            # Сообщение от пользователя
            request = event.text

            # Каменная логика ответа
            if request == "Stsrt":
                write_msg(event.user_id, "Хай")
            elif request == "пока":
                write_msg(event.user_id, "Пока((")
            else:
                write_msg(event.user_id, "Не поняла вашего ответа...")

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question