API

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

Constructor

new API(vkToken, stats) → {API}

Source:
Parameters:
Name Type Description
vkToken string

VK API token

stats Stats

statistics object

Returns:
Type:
API

Members

(private, static) this._isQueueProcessing :boolean

Is the queue being processed now?

Source:
Type:
  • boolean

(private, static) this._queue :Array.<Object>

Queue of scheduled API calls

Source:
Type:
  • Array.<Object>

(private, static) this._vkToken :string

VK API token

Source:
Type:
  • string

(static) this.API_QUOTA :number

API quota, in requests per second

Source:
Type:
  • number

(static) this.API_VERSION :string

VK API version used by API

Source:
Type:
  • string

(static, readonly) this.stats :Stats

Stats object

Source:
Type:

Methods

(async, private) _checkPermissions()

Checks if the required permissions for bot to work properly are present, and emits a warning if that is not the case.

Source:

(async, private) _processQueue()

Move forward through the queue, processing at most API_QUOTA items

Source:

(async) call(method, params) → {Promise}

Call a VK API Method

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

Source:
Parameters:
Name Type Description
method string

VK API method name

params Object

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

Returns:
Type:
Promise

promise

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}`)
})

(async) scheduleCall(method, params) → {Promise}

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

Source:
Parameters:
Name Type Description
method string

VK API method name

params Object

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

Returns:
Type:
Promise

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

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}`)
})

(async) send(pid, messageopt, attachmentopt, keyboardopt) → {Promise}

Sends a message to a user via User ID

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

Source:
Parameters:
Name Type Attributes Description
pid string | number

peer ID

message string <optional>

message text (required, if attachment is empty)

attachment string <optional>

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

keyboard string <optional>

json of keyboard

Returns:
Type:
Promise

promise

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