B
B
bublov2019-10-07 22:44:19
Python
bublov, 2019-10-07 22:44:19

Why does an incomprehensible error occur in the VK bot code?

The code:

import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
import random
import requests
import json
def get_button(label, color, payload=""):
    return {
        "action": {
            "type": "text",
            "payload": json.dumps(payload),
            "label": label
        },
        "color": color
    }
keyboard = {
    "one_time": True,
    "buttons":[
        [
        get_button("1", "positive")
        ]
    ]
}
def main():
    bot_session = vk_api.VkApi(token="токен")
    vk = bot_session.get_api()
    while True:
        longpoll = VkBotLongPoll(bot_session, "id группы")
        try:
            for event in longpoll.listen():
                if event.type == VkBotEventType.MESSAGE_NEW:
                    command = event.obj['text']
                    if command == 'Начать':
                        vk.messages.send(  # Отправляем сообщение
                            user_id=event.obj['from_id'],
                            random_id=random.getrandbits(64),
                            message="Выбери свой класс!",
                            keyboard=keyboard
                        )
                    else:
                        vk.messages.send(  # Отправляем сообщение
                            user_id=event.obj['from_id'],
                            random_id=random.getrandbits(64),
                            message="Функционал в разработке!",
                            keyboard=keyboard
                        )
        except requests.exceptions.ReadTimeout as timeout:
            continue
if __name__ == '__main__':
    main()

Error occurs:
File "C:\Users\user\PycharmProjects\project\venv\lib\site-packages\vk_api\bot_longpoll.py", line 160, in __init__
if self.obj.peer_id < 0:
TypeError: '<' not supported between instances of 'NoneType' and 'int'
Why? I don't even have a sign.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mahmudchon, 2019-10-07
@mahmudchon

Because the "less than" sign in the bot_longpoll.py file on line 160.
As for the solution itself - depending on the version of the API you are making a request to, VK may or may not return a peer_id . Think along these lines.
https://vk.com/dev/versions
The messages.send and messages.sendSticker methods take a random_id parameter instead of a guid. The account.getPushSettings method returns the peer_id field instead of chat_id and user_id. The account.setSilenceMode method takes a peer_id parameter instead of chat_id and user_id. The messages.getHistoryAttachments method returns products, wall posts, and comments as links.
Affects methods messages.send, account.getPushSettings, account.setSilenceMode, messages.getHistoryAttachments
...
The apps.get and apps.getCatalog methods return a photo object for the screenshots fields. Added fields in_read, out_read to the responses of the messages.getDialogs and messages.getHistory methods.
Added peer_id parameter for messages.send, messages.sendSticker, messages.deleteDialog, messages.setActivity and messages.getHistory methods.
Affects methods messages.send, messages.deleteDialog, messages.setActivity, messages.getDialogs, messages.getHistory, apps.get, apps.getCatalog

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question