2.3 KiB
2.3 KiB
retailCRM Message Gateway Bot API JS client
This is js retailCRM bot API client.
Installation
npm install --save mg-api-client
In your file
var RetailcrmBotApiClient = require('mg-api-client');
Usage
Get users
const api = new RetailcrmBotApiClient({
host: 'https://api.example.com',
token: 'your bot token',
apiVersion: 'v1' // optional
}).client;
api.getUsers()
.then(function (users) {
console.log(users);
})
.catch(function (e) {
console.log(e);
});
Send message
const api = new RetailcrmBotApiClient({
host: 'https://api.example.com',
token: 'your bot token',
apiVersion: 'v1' // optional
}).client;
let message = {
chat_id: 1,
content: 'Text message',
scope: 'public',
type: 'text'
};
api.sendMessage(message)
.then(function (result) {
console.log(result);
})
.catch(function (e) {
console.log(e);
});
Websocket Example
const WebSocket = require('ws');
const api = new RetailcrmBotApiClient({
host: 'https://api.example.com',
token: 'your bot token',
apiVersion: 'v1' // optional
}).client;
const wsData = api.getWebsocketData(['message_new']);
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);
})
}
});