VoximplantKit
Constructors
constructor
Voximplant Kit class, a middleware for working with functions.
module.exports = async function(context, callback) {
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
// Some code
console.log(Date.now());
// End of function
callback(200, kit.getResponseBody());
}
Parameters
context:
ContextObject
Methods
apiProxy
apiProxy(url: string
, data: any
): Promise<unknown>
Allows you to use the Voximplant Kit API.
// Example of getting an account name
const kit = new VoximplantKit(context);
try {
const { success, result } = await kit.apiProxy('/v2/account/getAccountInfo');
if (success) {
console.log('Account name', result.domain.name);
}
} catch (err) {
console.log(err);
}
// End of function
callback(200, kit.getResponseBody());
Parameters
url:
string
URL address
data:
any
Returns
type:
Promise<unknown>
cancelFinishRequest
cancelFinishRequest(): boolean
Reopens the client's request.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
if (this.isMessage()) {
kit.finishRequest();
}
// ...
// Сondition for reopening
const shouldCancel = true;
if (shouldCancel) {
kit.cancelFinishRequest();
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
boolean
cancelTransferToQueue
cancelTransferToQueue(): boolean
Cancels transferring a client to the queue.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
// Transfer a client to the queue
kit.transferToQueue({queue_id: null, queue_name: 'some_queue_name'});
//...
// Condition for canceling the transfer to the queue
const shouldCancel = true;
if (shouldCancel) {
kit.cancelTransferToQueue();
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
boolean
cancelTransferToUser
cancelTransferToUser(): boolean
Cancels transferring a client to the user.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
// Transfer a client to the queue
kit.transferToUser({user_id: 12});
//...
// Condition for canceling the transfer to the queue
const shouldCancel = true;
if (shouldCancel) {
kit.cancelTransferToUser();
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
boolean
dbCommit
dbCommit(): Promise<boolean>
Adds changes to the database. Available only after loadDatabases() execution.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
try {
// Connect available databases
await kit.loadDatabases();
// Get a value from the function scope by key
const _test = kit.dbGet('test_key', 'function')
// If there is no data
if (_test === null) {
kit.dbSet('test_key', 'Hello world!!!', 'function');
}
// Write changes to the database
await kit.dbCommit();
} catch(err) {
console.log(err);
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
Promise<boolean>
dbDelete
dbDelete(key: string
, scope: ): boolean
Deletes a value from the specified database scope, if the key already exists. Available only after loadDatabase() execution.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
try {
// Connect available databases
await kit.loadDatabases();
// Delete a value from the function scope by key
kit.dbDelete('test_key', 'function')
// Write changes to the database
await kit.dbCommit();
} catch(err) {
console.log(err);
}
// End of function
callback(200, kit.getResponseBody());
Parameters
key:
string
Key
scope:
Database scope
Returns
type:
boolean
dbGet
dbGet(key: string
, scope: ): string
| null
Gets a value from the database scope by key. Available only after loadDatabases() execution.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
try {
// Connect available databases
await kit.loadDatabases();
// Get the value from the function scope by key
const _test = kit.dbGet('test_key', 'function')
console.log(_test);
} catch(err) {
console.log(err);
}
// End of function
callback(200, kit.getResponseBody());
Parameters
key:
string
Key
scope:
Defaults on: "global"Database scope
Returns
type:
string
type:
null
dbGetAll
dbGetAll(scope: ): | null
Gets the whole database scope by name. Available only after loadDatabases() execution.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
try {
// Connect available databases
await kit.loadDatabases();
// Read contents from the global scope
const global_scope = kit.dbGetAll('global');
console.log(global_scope)
} catch(err) {
console.log(err);
}
// End of function
callback(200, kit.getResponseBody());
Parameters
scope:
Defaults on: "global"Database scope
Returns
type:
type:
null
dbSet
dbSet(key: string
, value: any
, scope: ): boolean
Adds a value to the database scope or updates it if the key already exists. Available only after loadDatabases() execution.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
try {
// Connect available databases
await kit.loadDatabases();
// Get a value from the function scope by key
const _test = kit.dbGet('test_key', 'function')
// If there is no data
if (_test === null) {
kit.dbSet('test_key', 'Hello world!!!', 'function');
}
// Write changes to the database
await kit.dbCommit();
} catch(err) {
console.log(err);
}
// End of function
callback(200, kit.getResponseBody());
Parameters
key:
string
Key
value:
any
Value
scope:
Defaults on: "global"Database scope
Returns
type:
boolean
deleteCustomData
deleteCustomData(name: string
): boolean
Delete custom data.
const kit = new VoximplantKit(context);
kit.deleteCustomData('my_data');
// End of function
callback(200, kit.getResponseBody());
Parameters
name:
string
Returns
type:
boolean
deleteVariable
deleteVariable(name: string
): boolean
Deletes a variable by name.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
kit.deleteVariable('my_var');
// Console will print null
console.log(kit.getVariable('my_var'));
// End of function
callback(200, kit.getResponseBody());
Parameters
name:
string
Variable name
Returns
type:
boolean
finishRequest
finishRequest(): boolean
Closes the client's request.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
if (this.isMessage()) {
kit.finishRequest();
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
boolean
getAvatarReply
getAvatarReply(): | null
Gets an avatar reply
const kit = new VoximplantKit(context);
if (kit.isCall()) {
const reply = kit.getAvatarReply();
console.log('Reply: ', reply);
} // End of function
callback(200, kit.getResponseBody());
Returns
type:
type:
null
getCallData
getCallData(): | null
Gets all call data.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
if (kit.isCall()) {
const call = kit.getCallData();
// Get the phone number from which the call is made
console.log(call.phone_a);
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
type:
null
getCallHeaders
getCallHeaders(): | null
Gets call headers.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
if (kit.isCall()) {
const headers = kit.getCallHeaders();
console.log(headers);
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
type:
null
getConversationUuid
getConversationUuid(): string
| null
Get the conversation uuid. Only applicable when called from a channel or when calling the function as a callbackUri in the sendMessageToAvatar method.
const kit = new VoximplantKit(context);
if (kit.isMessage() || kit.isAvatar()) {
const uuid = kit.getConversationUuid();
//... do something
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
string
type:
null
getDfKey
getDfKey(id: number
): | null
Get DialogFlow key by id.
const kit = new VoximplantKit(context);
const dfKey = kit.getDfKey(15);
if (dfKey) {
console.log('My DF key:', dfKey);
//... do something
}
// End of function
callback(200, kit.getResponseBody());
Parameters
id:
number
Returns
type:
type:
null
getDfKeysList
getDfKeysList(): string[]
Gets a list of available DialogFlow keys
const kit = new VoximplantKit(context);
const dfKeyList = kit.getDfKeysList();
console.log('My DF keys:', dfKeyList);
//... do something // End of function
callback(200, kit.getResponseBody());
Returns
type:
string[]
getEnvVariable
getEnvVariable(name: string
): string
| null
Gets an environment variable by name. More details here.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
const my_var = kit.getEnvVariable('myEnv');
if (my_var) {
console.log(my_var);
}
// End of function
callback(200, kit.getResponseBody());
Parameters
name:
string
Variable name
Returns
type:
string
type:
null
getEnvironmentVariable
getEnvironmentVariable(name: string
): string
| null
A static method used outside the function body that gets environment variables.
const my_var = VoximplantKit.getEnvironmentVariable('myEnv');
if (my_var) {
console.log(my_var);
}
Parameters
name:
string
Returns
type:
string
type:
null
getFunctionUriById
getFunctionUriById(id: number
): string
| null
Get the function URI by its id.
const kit = new VoximplantKit(context);
const uri = kit.getFunctionUriById(31);
// End of function
callback(200, kit.getResponseBody());
Parameters
id:
number
Returns
type:
string
type:
null
getIncomingMessage
getIncomingMessage(): | null
Gets an incoming message.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
// Check if the function is called from a channel
if (kit.isMessage()) {
// Get text from an incoming message
const message = kit.getIncomingMessage();
console.log(message.text);
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
null
getMessageObject
getMessageObject(): |
Gets a message object.
const kit = new VoximplantKit(context);
if (kit.isMessage() || kit.isAvatar()) {
const messageObject = kit.getMessageObject();
// ...do something
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
type:
getPriority
getPriority(): number
Gets call priorities.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
// Return a number from 0 to 10
const priority = kit.getPriority();
if (priority === 10) {
// Something to do
} else if (priority === 5) {
// Something to do
} else {
// Something to do
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
number
getResponseBody
getResponseBody(): | | undefined
Gets a function response. Needs to be called at the end of each function.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
// End of function
callback(200, kit.getResponseBody());
Returns
type:
type:
type:
undefined
getSkills
getSkills():
Gets all skills.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
if (this.isCall()) {
const all_skills = kit.getSkills();
console.log('All skills:', all_skills);
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
getVariable
getVariable(name: string
): string
| null
Gets a variable by name.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
const my_var = kit.getVariable('my_var');
if (my_var) {
console.log(my_var);
}
// End of function
callback(200, kit.getResponseBody());
Parameters
name:
string
Variable name
Returns
type:
string
type:
null
getVariables
getVariables():
Gets all variables.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
const all_vars = kit.getVariables();
console.log(all_vars);
// End of function
callback(200, kit.getResponseBody());
Returns
type:
isAvatar
isAvatar(): boolean
The function is called by the avatar.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
if (kit.isAvatar()) {
//...do something
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
boolean
isCall
isCall(): boolean
The function is called from a call.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
if (kit.isCall()) {
console.log('This function is called from the call')
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
boolean
isMessage
isMessage(): boolean
The function is called from a message.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
if (kit.isMessage()) {
console.log('This function is called from the channel');
const message = kit.getIncomingMessage();
//...
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
boolean
loadDatabases
loadDatabases(): Promise<void>
Loads the databases available in the scope.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
try {
// Connect available databases
await kit.loadDatabases();
// Read contents from the global scope
const global_scope = kit.dbGetAll('global');
console.log(global_scope)
} catch(err) {
console.log(err);
}
// End of function
callback(200, kit.getResponseBody());
Returns
type:
Promise<void>
removeSkill
removeSkill(id: number
): boolean
Removes a skill by id.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
kit.removeSkill(234);
// End of function
callback(200, kit.getResponseBody());
Parameters
id:
number
Name of the skill to remove
Returns
type:
boolean
setCustomData
setCustomData(name: string
, data: unknown
): boolean
Set custom data.
const kit = new VoximplantKit(context);
kit.setCustomData('my_data', {a: 1, b 'some text'}); // [12, 34]
// End of function
callback(200, kit.getResponseBody());
Parameters
name:
string
data:
unknown
Returns
type:
boolean
setPriority
setPriority(value: number
): boolean
Sets the call priority. The higher the priority, the less time a client will wait for the operator's response.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
// Transfer a client to the queue
kit.transferToQueue({queue_id: null, queue_name: 'some_queue_name'});
// Set the highest priority
kit.setPriority(10);
// End of function
callback(200, kit.getResponseBody());
Parameters
value:
number
Priority value, from 0 to 10
Returns
type:
boolean
setReplyMessageText
setReplyMessageText(text: string
): boolean
Sets a reply message text.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
// Check if the function is called from a channel
if (kit.isMessage()) {
// Get text from an incoming message
const message = kit.getIncomingMessage();
console.log(message.text);
// Set text of the reply
kit.setReplyMessageText('you wrote ' + message.text);
}
// End of function
callback(200, kit.getResponseBody());
Parameters
text:
string
Reply text
Returns
type:
boolean
setSkill
setSkill(skill: ): boolean
Adds a skill or updates it if the skill id already exists.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
if (kit.isCall()) {
kit.setSkill({skill_id: 234, level: 5});
} else if (kit.isMessage()) {
kit.setSkill({skill_id: 35, level: 3});
kit.transferToQueue({queue_id: 72});
}
// End of function
callback(200, kit.getResponseBody());
Parameters
skill:
Returns
type:
boolean
setTelegramInlineKeyboard
setTelegramInlineKeyboard(keyboard_markup: ): boolean
Adds inline keyboard for the telegram channel
const kit = new VoximplantKit(context);
if (kit.isMessage() || kit.isAvatar()) {
// Without a reply message, the keyboard will not be displayed
const message = kit.getIncomingMessage();
kit.setReplyMessageText(`You wrote: ${message.text}`); // An array of arrays with keyboard buttons.
// Text is required for each keyboard.
const inline_keyboard_markup = [
// Row one
[
{text: 'text', url: 'url', callback_data: '1'},
{text: 'text 2', url: 'url'},
],
// Row two
[
{text: 'text', url: 'url', callback_data: '1'},
]
]
kit.setTelegramInlineKeyboard(buttons);
// Calling the kit.setTelegramInlineKeyboard method
// with an empty array will clear previously passed buttons
// kit.setTelegramInlineKeyboard([]);
}
// End of function
callback(200, kit.getResponseBody());
Parameters
keyboard_markup:
Returns
type:
boolean
setTelegramReplyKeyboard
setTelegramReplyKeyboard(keyboard_markup: , keyboard_params: ): boolean
Adds reply keyboard for the telegram channel
const kit = new VoximplantKit(context);
if (kit.isMessage() || kit.isAvatar()) {
// Without a reply message, the keyboard will not be displayed
const message = kit.getIncomingMessage();
kit.setReplyMessageText(`You wrote: ${message.text}`); // An array of arrays with keyboard buttons.
// Text is required for each keyboard.
const reply_keyboard_markup = [
// Row one
[
{text: 'button 1', request_contact: true},
{text: 'button 2'},
],
// Row two
[
{text: 'button 3', request_location: true},
]
]
// Optional params
const params = {
is_persistent : false,
resize_keyboard: false,
one_time_keyboard: false,
input_field_placeholder: 'Some text',
selective: false
}
kit.setTelegramReplyKeyboard(reply_keyboard_markup, params);
// Calling the kit.settelgramreplykeyboard method
// with an empty array will clear previously passed buttons
// kit.setTelegramReplyKeyboard([]);
}
// End of function
callback(200, kit.getResponseBody());
Parameters
keyboard_markup:
keyboard_params:
Defaults on: {}
Returns
type:
boolean
setTelegramReplyKeyboardRemove
setTelegramReplyKeyboardRemove(remove_params: ): boolean
Remove replyKeyboard for telegram channel
const kit = new VoximplantKit(context);
if (kit.isMessage() || kit.isAvatar()) {
const remove_params = {
remove_keyboard: true, // required
selective: false
}
kit.setTelegramReplyKeyboardRemove(remove_params);
} // End of function
callback(200, kit.getResponseBody());
Parameters
remove_params:
Returns
type:
boolean
setVariable
setVariable(name: string
, value: any
): boolean
Adds a variable or updates it if the variable name already exists.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
kit.setVariable('my_var', 'some_value');
console.log(kit.getVariable('my_var'));
// End of function
callback(200, kit.getResponseBody());
Parameters
name:
string
Variable name
value:
any
Variable value
Returns
type:
boolean
setWhatsappEdnaKeyboard
setWhatsappEdnaKeyboard(keyboard_rows: ): boolean
Set Whatsapp Edna keyboard
const kit = new VoximplantKit(context);
if (kit.isMessage() || kit.isAvatar()) {
const message = kit.getIncomingMessage();
kit.setReplyMessageText(`You wrote: ${message.text}`);
const keyboard = [
{
"buttons": [
{
"text": "test 1", // Required
"payload": "test payload 1",
"type": "QUICK_REPLY" // Required
},
{
"text": "test 2", // Required
"payload": "test payload 2",
"type": "QUICK_REPLY" // Required
},
]
}
];
const isSet = kit.setWhatsappEdnaKeyboard(keyboard);
console.log('Buttons for whatsapp have been added:', isSet);
} // End of function
callback(200, kit.getResponseBody());
Parameters
keyboard_rows:
Returns
type:
boolean
transferToQueue
transferToQueue(queue: ): boolean
Transfers a client to the queue.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
// Transfer a client to the queue
kit.transferToQueue({queue_id: 82});
// End of function
callback(200, kit.getResponseBody());
Parameters
queue:
Returns
type:
boolean
transferToUser
transferToUser(user: ): boolean
Transfers a client to the user. Only for text channels and Avatar.
// Initialize a VoximplantKit instance
const kit = new VoximplantKit(context);
if (this.isMessage() || this.isAvatar()) {
// Use user_id or user_email.
kit.transferToUser({user_id: 12});
}
// End of function
callback(200, kit.getResponseBody());
Parameters
user:
Returns
type:
boolean
version
version(): string
| void
Gets a client’s SDK version.
const kit = new VoximplantKit(context);
// Get a client’s SDK version
kit.version();
// End of function
callback(200, kit.getResponseBody());
Returns
type:
string
type:
void
Props
avatar
avatar: