Skip to content
Ethosa edited this page Feb 7, 2020 · 5 revisions

Vk is the main class in the library. Responsible for contacting VK API, and also provides some convenient things.

Reference

Import:

from saya import Vk

Authorization

saya provides several ways to authorize through a user or group

User authorization

  • With login and password:
vk = Vk(login=88005553535, password="saya_password")
  • With token:
vk = Vk(token="123asd456fgh789jkl0987_jhavdf")

Group authorization

  • With token and group id:
vk = Vk(token="my_group_token_secret!!", group_id=123123)

Debug mode

By adding one argument to authorization, you can get information about method calls and the status of Longpoll

vk = Vk(debug=True, ...)

Calling VK API Methods

As mentioned above - saya provides a free call to any of the existing VK API methods.
You can read about the methods here

First, we can create a wall post with the text "Hello saya-chan! ^^"

vk.wall.post(message="Hello, saya-chan! ^^")

Very easy, isn't it? Now let's move on to a little more complicated methods.

Let's try to send a message to your first conversation

vk.messages.send(message="Hello, saya-chan! ^^", peer_id=2_000_000_001, random_id=12345)

And that, too, was not complicated, right? ^^

Okay, we figured out simple methods, but what if we need to get some information from a method call? And it is possible! Let's try to get information about saya developer :D

users = vk.users.get(user_ids="akihayase")
print(users)
print(users["response"])

Receive real-time events

We learned how to call VK Api methods, okay, but what about receiving new messages? Or any other events? Saya can also provide this for you!

Let's try to receive any event

# The True parameter in the vk.longpoll.listen method is responsible for converting the list of events into a dictionary.
# you can remove it if you consider it necessary.
for event in vk.longpoll.listen(True):
    print(event)

Using a loop as the beginning of listening to any event is certainly good, but how about making it more convenient?

# on_message_new only fires when the event type is message_new.
# hence the name vk.on_message_new.
# You can also do the same with any type of event by simply writing "@vk.on_<TYPE_NAME>"
# The False parameter below does not allow the function to run in a new thread.
@vk.on_message_new(False)
def get_msg(event):
    print(event)

# The vk.start_listen method is needed to start listening to events in one thread,but makes it more convenient for the user.
vk.start_listen()

Understood the main points, let's now try a slightly different way of receiving events - multithreading.

# If we remove (False) from the previous example,
# then we get autostart functions that will work with every new event of their type
@vk.on_message_new
def get_msg(event):
    print(event)

@vk.on_message_edit
def get_msg(event):
    print(event)

OOP style

We figured out the usual use, but is it possible to interact with the class? Of course.

class MyVk(Vk):
    def __init__(self):
        Vk.__init__(self, token="....")
    
    # When inheriting from Vk, methods that are also called the type of the incoming event are automatically called
    def message_new(self, event):
        print(event)

vk = MyVk()
vk.start_listen()