Compare commits
20 commits
Author | SHA1 | Date | |
---|---|---|---|
|
d6b1f0d21d | ||
|
3b49e487b6 | ||
|
fa5499b9fe | ||
|
bddf54e9fe | ||
|
89a38c39b4 | ||
|
05d2188453 | ||
|
19f828cce7 | ||
|
02ab9feec0 | ||
|
fe4d708ea9 | ||
|
347cdcca8b | ||
|
59ba7971a5 | ||
|
da8631e5d8 | ||
|
975a352053 | ||
|
0d680208b1 | ||
|
8598c68426 | ||
|
6edec6141c | ||
|
d0b31da80d | ||
|
e71a9ab7e3 | ||
|
74cdccf5cf | ||
|
b430a9146f |
16 changed files with 1105 additions and 470 deletions
3
.babelrc
Normal file
3
.babelrc
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"presets": ["@babel/preset-env"]
|
||||||
|
}
|
66
.github/workflows/ci.yml
vendored
Normal file
66
.github/workflows/ci.yml
vendored
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
name: ci
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- '**'
|
||||||
|
tags-ignore:
|
||||||
|
- '*.*'
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
node-version: [10.x, 12.x, 13.x, 14.x]
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Setup Node.js ${{ matrix.node-version }}
|
||||||
|
uses: actions/setup-node@v1
|
||||||
|
with:
|
||||||
|
node-version: ${{ matrix.node-version }}
|
||||||
|
- name: Install dependencies
|
||||||
|
run: npm install
|
||||||
|
- name: Run tests
|
||||||
|
run: npm run test
|
||||||
|
- name: Coverage
|
||||||
|
run: |
|
||||||
|
npm install -g codecov
|
||||||
|
npm run report-coverage
|
||||||
|
gh-pages:
|
||||||
|
needs: ['test']
|
||||||
|
if: success() && github.event_name == 'push' && github.repository_owner == 'retailcrm' && github.ref == 'refs/heads/master'
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Setup Node.js
|
||||||
|
uses: actions/setup-node@v1
|
||||||
|
with:
|
||||||
|
node-version: 10.x
|
||||||
|
- name: Install dependencies
|
||||||
|
run: npm install
|
||||||
|
- name: Generate doc
|
||||||
|
run: npm run doc
|
||||||
|
- name: Deploy doc
|
||||||
|
uses: peaceiris/actions-gh-pages@v3
|
||||||
|
with:
|
||||||
|
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
publish_dir: ./out
|
||||||
|
deploy:
|
||||||
|
needs: ['test']
|
||||||
|
if: success() && github.event_name == 'push' && github.repository_owner == 'retailcrm' && github.ref == 'refs/heads/master'
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v1
|
||||||
|
- uses: actions/setup-node@v1
|
||||||
|
with:
|
||||||
|
node-version: 10.x
|
||||||
|
- name: Install dependencies
|
||||||
|
run: npm install
|
||||||
|
- name: Build
|
||||||
|
run: npm run build
|
||||||
|
- name: Publish
|
||||||
|
uses: JS-DevTools/npm-publish@v1
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.NPM_TOKEN }}
|
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -60,4 +60,6 @@ typings/
|
||||||
# next.js build output
|
# next.js build output
|
||||||
.next
|
.next
|
||||||
.idea
|
.idea
|
||||||
package-lock.json
|
|
||||||
|
out/
|
||||||
|
dist/
|
||||||
|
|
64
.npmignore
Normal file
64
.npmignore
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
pids
|
||||||
|
*.pid
|
||||||
|
*.seed
|
||||||
|
*.pid.lock
|
||||||
|
|
||||||
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
lib-cov
|
||||||
|
|
||||||
|
# Coverage directory used by tools like istanbul
|
||||||
|
coverage
|
||||||
|
|
||||||
|
# nyc test coverage
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
.grunt
|
||||||
|
|
||||||
|
# Bower dependency directory (https://bower.io/)
|
||||||
|
bower_components
|
||||||
|
|
||||||
|
# node-waf configuration
|
||||||
|
.lock-wscript
|
||||||
|
|
||||||
|
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||||
|
build/Release
|
||||||
|
|
||||||
|
# Dependency directories
|
||||||
|
node_modules/
|
||||||
|
jspm_packages/
|
||||||
|
|
||||||
|
# TypeScript v1 declaration files
|
||||||
|
typings/
|
||||||
|
|
||||||
|
# Optional npm cache directory
|
||||||
|
.npm
|
||||||
|
|
||||||
|
# Optional eslint cache
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Optional REPL history
|
||||||
|
.node_repl_history
|
||||||
|
|
||||||
|
# Output of 'npm pack'
|
||||||
|
*.tgz
|
||||||
|
|
||||||
|
# Yarn Integrity file
|
||||||
|
.yarn-integrity
|
||||||
|
|
||||||
|
# dotenv environment variables file
|
||||||
|
.env
|
||||||
|
|
||||||
|
# next.js build output
|
||||||
|
.next
|
||||||
|
.idea
|
||||||
|
|
||||||
|
out/
|
|
@ -1,6 +0,0 @@
|
||||||
language: node_js
|
|
||||||
node_js:
|
|
||||||
- "11"
|
|
||||||
- "10"
|
|
||||||
- "8"
|
|
||||||
script: npm run test
|
|
2
LICENSE
2
LICENSE
|
@ -1,6 +1,6 @@
|
||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2018 retailCRM
|
Copyright (c) 2018-2020 RetailDriver LLC
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
67
README.md
67
README.md
|
@ -1,28 +1,34 @@
|
||||||
[](https://travis-ci.org/retailcrm/mg-bot-api-client-js)
|
[](https://github.com/retailcrm/mg-bot-api-client-js/actions)
|
||||||
[](https://github.com/retailcrm/mg-bot-api-client-js/releases)
|
[](https://codecov.io/gh/retailcrm/mg-bot-api-client-js)
|
||||||
[](https://www.npmjs.com/package/mg-api-client)
|
[](https://npmjs.com/package/mg-api-client)
|
||||||
|
[](https://www.npmjs.com/package/mg-api-client)
|
||||||
|
[](https://retailcrm.github.io/mg-bot-api-client-js/)
|
||||||
|
|
||||||
|
# Message Gateway Bot API JavaScript client
|
||||||
# retailCRM Message Gateway Bot API JS client
|
|
||||||
|
|
||||||
This is js retailCRM bot API client.
|
|
||||||
|
|
||||||
# Installation
|
# Installation
|
||||||
```
|
```
|
||||||
npm install --save mg-api-client
|
npm install --save mg-api-client
|
||||||
```
|
```
|
||||||
In your file
|
In your file
|
||||||
|
|
||||||
|
###### CommonJS
|
||||||
```
|
```
|
||||||
var RetailcrmBotApiClient = require('mg-api-client');
|
var MgBotApiClient = require('mg-api-client');
|
||||||
```
|
```
|
||||||
|
###### es6
|
||||||
|
```
|
||||||
|
import MgBotApiClient from 'mg-api-client';
|
||||||
|
```
|
||||||
|
|
||||||
# Usage
|
# Usage
|
||||||
#### Get users
|
#### Get users
|
||||||
```javascript
|
```javascript
|
||||||
var api = new RetailcrmBotApiClient({
|
const api = new MgBotApiClient({
|
||||||
host: 'https://api.example.com',
|
host: 'https://api.example.com',
|
||||||
token: 'your bot token',
|
token: 'your bot token',
|
||||||
apiVersion: 'v1' // optional
|
apiVersion: 'v1' // optional
|
||||||
}).getClient();
|
}).client;
|
||||||
|
|
||||||
api.getUsers()
|
api.getUsers()
|
||||||
.then(function (users) {
|
.then(function (users) {
|
||||||
|
@ -35,13 +41,13 @@ api.getUsers()
|
||||||
|
|
||||||
#### Send message
|
#### Send message
|
||||||
```javascript
|
```javascript
|
||||||
var api = new RetailcrmBotApiClient({
|
const api = new MgBotApiClient({
|
||||||
host: 'https://api.example.com',
|
host: 'https://api.example.com',
|
||||||
token: 'your bot token',
|
token: 'your bot token',
|
||||||
apiVersion: 'v1' // optional
|
apiVersion: 'v1' // optional
|
||||||
}).getClient();
|
}).client;
|
||||||
|
|
||||||
var message = {
|
let message = {
|
||||||
chat_id: 1,
|
chat_id: 1,
|
||||||
content: 'Text message',
|
content: 'Text message',
|
||||||
scope: 'public',
|
scope: 'public',
|
||||||
|
@ -56,3 +62,38 @@ api.sendMessage(message)
|
||||||
console.log(e);
|
console.log(e);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
#### Websocket Example
|
||||||
|
```javascript
|
||||||
|
const WebSocket = require('ws');
|
||||||
|
|
||||||
|
const api = new MgBotApiClient({
|
||||||
|
host: 'https://api.example.com',
|
||||||
|
token: 'your bot token',
|
||||||
|
apiVersion: 'v1' // optional
|
||||||
|
}).client;
|
||||||
|
|
||||||
|
const wsData = api.getWebsocketData([MgBotApiClient.types().wsMessageNew]);
|
||||||
|
const ws = new WebSocket(wsData.get('url'), {
|
||||||
|
headers: wsData.get('headers')
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on('message', function (content) {
|
||||||
|
let event = JSON.parse(content);
|
||||||
|
let data = event.data;
|
||||||
|
|
||||||
|
if (event.type === 'message_new' && data.message.from.type !== 'bot') {
|
||||||
|
let message = {
|
||||||
|
chat_id: data.message.chat_id,
|
||||||
|
content: 'Bonjour!',
|
||||||
|
scope: 'public',
|
||||||
|
type: 'text'
|
||||||
|
};
|
||||||
|
|
||||||
|
api.sendMessage(message).then(function (res) {
|
||||||
|
console.log(res);
|
||||||
|
}).catch(function (e) {
|
||||||
|
console.log(e);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
85
index.js
85
index.js
|
@ -1,48 +1,57 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var v1 = require('./lib/v1/client');
|
import v1 from './lib/v1/client'
|
||||||
var request = require('./lib/request');
|
import Request from './lib/request'
|
||||||
|
import * as consts from './lib/consts'
|
||||||
|
|
||||||
module.exports = RetailcrmBotApiClient;
|
const lastApiVersion = 'v1';
|
||||||
|
|
||||||
/**
|
export default class MgBotApiClient {
|
||||||
* @param {Object} options
|
/**
|
||||||
* @throws {Error}
|
* @param {Object} options
|
||||||
* @constructor
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
function RetailcrmBotApiClient(options) {
|
constructor(options) {
|
||||||
if (!options.host) {
|
if (!options.host) {
|
||||||
throw new Error('Url is required');
|
throw new Error('Url is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.host.indexOf('https') !== 0) {
|
||||||
|
throw new Error('HTTPS required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(options.token)) {
|
||||||
|
throw new Error('Token is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
let currentVersion;
|
||||||
|
|
||||||
|
const clients = {
|
||||||
|
'v1': v1
|
||||||
|
};
|
||||||
|
|
||||||
|
if (options.apiVersion) {
|
||||||
|
currentVersion = options.apiVersion;
|
||||||
|
} else {
|
||||||
|
currentVersion = lastApiVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._client = new clients[currentVersion](new Request(options));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.host.indexOf('https') !== 0) {
|
/**
|
||||||
throw new Error('HTTPS required');
|
* Get API client
|
||||||
}
|
* @returns {Client}
|
||||||
|
*/
|
||||||
if (!(options.token)) {
|
get client() {
|
||||||
throw new Error('Token is required');
|
return this._client;
|
||||||
}
|
|
||||||
|
|
||||||
var currentVersion;
|
|
||||||
var lastApiVersion = 'v1';
|
|
||||||
|
|
||||||
var clients = {
|
|
||||||
'v1': v1.Client
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (options.apiVersion) {
|
/**
|
||||||
currentVersion = options.apiVersion;
|
* Get types
|
||||||
} else {
|
* @returns {{msgTypeOrder?: string, wsUserJoinedChat?: string, msgTypeImage?: string, wsDialogAssign?: string, msgTypeText?: string, messageScopePublic?: string, wsMessageDeleted?: string, msgTypeCommand?: string, msgTypeFile?: string, msgTypeSystem?: string, wsBotUpdated?: string, msgTypeProduct?: string, wsDialogClosed?: string, wsMessageNew?: string, wsMessageUpdated?: string, wsSettingsUpdated?: string, wsUserUpdated?: string, wsCustomerUpdated?: string, wsChatCreated?: string, wsUserLeftChat?: string, wsChannelUpdated?: string, wsDialogOpened?: string, messageScopePrivate?: string, wsUserOnlineUpdated?: string, wsChatUnreadUpdated?: string, wsChatUpdated?: string}}
|
||||||
currentVersion = lastApiVersion;
|
*/
|
||||||
|
static types() {
|
||||||
|
return consts;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._client = new clients[currentVersion](new request.Request(options));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get API client
|
|
||||||
* @returns {Client}
|
|
||||||
*/
|
|
||||||
RetailcrmBotApiClient.prototype.getClient = function () {
|
|
||||||
return this._client;
|
|
||||||
};
|
|
||||||
|
|
212
lib/consts.js
Normal file
212
lib/consts.js
Normal file
|
@ -0,0 +1,212 @@
|
||||||
|
/**
|
||||||
|
* New message websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsMessageNew
|
||||||
|
*/
|
||||||
|
export const wsMessageNew = 'message_new';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updating message websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsMessageUpdated
|
||||||
|
*/
|
||||||
|
export const wsMessageUpdated = 'message_updated';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Websocket event delete message
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsMessageDeleted
|
||||||
|
*/
|
||||||
|
export const wsMessageDeleted = 'message_deleted';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dialogue opening websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsDialogOpened
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export const wsDialogOpened = 'dialog_opened';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dialogue closing websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsDialogClosed
|
||||||
|
*/
|
||||||
|
export const wsDialogClosed = 'dialog_closed';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dialogue appointment websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsDialogAssign
|
||||||
|
*/
|
||||||
|
export const wsDialogAssign = 'dialog_assign';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Chat creating websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsChatCreated
|
||||||
|
*/
|
||||||
|
export const wsChatCreated = 'chat_created';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Chat updating websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsChatUpdated
|
||||||
|
*/
|
||||||
|
export const wsChatUpdated = 'chat_updated';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unread chat updating websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsChatUnreadUpdated
|
||||||
|
*/
|
||||||
|
export const wsChatUnreadUpdated = 'chat_unread_updated';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User online status updating websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsUserOnlineUpdated
|
||||||
|
*/
|
||||||
|
export const wsUserOnlineUpdated = 'user_online_updated';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User joined chat websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsUserJoinedChat
|
||||||
|
*/
|
||||||
|
export const wsUserJoinedChat = 'user_joined_chat';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User left chat websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsUserLeftChat
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export const wsUserLeftChat = 'user_left_chat';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User updating websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsUserUpdated
|
||||||
|
*/
|
||||||
|
export const wsUserUpdated = 'user_updated';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Customer updating websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsCustomerUpdated
|
||||||
|
*/
|
||||||
|
export const wsCustomerUpdated = 'customer_updated';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bot updating websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsBotUpdated
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export const wsBotUpdated = 'bot_updated';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Channel updating websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsChannelUpdated
|
||||||
|
*/
|
||||||
|
export const wsChannelUpdated = 'channel_updated';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Settings updating websocket event
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().wsSettingsUpdated
|
||||||
|
*/
|
||||||
|
export const wsSettingsUpdated = 'settings_updated';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public message scope
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().messageScopePublic
|
||||||
|
*/
|
||||||
|
export const messageScopePublic = 'public';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public message scope
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().messageScopePrivate
|
||||||
|
*/
|
||||||
|
export const messageScopePrivate = 'private';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Text message type
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().msgTypeText
|
||||||
|
*/
|
||||||
|
export const msgTypeText = 'text';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* System message type
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().msgTypeSystem
|
||||||
|
*/
|
||||||
|
export const msgTypeSystem = 'system';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command message type
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().msgTypeCommand
|
||||||
|
*/
|
||||||
|
export const msgTypeCommand = 'command';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Order message type
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().msgTypeOrder
|
||||||
|
*/
|
||||||
|
export const msgTypeOrder = 'order';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Product message type
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().msgTypeProduct
|
||||||
|
*/
|
||||||
|
export const msgTypeProduct = 'product';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* File message type
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().msgTypeFile
|
||||||
|
*/
|
||||||
|
export const msgTypeFile = 'file';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Image message type
|
||||||
|
* @constant
|
||||||
|
* @type {string}
|
||||||
|
* @example MgBotApiClient.types().msgTypeImage
|
||||||
|
*/
|
||||||
|
export const msgTypeImage = 'image';
|
310
lib/request.js
310
lib/request.js
|
@ -1,161 +1,189 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var url = require('url');
|
import url from 'url'
|
||||||
var https = require('https');
|
import https from 'https'
|
||||||
var querystring = require('querystring');
|
import querystring from 'querystring'
|
||||||
|
|
||||||
exports.Request = Request;
|
export default class Request {
|
||||||
|
/**
|
||||||
|
* @param {Object} options
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
constructor(options) {
|
||||||
|
/**
|
||||||
|
* @prop System host
|
||||||
|
* @type {string}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
this._host = url.parse(options.host).host;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Object} options
|
* @prop Bot token
|
||||||
* @constructor
|
* @type {*|string|string}
|
||||||
*/
|
* @private
|
||||||
function Request(options) {
|
*/
|
||||||
this._host = url.parse(options.host).host;
|
this._token = options.token;
|
||||||
this._token = options.token;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get request path
|
|
||||||
* @param {string} endpoint
|
|
||||||
* @returns {string}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
Request.prototype._getPath = function (endpoint) {
|
|
||||||
return '/api/bot/' + endpoint;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make request
|
|
||||||
* @param {string} endpoint
|
|
||||||
* @param {string} method
|
|
||||||
* @param {Object} data
|
|
||||||
* @returns {Promise}
|
|
||||||
* @throws {Error}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
Request.prototype._request = function (endpoint, method, data) {
|
|
||||||
var path = this._getPath(endpoint);
|
|
||||||
var response = '';
|
|
||||||
|
|
||||||
if (method === 'GET' && data.length > 0) {
|
|
||||||
path += '?' + querystring.stringify(data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var options = {
|
/**
|
||||||
host: this._host,
|
* Get request path
|
||||||
method: method,
|
* @param {string} endpoint
|
||||||
path: path,
|
* @returns {string}
|
||||||
headers: {
|
* @private
|
||||||
'x-bot-token': this._token
|
*/
|
||||||
|
_getPath(endpoint) {
|
||||||
|
return '/api/bot/' + endpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make request
|
||||||
|
* @param {string} endpoint
|
||||||
|
* @param {string} method
|
||||||
|
* @param {Object} data
|
||||||
|
* @param {boolean} serializable
|
||||||
|
* @returns {Promise}
|
||||||
|
* @throws {Error}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_request(endpoint, method, data = {}, serializable = true) {
|
||||||
|
let path = this._getPath(endpoint);
|
||||||
|
let response = '';
|
||||||
|
|
||||||
|
if (method === 'GET' && Object.keys(data).length > 0) {
|
||||||
|
path += '?' + querystring.stringify(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
host: this._host,
|
||||||
|
method: method,
|
||||||
|
path: path,
|
||||||
|
headers: {
|
||||||
|
'X-Bot-Token': this._token
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
const request = https.request(options, function (res) {
|
||||||
|
res.on('data', function (chunk) {
|
||||||
|
response += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('end', function () {
|
||||||
|
try {
|
||||||
|
const result = JSON.parse(response);
|
||||||
|
|
||||||
|
if (res.statusCode < 400) {
|
||||||
|
resolve(result);
|
||||||
|
} else {
|
||||||
|
reject(new Error(result.errors.join(',')));
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
reject(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('error', function (e) {
|
||||||
|
reject(e);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
if (['POST', 'PUT', 'PATCH'].includes(method)) {
|
||||||
|
let sendData;
|
||||||
|
if (serializable) {
|
||||||
|
sendData = JSON.stringify(data);
|
||||||
|
} else {
|
||||||
|
sendData = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
request.write(sendData);
|
||||||
|
}
|
||||||
|
|
||||||
|
request.end();
|
||||||
|
|
||||||
|
request.on('error', function(e) {
|
||||||
|
reject(e);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method GET
|
||||||
|
* @param {string} endpoint
|
||||||
|
* @param {Object} params
|
||||||
|
* @returns {Promise}
|
||||||
|
*/
|
||||||
|
get(endpoint, params = {}) {
|
||||||
|
return this._request(endpoint, 'GET', params);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method POST
|
||||||
|
* @param {string} endpoint
|
||||||
|
* @param {Object} data
|
||||||
|
* @param {boolean} serializable
|
||||||
|
* @returns {Promise}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
post(endpoint, data, serializable = true) {
|
||||||
|
if (!data) {
|
||||||
|
throw new Error('Body is not be empty');
|
||||||
|
}
|
||||||
|
|
||||||
|
return this._request(endpoint, 'POST', data, serializable);
|
||||||
};
|
};
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
/**
|
||||||
var request = https.request(options, function (res) {
|
* Method PATCH
|
||||||
res.on('data', function (chunk) {
|
* @param {string} endpoint
|
||||||
response += chunk;
|
* @param {Object} data
|
||||||
});
|
* @returns {Promise}
|
||||||
|
* @throws {Error}
|
||||||
res.on('end', function () {
|
*/
|
||||||
try {
|
patch(endpoint, data) {
|
||||||
var result = JSON.parse(response);
|
if (!data) {
|
||||||
|
throw new Error('Body is not be empty');
|
||||||
if (res.statusCode < 400) {
|
|
||||||
resolve(result);
|
|
||||||
} else {
|
|
||||||
reject(new Error(result.errors.join(',')));
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
reject(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
res.on('error', function (e) {
|
|
||||||
reject(e);
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
if (['POST', 'PUT', 'PATCH'].includes(method)) {
|
|
||||||
request.write(JSON.stringify(data));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
request.end();
|
return this._request(endpoint, 'PATCH', data);
|
||||||
|
|
||||||
request.on('error', function(e) {
|
|
||||||
reject(e);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method GET
|
|
||||||
* @param {string} endpoint
|
|
||||||
* @param {Object} params
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
Request.prototype.get = function (endpoint, params) {
|
|
||||||
if (params === undefined) {
|
|
||||||
params = {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._request(endpoint, 'GET', params);
|
/**
|
||||||
};
|
* Method PUT
|
||||||
|
* @param {string} endpoint
|
||||||
|
* @param {Object} data
|
||||||
|
* @returns {Promise}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
put(endpoint, data) {
|
||||||
|
if (!data) {
|
||||||
|
throw new Error('Body is not be empty');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
return this._request(endpoint, 'PUT', data);
|
||||||
* Method POST
|
|
||||||
* @param {string} endpoint
|
|
||||||
* @param {Object} data
|
|
||||||
* @returns {Promise}
|
|
||||||
* @throws {Error}
|
|
||||||
*/
|
|
||||||
Request.prototype.post = function (endpoint, data) {
|
|
||||||
if (!data) {
|
|
||||||
throw new Error('Body is not be empty');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._request(endpoint, 'POST', data);
|
/**
|
||||||
};
|
* Method DELETE
|
||||||
|
* @param {string} endpoint
|
||||||
/**
|
* @returns {Promise}
|
||||||
* Method PATCH
|
*/
|
||||||
* @param {string} endpoint
|
delete(endpoint) {
|
||||||
* @param {Object} data
|
return this._request(endpoint, 'DELETE');
|
||||||
* @returns {Promise}
|
|
||||||
* @throws {Error}
|
|
||||||
*/
|
|
||||||
Request.prototype.patch = function (endpoint, data) {
|
|
||||||
if (!data) {
|
|
||||||
throw new Error('Body is not be empty');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._request(endpoint, 'PATCH', data);
|
/**
|
||||||
};
|
* Get api host
|
||||||
|
* @returns {string | *}
|
||||||
/**
|
*/
|
||||||
* Method PUT
|
get host() {
|
||||||
* @param {string} endpoint
|
return this._host;
|
||||||
* @param {Object} data
|
|
||||||
* @returns {Promise}
|
|
||||||
* @throws {Error}
|
|
||||||
*/
|
|
||||||
Request.prototype.put = function (endpoint, data) {
|
|
||||||
if (!data) {
|
|
||||||
throw new Error('Body is not be empty');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._request(endpoint, 'PUT', data);
|
/**
|
||||||
};
|
* Get bot token
|
||||||
|
* @returns {*|string}
|
||||||
/**
|
*/
|
||||||
* Method DELETE
|
get token() {
|
||||||
* @param {string} endpoint
|
return this._token;
|
||||||
* @returns {Promise}
|
}
|
||||||
*/
|
}
|
||||||
Request.prototype.delete = function (endpoint) {
|
|
||||||
return this._request(endpoint, 'DELETE', {});
|
|
||||||
};
|
|
||||||
|
|
||||||
Request.prototype.getHost = function () {
|
|
||||||
return this._host;
|
|
||||||
};
|
|
||||||
|
|
501
lib/v1/client.js
501
lib/v1/client.js
|
@ -1,210 +1,299 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
exports.Client = Client;
|
/** @class Client */
|
||||||
|
export default class Client {
|
||||||
|
constructor(request) {
|
||||||
|
this._version = 'v1';
|
||||||
|
this._request = request;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Request} request
|
* Get bots
|
||||||
* @constructor
|
* @param {Object} params - Filter's object for bots
|
||||||
*/
|
* @returns {Promise}
|
||||||
function Client(request) {
|
* @memberOf Client
|
||||||
this._version = 'v1';
|
*/
|
||||||
this._request = request;
|
getBots(params = {}) {
|
||||||
|
return this._request.get(this._version + '/bots', params);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get channels
|
||||||
|
* @param {Object} params - Filter's object for channels
|
||||||
|
* @returns {Promise}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
getChannels(params = {}) {
|
||||||
|
return this._request.get(this._version + '/channels', params);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get chats
|
||||||
|
* @param {Object} params - Filter's object for chats
|
||||||
|
* @returns {Promise}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
getChats(params = {}) {
|
||||||
|
return this._request.get(this._version + '/chats', params);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get customers
|
||||||
|
* @param {Object} params - Filter's object for customers
|
||||||
|
* @returns {Promise}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
getCustomers(params = {}) {
|
||||||
|
return this._request.get(this._version + '/customers', params);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get dialogs
|
||||||
|
* @param {Object} params - Filter's object for dialogs
|
||||||
|
* @returns {Promise}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
getDialogs(params = {}) {
|
||||||
|
return this._request.get(this._version + '/dialogs', params);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get members
|
||||||
|
* @param {Object} params - Filter's object for members
|
||||||
|
* @returns {Promise}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
getMembers(params = {}) {
|
||||||
|
return this._request.get(this._version + '/members', params);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assign dialog
|
||||||
|
* @param {Number} dialog_id - Dialog id
|
||||||
|
* @param {Object} dialog - Dialog object
|
||||||
|
* @returns {Promise}
|
||||||
|
* @throws {Error}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
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', {});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close dialog
|
||||||
|
* @param {Number} dialog_id - Dialog id
|
||||||
|
* @returns {Promise}
|
||||||
|
* @throws {Error}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
* @param {Object} message - Message object
|
||||||
|
* @returns {Promise}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
sendMessage(message) {
|
||||||
|
return this._request.post(this._version + '/messages', message);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get messages
|
||||||
|
* @param {Object} params - Filter's object for messages
|
||||||
|
* @returns {Promise}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
getMessages(params = {}) {
|
||||||
|
return this._request.get(this._version + '/messages', params);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete message
|
||||||
|
* @param {Number} message_id - Message id
|
||||||
|
* @returns {Promise}
|
||||||
|
* @throws {Error}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
* @param {Number} message_id - Message id
|
||||||
|
* @param {Object} message - Message object
|
||||||
|
* @returns {Promise}
|
||||||
|
* @throws {Error}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
* @param {Object} params - Filter's object for commands
|
||||||
|
* @returns {Promise}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
getCommands(params = {}) {
|
||||||
|
return this._request.get(this._version + '/my/commands', params);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Edit bot command
|
||||||
|
* @param {string} command_name - Command name
|
||||||
|
* @param {Object} command - Command object
|
||||||
|
* @returns {Promise}
|
||||||
|
* @throws {Error}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
editCommand(command_name, command) {
|
||||||
|
if (!command_name) {
|
||||||
|
throw new Error('Parameter `command_name` is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
return this._request.put(this._version + '/my/commands/' + command_name, command);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete bot command
|
||||||
|
* @param {string} command_name - Command name
|
||||||
|
* @returns {Promise}
|
||||||
|
* @throws {Error}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
* @param {Object} data - Bot data
|
||||||
|
* @returns {Promise}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
info(data) {
|
||||||
|
return this._request.patch(this._version + '/my/info', data);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get users
|
||||||
|
* @param {Object} params - Filter's object for users
|
||||||
|
* @returns {Promise}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
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})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get websocket url
|
||||||
|
* @param {array<string>} events - Array of strings with websocket events
|
||||||
|
* @returns {Map}
|
||||||
|
* @throws {Error}
|
||||||
|
* @memberOf Client
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get bots
|
|
||||||
* @param {string} params
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
Client.prototype.getBots = function (params) {
|
|
||||||
return this._request.get(this._version + '/bots', params);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get channels
|
|
||||||
* @param {string} params
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
Client.prototype.getChannels = function (params) {
|
|
||||||
return this._request.get(this._version + '/channels', params);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get chats
|
|
||||||
* @param {string} params
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
Client.prototype.getChats = function (params) {
|
|
||||||
return this._request.get(this._version + '/chats', params);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get customers
|
|
||||||
* @param {string} params
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
Client.prototype.getCustomers = function (params) {
|
|
||||||
return this._request.get(this._version + '/customers', params);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get dialogs
|
|
||||||
* @param {string} params
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
Client.prototype.getDialogs = function (params) {
|
|
||||||
return this._request.get(this._version + '/dialogs', params);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get members
|
|
||||||
* @param {string} params
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
Client.prototype.getMembers = function (params) {
|
|
||||||
return this._request.get(this._version + '/members', params);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Assign dialog
|
|
||||||
* @param {Number} dialog_id
|
|
||||||
* @param {Object} dialog
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
Client.prototype.assignDialog = function (dialog_id, dialog) {
|
|
||||||
return this._request.patch(this._version + '/dialogs/'+ dialog_id + '/assign', dialog);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close dialog
|
|
||||||
* @param {Number} dialog_id
|
|
||||||
* @returns {Promise}
|
|
||||||
* @throws {Error}
|
|
||||||
*/
|
|
||||||
Client.prototype.closeDialog = function (dialog_id) {
|
|
||||||
if (!dialog_id) {
|
|
||||||
throw new Error('dialog_id is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
return this._request.delete(this._version + '/dialogs/'+ dialog_id + '/close');
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send message
|
|
||||||
* @param {Object} data
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
Client.prototype.sendMessage = function (data) {
|
|
||||||
return this._request.post(this._version + '/messages', data);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get messages
|
|
||||||
* @param {Object} params
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
Client.prototype.getMessages = function (params) {
|
|
||||||
return this._request.get(this._version + '/messages', params);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete message
|
|
||||||
* @param {Number} message_id
|
|
||||||
* @returns {Promise}
|
|
||||||
* @throws {Error}
|
|
||||||
*/
|
|
||||||
Client.prototype.deleteMessage = function (message_id) {
|
|
||||||
if (!message_id) {
|
|
||||||
throw new Error('message_id is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
return this._request.delete(this._version + '/messages/' + message_id);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Edit message
|
|
||||||
* @param {Number} message_id
|
|
||||||
* @param {Object} message
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
Client.prototype.editMessage = function (message_id, message) {
|
|
||||||
return this._request.patch(this._version + '/messages/' + message_id, message);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get bot commands
|
|
||||||
* @param {Object} params
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
Client.prototype.getCommands = function (params) {
|
|
||||||
return this._request.get(this._version + '/my/commands', params);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Edit bot command
|
|
||||||
* @param {string} command_name
|
|
||||||
* @param {Object} data
|
|
||||||
* @returns {Promise}
|
|
||||||
* @throws {Error}
|
|
||||||
*/
|
|
||||||
Client.prototype.editCommand = function (command_name, data) {
|
|
||||||
if (!command_name) {
|
|
||||||
throw new Error('Parameter command name is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
return this._request.put(this._version + '/my/commands/' + command_name, data);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete bot command
|
|
||||||
* @param {string} command_name
|
|
||||||
* @returns {Promise}
|
|
||||||
* @throws {Error}
|
|
||||||
*/
|
|
||||||
Client.prototype.deleteCommand = function (command_name) {
|
|
||||||
if (!command_name) {
|
|
||||||
throw new Error('command_name is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
return this._request.delete(this._version + '/my/commands/' + command_name);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bot information update
|
|
||||||
* @param {Object} data
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
Client.prototype.info = function (data) {
|
|
||||||
return this._request.patch(this._version + '/my/info', data);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get users
|
|
||||||
* @param {Object} params
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
Client.prototype.getUsers = function (params) {
|
|
||||||
return this._request.get(this._version + '/users', params);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get websocket url
|
|
||||||
* @param {array<string>} events
|
|
||||||
* @returns {string}
|
|
||||||
* @throws {Error}
|
|
||||||
*/
|
|
||||||
Client.prototype.getWebsocketUrl = function (events) {
|
|
||||||
if (!events) {
|
|
||||||
throw new Error('Events is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
var url = 'wss://' + this._request.getHost() + '/api/bot/' + this._version + '/ws?events=';
|
|
||||||
|
|
||||||
events.forEach(function (event) {
|
|
||||||
url += event + ',';
|
|
||||||
});
|
|
||||||
|
|
||||||
url = url.slice(0, -1);
|
|
||||||
|
|
||||||
return url;
|
|
||||||
};
|
|
||||||
|
|
35
package.json
35
package.json
|
@ -1,25 +1,42 @@
|
||||||
{
|
{
|
||||||
"name": "mg-api-client",
|
"name": "mg-api-client",
|
||||||
"description": "JS client for retailCRM Bot API",
|
"description": "JavaScript client for Message Gateway Bot API",
|
||||||
"tags": ["API", "retailCRM", "Bot", "Chat"],
|
"tags": [
|
||||||
"version": "1.0.0",
|
"API",
|
||||||
|
"RetailCRM",
|
||||||
|
"Bot",
|
||||||
|
"Chat"
|
||||||
|
],
|
||||||
|
"version": "1.1.0",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "./node_modules/.bin/_mocha ./tests/."
|
"doc": "./node_modules/.bin/jsdoc lib/v1/client.js lib/consts.js -R README.md",
|
||||||
|
"build": "./node_modules/.bin/rollup -c",
|
||||||
|
"test": "./node_modules/.bin/nyc ./node_modules/.bin/_mocha --require @babel/register ./tests/.",
|
||||||
|
"report-coverage": "./node_modules/.bin/nyc report --reporter=text-lcov > coverage.lcov && codecov"
|
||||||
},
|
},
|
||||||
"author": "retailCRM",
|
"author": "RetailCRM",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "index.js",
|
"main": "./dist/index.js",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/retailcrm/mg-bot-api-client-js/issues"
|
"url": "https://github.com/retailcrm/mg-bot-api-client-js/issues"
|
||||||
},
|
},
|
||||||
"dependencies": {},
|
"dependencies": {},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@babel/cli": "^7.10.5",
|
||||||
|
"@babel/core": "^7.10.5",
|
||||||
|
"@babel/preset-env": "^7.10.4",
|
||||||
|
"@babel/register": "^7.10.5",
|
||||||
|
"@rollup/plugin-babel": "^5.2.0",
|
||||||
|
"babelrc-rollup": "^3.0.0",
|
||||||
"chai": "^4.2.0",
|
"chai": "^4.2.0",
|
||||||
"mocha": "^5.2.0",
|
"jsdoc": "^3.5.5",
|
||||||
"nock": "^10.0.6"
|
"mocha": "^8.1.1",
|
||||||
|
"nock": "^10.0.6",
|
||||||
|
"nyc": "^15.1.0",
|
||||||
|
"rollup": "^2.26.3"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 4.0.0"
|
"node": ">= 10.0.0"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/retailcrm/mg-bot-api-client-js#readme",
|
"homepage": "https://github.com/retailcrm/mg-bot-api-client-js#readme",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|
17
rollup.config.js
Normal file
17
rollup.config.js
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import babel from '@rollup/plugin-babel';
|
||||||
|
|
||||||
|
let pluginOptions = [
|
||||||
|
babel({
|
||||||
|
exclude: 'node_modules/**',
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
|
export default [{
|
||||||
|
input: 'index.js',
|
||||||
|
output: {
|
||||||
|
name: 'main',
|
||||||
|
file: 'dist/index.js',
|
||||||
|
format: 'umd',
|
||||||
|
},
|
||||||
|
plugins: pluginOptions,
|
||||||
|
}];
|
|
@ -1,17 +1,16 @@
|
||||||
var nock = require('nock');
|
import chai from 'chai'
|
||||||
var chai = require('chai');
|
import MgBotApiClient from '../index'
|
||||||
var RetailcrmBotApiClient = require('../index');
|
|
||||||
|
|
||||||
describe('#Constructor', function () {
|
describe('#Constructor', function () {
|
||||||
it('Empty url', function () {
|
it('Empty url', function () {
|
||||||
chai.expect(function() {
|
chai.expect(function() {
|
||||||
new RetailcrmBotApiClient({token: 'test_token'});
|
new MgBotApiClient({token: 'test_token'});
|
||||||
}).to.throw('Url is required');
|
}).to.throw('Url is required');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Incorrect url', function () {
|
it('Incorrect url', function () {
|
||||||
chai.expect(function() {
|
chai.expect(function() {
|
||||||
new RetailcrmBotApiClient({
|
new MgBotApiClient({
|
||||||
host: 'http://api.example.com',
|
host: 'http://api.example.com',
|
||||||
token: 'test_token'
|
token: 'test_token'
|
||||||
});
|
});
|
||||||
|
@ -20,7 +19,7 @@ describe('#Constructor', function () {
|
||||||
|
|
||||||
it('Empty token', function () {
|
it('Empty token', function () {
|
||||||
chai.expect(function() {
|
chai.expect(function() {
|
||||||
new RetailcrmBotApiClient({host: 'https://api.example.com'});
|
new MgBotApiClient({host: 'https://api.example.com'});
|
||||||
}).to.throw('Token is required');
|
}).to.throw('Token is required');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
var chai = require('chai');
|
import chai from 'chai'
|
||||||
var request = require('../lib/request');
|
import Request from '../lib/request'
|
||||||
|
|
||||||
describe('#Request', function () {
|
describe('#Request', function () {
|
||||||
var req = new request.Request({
|
let req = new Request({
|
||||||
host: 'http://api.example.com',
|
host: 'http://api.example.com',
|
||||||
token: 'test_token'
|
token: 'test_token'
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
var nock = require('nock');
|
import nock from 'nock'
|
||||||
var chai = require('chai');
|
import chai from 'chai'
|
||||||
var RetailcrmBotApiClient = require('../index');
|
import https from 'https'
|
||||||
|
import MgBotApiClient from '../index'
|
||||||
|
|
||||||
describe('#API client v1', function() {
|
describe('#API client v1', function() {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
nock.cleanAll();
|
nock.cleanAll();
|
||||||
});
|
});
|
||||||
|
|
||||||
var retailcrm = new RetailcrmBotApiClient({
|
const api = new MgBotApiClient({
|
||||||
host: 'https://api.example.com',
|
host: 'https://api.example.com',
|
||||||
token: 'test_token'
|
token: 'test_token'
|
||||||
}).getClient();
|
}).client;
|
||||||
|
|
||||||
it('Get bots list', function() {
|
it('Get bots list', function() {
|
||||||
nock('https://api.example.com/api/bot/v1').get('/bots').reply(200, [{
|
nock('https://api.example.com/api/bot/v1').get('/bots').reply(200, [{
|
||||||
|
@ -18,16 +19,16 @@ describe('#API client v1', function() {
|
||||||
isActive: true
|
isActive: true
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
retailcrm.getBots().then(function (value) {
|
api.getBots().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.not.empty;
|
chai.expect(value).to.be.not.empty;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Get empty bots list', function () {
|
it('Get empty bots list', function () {
|
||||||
nock('https://api.example.com/api/bot/v1').get('/bots').reply(200, []);
|
nock('https://api.example.com/api/bot/v1').get('/bots?id=1').reply(200, []);
|
||||||
|
|
||||||
retailcrm.getBots().then(function (value) {
|
api.getBots({id: 1}).then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.empty;
|
chai.expect(value).to.be.empty;
|
||||||
});
|
});
|
||||||
|
@ -38,7 +39,7 @@ describe('#API client v1', function() {
|
||||||
id: 1
|
id: 1
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
retailcrm.getChannels().then(function (value) {
|
api.getChannels().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.not.empty;
|
chai.expect(value).to.be.not.empty;
|
||||||
});
|
});
|
||||||
|
@ -47,7 +48,7 @@ describe('#API client v1', function() {
|
||||||
it('Get empty channels list', function () {
|
it('Get empty channels list', function () {
|
||||||
nock('https://api.example.com/api/bot/v1').get('/channels').reply(200, []);
|
nock('https://api.example.com/api/bot/v1').get('/channels').reply(200, []);
|
||||||
|
|
||||||
retailcrm.getChannels().then(function (value) {
|
api.getChannels().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.empty;
|
chai.expect(value).to.be.empty;
|
||||||
});
|
});
|
||||||
|
@ -59,7 +60,7 @@ describe('#API client v1', function() {
|
||||||
id: 1
|
id: 1
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
retailcrm.getChats().then(function (value) {
|
api.getChats().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.not.empty;
|
chai.expect(value).to.be.not.empty;
|
||||||
});
|
});
|
||||||
|
@ -68,7 +69,7 @@ describe('#API client v1', function() {
|
||||||
it('Get empty chats list', function () {
|
it('Get empty chats list', function () {
|
||||||
nock('https://api.example.com/api/bot/v1').get('/chats').reply(200, []);
|
nock('https://api.example.com/api/bot/v1').get('/chats').reply(200, []);
|
||||||
|
|
||||||
retailcrm.getChats().then(function (value) {
|
api.getChats().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.empty;
|
chai.expect(value).to.be.empty;
|
||||||
});
|
});
|
||||||
|
@ -81,7 +82,7 @@ describe('#API client v1', function() {
|
||||||
id: 1
|
id: 1
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
retailcrm.getCustomers().then(function (value) {
|
api.getCustomers().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.not.empty;
|
chai.expect(value).to.be.not.empty;
|
||||||
});
|
});
|
||||||
|
@ -90,7 +91,7 @@ describe('#API client v1', function() {
|
||||||
it('Get empty customers list', function () {
|
it('Get empty customers list', function () {
|
||||||
nock('https://api.example.com/api/bot/v1').get('/customers').reply(200, []);
|
nock('https://api.example.com/api/bot/v1').get('/customers').reply(200, []);
|
||||||
|
|
||||||
retailcrm.getCustomers().then(function (value) {
|
api.getCustomers().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.empty;
|
chai.expect(value).to.be.empty;
|
||||||
});
|
});
|
||||||
|
@ -102,7 +103,7 @@ describe('#API client v1', function() {
|
||||||
id: 1
|
id: 1
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
retailcrm.getDialogs().then(function (value) {
|
api.getDialogs().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.not.empty;
|
chai.expect(value).to.be.not.empty;
|
||||||
});
|
});
|
||||||
|
@ -111,7 +112,7 @@ describe('#API client v1', function() {
|
||||||
it('Get empty dialogs list', function () {
|
it('Get empty dialogs list', function () {
|
||||||
nock('https://api.example.com/api/bot/v1').get('/dialogs').reply(200, []);
|
nock('https://api.example.com/api/bot/v1').get('/dialogs').reply(200, []);
|
||||||
|
|
||||||
retailcrm.getDialogs().then(function (value) {
|
api.getDialogs().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.empty;
|
chai.expect(value).to.be.empty;
|
||||||
});
|
});
|
||||||
|
@ -122,7 +123,7 @@ describe('#API client v1', function() {
|
||||||
id: 1
|
id: 1
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
retailcrm.getMembers().then(function (value) {
|
api.getMembers().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.not.empty;
|
chai.expect(value).to.be.not.empty;
|
||||||
});
|
});
|
||||||
|
@ -131,7 +132,7 @@ describe('#API client v1', function() {
|
||||||
it('Get empty members list', function () {
|
it('Get empty members list', function () {
|
||||||
nock('https://api.example.com/api/bot/v1').get('/members').reply(200, []);
|
nock('https://api.example.com/api/bot/v1').get('/members').reply(200, []);
|
||||||
|
|
||||||
retailcrm.getMembers().then(function (value) {
|
api.getMembers().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.empty;
|
chai.expect(value).to.be.empty;
|
||||||
});
|
});
|
||||||
|
@ -145,7 +146,7 @@ describe('#API client v1', function() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
retailcrm.assignDialog(1, {
|
api.assignDialog(1, {
|
||||||
manager_id: 1
|
manager_id: 1
|
||||||
}).then(function (value) {
|
}).then(function (value) {
|
||||||
chai.expect(value).to.be.an('object');
|
chai.expect(value).to.be.an('object');
|
||||||
|
@ -153,19 +154,33 @@ describe('#API client v1', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Assign dialog incorrect', function () {
|
it('Assign dialog incorrect', function () {
|
||||||
chai.expect(retailcrm.assignDialog.bind(retailcrm)).to.throw('Body is not be empty');
|
chai.expect(api.assignDialog.bind(api)).to.throw('Parameter `dialog_id` is required');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Unassign dialog', function () {
|
||||||
|
nock('https://api.example.com/api/bot/v1').patch('/dialogs/1/unassign').reply(200, {
|
||||||
|
previous_responsible: {type: 'user', id: 1, assigned_at: '2019-01-22T11:50:13Z'}
|
||||||
|
});
|
||||||
|
|
||||||
|
api.unassignDialog(1).then(function (value) {
|
||||||
|
chai.expect(value).to.be.an('object');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Unassign dialog incorrect', function () {
|
||||||
|
chai.expect(api.unassignDialog.bind(api)).to.throw('Parameter `dialog_id` is required');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Close dialog', function () {
|
it('Close dialog', function () {
|
||||||
nock('https://api.example.com/api/bot/v1').delete('/dialogs/1/close').reply(200, {});
|
nock('https://api.example.com/api/bot/v1').delete('/dialogs/1/close').reply(200, {});
|
||||||
|
|
||||||
retailcrm.closeDialog(1).then(function (value) {
|
api.closeDialog(1).then(function (value) {
|
||||||
chai.expect(value).to.be.empty;
|
chai.expect(value).to.be.empty;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Close dialog incorrect', function () {
|
it('Close dialog incorrect', function () {
|
||||||
chai.expect(retailcrm.closeDialog.bind(retailcrm)).to.throw('dialog_id is required');
|
chai.expect(api.closeDialog.bind(api)).to.throw('Parameter `dialog_id` is required');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Send message', function () {
|
it('Send message', function () {
|
||||||
|
@ -178,7 +193,7 @@ describe('#API client v1', function() {
|
||||||
message_id: 1
|
message_id: 1
|
||||||
});
|
});
|
||||||
|
|
||||||
retailcrm.sendMessage({
|
api.sendMessage({
|
||||||
chat_id: 1,
|
chat_id: 1,
|
||||||
scope: 'public',
|
scope: 'public',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
|
@ -190,7 +205,7 @@ describe('#API client v1', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Send message incorrect', function () {
|
it('Send message incorrect', function () {
|
||||||
chai.expect(retailcrm.sendMessage.bind(retailcrm)).to.throw('Body is not be empty');
|
chai.expect(api.sendMessage.bind(api)).to.throw('Body is not be empty');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Get messages', function() {
|
it('Get messages', function() {
|
||||||
|
@ -202,7 +217,7 @@ describe('#API client v1', function() {
|
||||||
}
|
}
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
retailcrm.getMessages().then(function (value) {
|
api.getMessages().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.not.empty;
|
chai.expect(value).to.be.not.empty;
|
||||||
});
|
});
|
||||||
|
@ -211,7 +226,7 @@ describe('#API client v1', function() {
|
||||||
it('Get empty messages', function () {
|
it('Get empty messages', function () {
|
||||||
nock('https://api.example.com/api/bot/v1').get('/messages').reply(200, []);
|
nock('https://api.example.com/api/bot/v1').get('/messages').reply(200, []);
|
||||||
|
|
||||||
retailcrm.getMessages().then(function (value) {
|
api.getMessages().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.empty;
|
chai.expect(value).to.be.empty;
|
||||||
});
|
});
|
||||||
|
@ -220,13 +235,13 @@ describe('#API client v1', function() {
|
||||||
it('Delete message', function () {
|
it('Delete message', function () {
|
||||||
nock('https://api.example.com/api/bot/v1').delete('/messages/1').reply(200, {});
|
nock('https://api.example.com/api/bot/v1').delete('/messages/1').reply(200, {});
|
||||||
|
|
||||||
retailcrm.deleteMessage(1).then(function (value) {
|
api.deleteMessage(1).then(function (value) {
|
||||||
chai.expect(value).to.be.empty;
|
chai.expect(value).to.be.empty;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Delete message incorrect', function () {
|
it('Delete message incorrect', function () {
|
||||||
chai.expect(retailcrm.deleteMessage.bind(retailcrm)).to.throw('message_id is required');
|
chai.expect(api.deleteMessage.bind(api)).to.throw('Parameter `message_id` is required');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Edit message', function () {
|
it('Edit message', function () {
|
||||||
|
@ -234,7 +249,7 @@ describe('#API client v1', function() {
|
||||||
content: 'tests message'
|
content: 'tests message'
|
||||||
}).reply(200, {});
|
}).reply(200, {});
|
||||||
|
|
||||||
retailcrm.editMessage(1, {
|
api.editMessage(1, {
|
||||||
content: 'tests message'
|
content: 'tests message'
|
||||||
}).then(function (value) {
|
}).then(function (value) {
|
||||||
chai.expect(value).to.be.empty;
|
chai.expect(value).to.be.empty;
|
||||||
|
@ -242,7 +257,7 @@ describe('#API client v1', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Edit message incorrect', function () {
|
it('Edit message incorrect', function () {
|
||||||
chai.expect(retailcrm.editMessage.bind(retailcrm)).to.throw('Body is not be empty');
|
chai.expect(api.editMessage.bind(api)).to.throw('Parameter `message_id` is required');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Get commands', function () {
|
it('Get commands', function () {
|
||||||
|
@ -251,7 +266,7 @@ describe('#API client v1', function() {
|
||||||
name: 'Command name'
|
name: 'Command name'
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
retailcrm.getCommands().then(function (value) {
|
api.getCommands().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.not.empty;
|
chai.expect(value).to.be.not.empty;
|
||||||
});
|
});
|
||||||
|
@ -260,7 +275,7 @@ describe('#API client v1', function() {
|
||||||
it('Get empty commands', function () {
|
it('Get empty commands', function () {
|
||||||
nock('https://api.example.com/api/bot/v1').get('/my/commands').reply(200, []);
|
nock('https://api.example.com/api/bot/v1').get('/my/commands').reply(200, []);
|
||||||
|
|
||||||
retailcrm.getCommands().then(function (value) {
|
api.getCommands().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.empty;
|
chai.expect(value).to.be.empty;
|
||||||
});
|
});
|
||||||
|
@ -272,7 +287,7 @@ describe('#API client v1', function() {
|
||||||
name: 'name'
|
name: 'name'
|
||||||
}).reply(200, {});
|
}).reply(200, {});
|
||||||
|
|
||||||
retailcrm.editCommand('command', {
|
api.editCommand('command', {
|
||||||
description: 'Desc',
|
description: 'Desc',
|
||||||
name: 'name'
|
name: 'name'
|
||||||
}).then(function (value) {
|
}).then(function (value) {
|
||||||
|
@ -281,20 +296,20 @@ describe('#API client v1', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Edit command incorrect', function () {
|
it('Edit command incorrect', function () {
|
||||||
chai.expect(retailcrm.editCommand.bind(retailcrm, 'command')).to.throw('Body is not be empty');
|
chai.expect(api.editCommand.bind(api, 'command')).to.throw('Body is not be empty');
|
||||||
chai.expect(retailcrm.editCommand.bind(retailcrm)).to.throw('Parameter command name is required');
|
chai.expect(api.editCommand.bind(api)).to.throw('Parameter `command_name` is required');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Delete command', function () {
|
it('Delete command', function () {
|
||||||
nock('https://api.example.com/api/bot/v1').delete('/my/commands/command').reply(200, {});
|
nock('https://api.example.com/api/bot/v1').delete('/my/commands/command').reply(200, {});
|
||||||
|
|
||||||
retailcrm.deleteCommand('command').then(function (value) {
|
api.deleteCommand('command').then(function (value) {
|
||||||
chai.expect(value).to.be.empty;
|
chai.expect(value).to.be.empty;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Delete command incorrect', function () {
|
it('Delete command incorrect', function () {
|
||||||
chai.expect(retailcrm.deleteCommand.bind(retailcrm)).to.throw('command_name is required');
|
chai.expect(api.deleteCommand.bind(api)).to.throw('Parameter `command_name` is required');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Update bot info', function () {
|
it('Update bot info', function () {
|
||||||
|
@ -303,7 +318,7 @@ describe('#API client v1', function() {
|
||||||
name: 'Bot'
|
name: 'Bot'
|
||||||
}).reply(200, {});
|
}).reply(200, {});
|
||||||
|
|
||||||
retailcrm.info({
|
api.info({
|
||||||
avatar_url: 'http://tests.ru/avatar.png',
|
avatar_url: 'http://tests.ru/avatar.png',
|
||||||
name: 'Bot'
|
name: 'Bot'
|
||||||
}).then(function (value) {
|
}).then(function (value) {
|
||||||
|
@ -312,7 +327,7 @@ describe('#API client v1', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Update bot info incorrect', function () {
|
it('Update bot info incorrect', function () {
|
||||||
chai.expect(retailcrm.info.bind(retailcrm)).to.throw('Body is not be empty');
|
chai.expect(api.info.bind(api)).to.throw('Body is not be empty');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Get users', function () {
|
it('Get users', function () {
|
||||||
|
@ -321,7 +336,7 @@ describe('#API client v1', function() {
|
||||||
name: 'Username'
|
name: 'Username'
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
retailcrm.getUsers().then(function (value) {
|
api.getUsers().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.not.empty;
|
chai.expect(value).to.be.not.empty;
|
||||||
});
|
});
|
||||||
|
@ -330,20 +345,99 @@ describe('#API client v1', function() {
|
||||||
it('Get empty users', function () {
|
it('Get empty users', function () {
|
||||||
nock('https://api.example.com/api/bot/v1').get('/users').reply(200, []);
|
nock('https://api.example.com/api/bot/v1').get('/users').reply(200, []);
|
||||||
|
|
||||||
retailcrm.getUsers().then(function (value) {
|
api.getUsers().then(function (value) {
|
||||||
chai.expect(value).to.be.an('array');
|
chai.expect(value).to.be.an('array');
|
||||||
chai.expect(value).to.be.empty;
|
chai.expect(value).to.be.empty;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Get websocket url', function () {
|
it('Get file', function () {
|
||||||
var url = retailcrm.getWebsocketUrl(['message_new', 'message_updated']);
|
nock('https://api.example.com/api/bot/v1').get('/files/1').reply(200, {
|
||||||
var expected = 'wss://api.example.com/api/bot/v1/ws?events=message_new,message_updated';
|
id: '1',
|
||||||
|
size: 100,
|
||||||
|
type: 'image',
|
||||||
|
url: 'https://file.url'
|
||||||
|
});
|
||||||
|
|
||||||
chai.assert.equal(url, expected);
|
api.getFile('1').then(function (value) {
|
||||||
|
chai.expect(value).to.be.an('object');
|
||||||
|
chai.expect(value.id).to.be.equal('1');
|
||||||
|
chai.expect(value.size).to.be.equal(100);
|
||||||
|
chai.expect(value.type).to.be.equal('image');
|
||||||
|
chai.expect(value.url).to.be.equal('https://file.url');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Get file incorrect', function () {
|
||||||
|
chai.expect(api.getFile.bind(api)).to.throw('Parameter `file_id` is required');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('File upload', function () {
|
||||||
|
const options = {
|
||||||
|
host: 'via.placeholder.com',
|
||||||
|
path: '/300'
|
||||||
|
};
|
||||||
|
|
||||||
|
const req = https.get(options, function (res) {
|
||||||
|
let data = Buffer.from('', 'binary');
|
||||||
|
res.on('data', function (chunk) {
|
||||||
|
data = Buffer.concat([data, Buffer.from(chunk, 'binary')])
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('end', function () {
|
||||||
|
nock('https://api.example.com/api/bot/v1').post('/files/upload', data).reply(200, {
|
||||||
|
id: '1',
|
||||||
|
size: 1132,
|
||||||
|
type: 'image'
|
||||||
|
});
|
||||||
|
|
||||||
|
api.filesUpload(data).then(function (value) {
|
||||||
|
chai.expect(value).to.be.an('object');
|
||||||
|
chai.expect(value.id).to.be.equal('1');
|
||||||
|
chai.expect(value.size).to.be.equal(1132);
|
||||||
|
chai.expect(value.type).to.be.equal('image');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('File upload incorrect', function () {
|
||||||
|
chai.expect(api.filesUpload.bind(api)).to.throw('Body is not be empty');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('File upload by url', function () {
|
||||||
|
nock('https://api.example.com/api/bot/v1').post('/files/upload_by_url', {url: 'https://fileurl.com'}).reply(200, {
|
||||||
|
id: '123',
|
||||||
|
size: 1132,
|
||||||
|
type: 'image',
|
||||||
|
url: 'https://file.url'
|
||||||
|
});
|
||||||
|
|
||||||
|
api.filesUploadByUrl('https://fileurl.com').then(function (value) {
|
||||||
|
chai.expect(value).to.be.an('object');
|
||||||
|
chai.expect(value.id).to.be.equal('123');
|
||||||
|
chai.expect(value.size).to.be.equal(1132);
|
||||||
|
chai.expect(value.type).to.be.equal('image');
|
||||||
|
chai.expect(value.url).to.be.equal('https://file.url');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('File upload by url incorrect', function () {
|
||||||
|
chai.expect(api.filesUploadByUrl.bind(api)).to.throw('Parameter `url` is required');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Get websocket data', function () {
|
||||||
|
const wsData = api.getWebsocketData([MgBotApiClient.types().wsMessageNew, MgBotApiClient.types().wsMessageUpdated]);
|
||||||
|
const expectedUrl = 'wss://api.example.com/api/bot/v1/ws?events=message_new,message_updated';
|
||||||
|
const expectedHeaders = {'X-Bot-Token': 'test_token'};
|
||||||
|
|
||||||
|
chai.assert.equal(wsData.get('url'), expectedUrl);
|
||||||
|
chai.assert.equal(wsData.get('headers')["X-Bot-Token"], expectedHeaders["X-Bot-Token"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Get websocket url incorrect', function () {
|
it('Get websocket url incorrect', function () {
|
||||||
chai.expect(retailcrm.getWebsocketUrl.bind(retailcrm)).to.throw('Events is required');
|
chai.expect(api.getWebsocketData.bind(api)).to.throw('Events is required');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue