1
0
mirror of synced 2024-12-01 08:06:03 +03:00
mg-bot-api-client-js/lib/v1/client.js

300 lines
7.4 KiB
JavaScript
Raw Normal View History

2019-02-08 13:31:38 +03:00
'use strict';
2019-03-06 14:03:41 +03:00
/** @class Client */
2019-03-01 13:46:30 +03:00
export default class Client {
constructor(request) {
this._version = 'v1';
this._request = request;
2019-02-08 13:31:38 +03:00
}
2019-03-01 13:46:30 +03:00
/**
* Get bots
2019-03-06 14:03:41 +03:00
* @param {Object} params - Filter's object for bots
2019-03-01 13:46:30 +03:00
* @returns {Promise}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
getBots(params = {}) {
return this._request.get(this._version + '/bots', params);
};
/**
* Get channels
2019-03-06 14:03:41 +03:00
* @param {Object} params - Filter's object for channels
2019-03-01 13:46:30 +03:00
* @returns {Promise}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
getChannels(params = {}) {
return this._request.get(this._version + '/channels', params);
};
/**
* Get chats
2019-03-06 14:03:41 +03:00
* @param {Object} params - Filter's object for chats
2019-03-01 13:46:30 +03:00
* @returns {Promise}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
getChats(params = {}) {
return this._request.get(this._version + '/chats', params);
};
/**
* Get customers
2019-03-06 14:03:41 +03:00
* @param {Object} params - Filter's object for customers
2019-03-01 13:46:30 +03:00
* @returns {Promise}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
getCustomers(params = {}) {
return this._request.get(this._version + '/customers', params);
};
/**
* Get dialogs
2019-03-06 14:03:41 +03:00
* @param {Object} params - Filter's object for dialogs
2019-03-01 13:46:30 +03:00
* @returns {Promise}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
getDialogs(params = {}) {
return this._request.get(this._version + '/dialogs', params);
};
/**
* Get members
2019-03-06 14:03:41 +03:00
* @param {Object} params - Filter's object for members
2019-03-01 13:46:30 +03:00
* @returns {Promise}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
getMembers(params = {}) {
return this._request.get(this._version + '/members', params);
};
/**
* Assign dialog
2019-03-06 14:03:41 +03:00
* @param {Number} dialog_id - Dialog id
2019-03-01 13:46:30 +03:00
* @param {Object} dialog - Dialog object
* @returns {Promise}
* @throws {Error}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
assignDialog(dialog_id, dialog) {
if (!dialog_id) {
throw new Error('Parameter `dialog_id` is required');
}
return this._request.patch(this._version + '/dialogs/'+ dialog_id + '/assign', dialog);
};
/**
* Unassign dialog
* @param {Number} dialog_id - Dialog id
* @returns {Promise}
* @throws {Error}
* @memberOf Client
*/
unassignDialog(dialog_id) {
if (!dialog_id) {
throw new Error('Parameter `dialog_id` is required');
}
return this._request.patch(this._version + '/dialogs/'+ dialog_id + '/unassign', {});
}
2019-03-01 13:46:30 +03:00
/**
* Close dialog
2019-03-06 14:03:41 +03:00
* @param {Number} dialog_id - Dialog id
2019-03-01 13:46:30 +03:00
* @returns {Promise}
* @throws {Error}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
closeDialog(dialog_id) {
if (!dialog_id) {
throw new Error('Parameter `dialog_id` is required');
}
return this._request.delete(this._version + '/dialogs/'+ dialog_id + '/close');
};
/**
* Send message
2019-03-06 14:03:41 +03:00
* @param {Object} message - Message object
2019-03-01 13:46:30 +03:00
* @returns {Promise}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
2019-03-06 14:03:41 +03:00
sendMessage(message) {
return this._request.post(this._version + '/messages', message);
2019-03-01 13:46:30 +03:00
};
/**
* Get messages
2019-03-06 14:03:41 +03:00
* @param {Object} params - Filter's object for messages
2019-03-01 13:46:30 +03:00
* @returns {Promise}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
getMessages(params = {}) {
return this._request.get(this._version + '/messages', params);
};
/**
* Delete message
2019-03-06 14:03:41 +03:00
* @param {Number} message_id - Message id
2019-03-01 13:46:30 +03:00
* @returns {Promise}
* @throws {Error}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
deleteMessage(message_id) {
if (!message_id) {
throw new Error('Parameter `message_id` is required');
}
return this._request.delete(this._version + '/messages/' + message_id);
};
/**
* Edit message
2019-03-06 14:03:41 +03:00
* @param {Number} message_id - Message id
* @param {Object} message - Message object
2019-03-01 13:46:30 +03:00
* @returns {Promise}
* @throws {Error}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
editMessage(message_id, message) {
if (!message_id) {
throw new Error('Parameter `message_id` is required');
}
return this._request.patch(this._version + '/messages/' + message_id, message);
};
/**
* Get bot commands
2019-03-06 14:03:41 +03:00
* @param {Object} params - Filter's object for commands
2019-03-01 13:46:30 +03:00
* @returns {Promise}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
getCommands(params = {}) {
return this._request.get(this._version + '/my/commands', params);
};
/**
* Edit bot command
2019-03-06 14:03:41 +03:00
* @param {string} command_name - Command name
* @param {Object} command - Command object
2019-03-01 13:46:30 +03:00
* @returns {Promise}
* @throws {Error}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
2019-03-06 14:03:41 +03:00
editCommand(command_name, command) {
2019-03-01 13:46:30 +03:00
if (!command_name) {
throw new Error('Parameter `command_name` is required');
}
2019-03-06 14:03:41 +03:00
return this._request.put(this._version + '/my/commands/' + command_name, command);
2019-03-01 13:46:30 +03:00
};
/**
* Delete bot command
2019-03-06 14:03:41 +03:00
* @param {string} command_name - Command name
2019-03-01 13:46:30 +03:00
* @returns {Promise}
* @throws {Error}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
deleteCommand(command_name) {
if (!command_name) {
throw new Error('Parameter `command_name` is required');
}
return this._request.delete(this._version + '/my/commands/' + command_name);
};
/**
* Bot information update
2019-03-06 14:03:41 +03:00
* @param {Object} data - Bot data
2019-03-01 13:46:30 +03:00
* @returns {Promise}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
info(data) {
return this._request.patch(this._version + '/my/info', data);
};
/**
* Get users
2019-03-06 14:03:41 +03:00
* @param {Object} params - Filter's object for users
2019-03-01 13:46:30 +03:00
* @returns {Promise}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
getUsers(params = {}) {
return this._request.get(this._version + '/users', params);
};
/**
* Get file information
* @param {string} file_id - File identifier
* @returns {Promise}
* @throws {Error}
* @memberOf Client
*/
getFile(file_id) {
if (!file_id) {
throw new Error('Parameter `file_id` is required');
}
return this._request.get(this._version + '/files/' + file_id)
}
/**
* Upload file
*
* @param {string} data - Binary data
* @returns {Promise}
* @throws {Error}
* @memberOf Client
*/
filesUpload(data) {
return this._request.post(this._version + '/files/upload', data, false);
}
/**
* Upload file by url
*
* @param {string} url - File url address
* @returns {Promise}
* @throws {Error}
* @memberOf Client
*/
filesUploadByUrl(url) {
if (!url) {
throw new Error('Parameter `url` is required');
}
return this._request.post(this._version + '/files/upload_by_url', {url})
}
2019-03-01 13:46:30 +03:00
/**
* Get websocket url
2019-03-06 14:03:41 +03:00
* @param {array<string>} events - Array of strings with websocket events
2019-03-01 13:46:30 +03:00
* @returns {Map}
* @throws {Error}
2019-03-06 14:03:41 +03:00
* @memberOf Client
2019-03-01 13:46:30 +03:00
*/
getWebsocketData(events) {
if (!events) {
throw new Error('Events is required');
}
let map = new Map();
let url = 'wss://' + this._request.host + '/api/bot/' + this._version + '/ws?events=';
events.forEach(function (event) {
url += event + ',';
});
url = url.slice(0, -1);
map.set('url', url);
map.set('headers', {
'X-Bot-Token': this._request.token
});
return map;
};
}