vk_api

repository·master·Indexed 23 days ago

https://github.com/python273/vk_api

A Python wrapper for the VK (vk.ru) API designed for creating scripts and bots. Version 11.10.1 provides tools for authentication, long polling (user and bot), media uploads via VkUpload, and audio interaction via VkAudio. It includes specialized modules for request pooling with VkRequestsPool, streaming with VkStreaming, and creating interactive bot keyboards with VkKeyboard.

Tokens
10.5K
Snippets
7
Records
95
Agent score
80%

What's inside vk_api

  1. Use the `bot_longpoll` module for VK Bot development

    master

    The vk_api.bot_longpoll module provides classes for interacting with the VK Bots Long Poll API. It is designed to handle incoming events from VK bots using the long polling method.

    Key classes include:

    • VkBotLongPoll: The main class used to manage the long polling connection and receive events.
    • VkBotEvent: The base class for all bot events.
    • VkBotMessageEvent: A specialized subclass of VkBotEvent representing incoming messages.
  2. Create bot keyboards with VkKeyboard

    master

    The vk_api.keyboard module provides tools for creating interactive keyboards for VK bots. It consists of three main components:

    1. VkKeyboard: The main class used to construct the keyboard layout.
    2. VkKeyboardButton: Represents an individual button within the keyboard.
    3. VkKeyboardColor: Defines the visual color/style of the buttons.

    You use these classes to build a structured keyboard that can be sent to users via the VK API.

  3. Use VkRequestsPool to batch multiple requests into a single execute call

    master

    The VkRequestsPool module allows you to group multiple API requests together and execute them in a single batch using the execute method. This is useful for reducing the number of network calls and optimizing interaction with the VK API.

    Key components include:

    • VkRequestsPool: The main class used to collect and batch requests.
    • RequestResult: Represents the result of an individual request within the pool.
    • vk_request_one_param_pool: A utility function for handling single-parameter request pooling.
  4. Quickstart: Authenticate and post to a wall

    master

    To use vk_api, initialize a VkApi session with your phone number and password, call .auth() to authenticate, and use .get_api() to access the VK API methods. The following example demonstrates how to post a message to a wall.

    import vk_api
    
    vk_session = vk_api.VkApi('+71234567890', 'mypassword')
    vk_session.auth()
    
    vk = vk_session.get_api()
    
    print(vk.wall.post(message='Hello world!'))
  5. Available usage examples in vk_api

    master

    The vk_api library provides several specialized examples for common tasks:

    API and Uploading

    Authentication and Security

    Long Poll and Bot Interaction

    Data Retrieval and Tools

    Advanced Networking and Infrastructure

  6. Configure credentials for vk_api examples

    master

    When using the provided examples, you must replace the placeholder credentials with your actual VK account and group information:

    1. Replace 'python@vk.ru' with your VK login.
    2. Replace 'mypassword' with your VK password.
    3. Replace 'bot_api_token' with your specific VK group token.
  7. Use the longpoll module for User Long Poll API

    master

    The vk_api.longpoll module provides the VkLongPoll class, which is used to interact with the VK User Long Poll API. This allows your bot to receive real-time updates (events) from the VK platform.

    To use it, you will typically instantiate VkLongPoll and iterate over incoming Event objects. The module includes several supporting classes to categorize and filter these events:

    • VkLongPollMode: Defines the mode of operation.
    • VkEventType: Specifies the type of event received (e.g., messages, notifications).
    • VkPlatform: Information about the platform where the event originated.
    • VkOfflineType: Indicates the offline status.
    • VkMessageFlag: Flags related to message properties.
    • VkPeerFlag: Flags related to the peer (user or chat) involved in the event.
    • Event: The base class for all incoming event objects.
  8. Handle Captchas and 2FA

    master

    The vk_api library provides hooks to handle security challenges like Captchas and Two-Factor Authentication (2FA).

    Captchas

    When a captcha is encountered, the library raises a Captcha exception. You can provide a captcha_handler during VkApi initialization to process it automatically.

    Two-Factor Authentication (2FA)

    When 2FA is required, the library looks for an auth_handler provided during initialization. This handler must be a function that returns a tuple: (code_string, remember_device_boolean).