1
0
mirror of synced 2024-11-21 20:16:03 +03:00
JS client for MG Bot API
Go to file
2020-11-26 16:23:46 +03:00
.github/workflows install dependencies for gh pages and deploy (#12) 2020-11-26 16:23:46 +03:00
lib upload files methods, unussign dialog method (#10) 2020-08-24 11:17:55 +03:00
tests upload files methods, unussign dialog method (#10) 2020-08-24 11:17:55 +03:00
.babelrc es6 syntax 2019-03-01 13:46:30 +03:00
.gitignore code coverage 2020-08-18 12:13:45 +03:00
.npmignore upload files methods, unussign dialog method (#10) 2020-08-24 11:17:55 +03:00
index.js JSDoc 2019-03-06 14:03:41 +03:00
LICENSE Initial commit 2018-09-04 10:25:53 +03:00
package.json upload files methods, unussign dialog method (#10) 2020-08-24 11:17:55 +03:00
README.md Move CI to GitHub Actions (#11) 2020-11-26 16:19:29 +03:00
rollup.config.js update devDependencies (#9) 2020-08-18 17:40:00 +03:00

Build Status Coverage Latest stable Node version JS Doc

Message Gateway Bot API JS client

This is js retailCRM bot API client.

Installation

npm install --save mg-api-client

In your file

CommonJS
var MgBotApiClient = require('mg-api-client');
es6
import MgBotApiClient from 'mg-api-client';

Usage

Get users

const api = new MgBotApiClient({
    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 MgBotApiClient({
    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 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);
        })
    }
});