Options
All
  • Public
  • Public/Protected
  • All
Menu

Class API

Used to call API methods.

You can get the API object from a Context object:

// Assuming your Context object is $
var api = $.api

Or from Core (after initialization with bot:

var api = core.api

Hierarchy

  • API

Index

Constructors

constructor

  • new API(vkToken: string, stats: Stats): API

Properties

Private isQueueProcessing

isQueueProcessing: boolean = false

Is the queue being processed now?

Private queue

queue: { method: string; params: {}; reject: Function; resolve: Function }[] = []

Queue of scheduled API calls.

Private stats

stats: Stats

Stats object.

Private vkToken

vkToken: string

VK API token.

Methods

call

  • call(method: string, params: {}): Promise<any>
  • Call a VK API Method.

    It is highly recommended to use scheduleCall instead to not exceed the API quota and to check whether the call was successful or not!

    example
    core.cmd('info', async $ => {
       var uid = $.obj.from_id;
    
       // Call VK API to get information about the user
       var json = await $.api.call('users.get', { user_ids: uid });
       var userInfo = json.response[0];
    
       var name = userInfo.first_name;
       var surname = userInfo.last_name;
    
     $.text(`User ID: ${uid}\nName: ${name} ${surname}`);
    });

    Parameters

    • method: string

      VK API method name

    • params: {}

      parameters for the method, access_token and v will be added automatically

      • [key: string]: string

    Returns Promise<any>

Private checkPermissions

  • checkPermissions(): Promise<string>
  • Checks if the required permissions for bot to work properly are present, and emits a warning if that is not the case.

    Returns Promise<string>

Private processQueue

  • processQueue(): Promise<void>
  • Move forward through the queue, processing at most API_QUOTA items

    Returns Promise<void>

scheduleCall

  • scheduleCall(method: string, params: {}): Promise<any>
  • Schedules a call to a VK API Method.

    After the call completes, a check will be performed to see if the call was successful or not, and in the latter case a warning will be logged.

    example
    core.cmd('info', async $ => {
       var uid = $.obj.from_id;
    
       // Call VK API to get information about the user
       var response = await $.api.scheduleCall('users.get', { user_ids: uid });
       var userInfo = response[0];
    
       var name = userInfo.first_name;
       var surname = userInfo.last_name;
    
     $.text(`User ID: ${uid}\nName: ${name} ${surname}`);
    });

    Parameters

    • method: string

      VK API method name

    • params: {}

      parameters for the method, access_token and v will be added automatically

      • [key: string]: string

    Returns Promise<any>

    promise, which resolves with json.response when the request is completed and a response is given, and rejects if an error happened

send

  • send(pid: string | number, message: string, attachment: string, keyboard: string): Promise<void>
  • Sends a message to a user via Peer ID.

    Note that it is much easier to use the Context object passed to handlers to compose and send messages, keyboards and attachments!

    example
    await api.send(1, 'Hello!', 'photo6492_456240778')

    Parameters

    • pid: string | number

      peer ID

    • message: string

      message text (required, if attachment is empty)

    • attachment: string

      list of attachments, comma-separated (see VK API Docs for further information) (required if message is empty)

    • keyboard: string

      json of keyboard

    Returns Promise<void>

Generated using TypeDoc