1
0
mirror of synced 2024-11-22 04:26:02 +03:00
mg-bot-api-client-js/README.md

101 lines
2.4 KiB
Markdown
Raw Normal View History

2019-02-08 14:53:02 +03:00
[![Build Status](https://img.shields.io/travis/retailcrm/mg-bot-api-client-js/master.svg?logo=travis&style=flat-square)](https://travis-ci.org/retailcrm/mg-bot-api-client-js)
[![GitHub release](https://img.shields.io/github/release/retailcrm/mg-bot-api-client-js.svg?style=flat-square)](https://github.com/retailcrm/mg-bot-api-client-js/releases)
2019-02-08 15:43:30 +03:00
[![Node version](https://img.shields.io/node/v/mg-api-client.svg?style=flat-square)](https://www.npmjs.com/package/mg-api-client)
2019-02-08 14:53:02 +03:00
2018-09-04 10:26:32 +03:00
# retailCRM Message Gateway Bot API JS client
2019-02-08 14:37:43 +03:00
This is js retailCRM bot API client.
2019-02-06 14:54:15 +03:00
# Installation
```
2019-02-08 15:43:30 +03:00
npm install --save mg-api-client
2019-02-06 14:54:15 +03:00
```
In your file
2019-03-06 14:03:41 +03:00
###### CommonJS
2019-02-06 14:54:15 +03:00
```
2019-02-08 15:43:30 +03:00
var RetailcrmBotApiClient = require('mg-api-client');
2019-02-06 14:54:15 +03:00
```
2019-03-06 14:03:41 +03:00
###### es6
```
import RetailcrmBotApiClient from 'mg-api-client';
```
2019-02-06 14:54:15 +03:00
# Usage
#### Get users
```javascript
2019-03-01 13:46:30 +03:00
const api = new RetailcrmBotApiClient({
2019-02-06 14:54:15 +03:00
host: 'https://api.example.com',
token: 'your bot token',
apiVersion: 'v1' // optional
2019-03-01 13:46:30 +03:00
}).client;
2019-02-06 14:54:15 +03:00
api.getUsers()
.then(function (users) {
console.log(users);
})
.catch(function (e) {
console.log(e);
});
```
#### Send message
```javascript
2019-03-01 13:46:30 +03:00
const api = new RetailcrmBotApiClient({
2019-02-06 14:54:15 +03:00
host: 'https://api.example.com',
token: 'your bot token',
apiVersion: 'v1' // optional
2019-03-01 13:46:30 +03:00
}).client;
2019-02-06 14:54:15 +03:00
2019-03-01 13:46:30 +03:00
let message = {
2019-02-06 14:54:15 +03:00
chat_id: 1,
content: 'Text message',
scope: 'public',
type: 'text'
2019-02-08 13:31:38 +03:00
};
2019-02-06 14:54:15 +03:00
api.sendMessage(message)
.then(function (result) {
console.log(result);
})
.catch(function (e) {
console.log(e);
});
```
2019-03-01 13:46:30 +03:00
#### Websocket Example
```javascript
const WebSocket = require('ws');
const api = new RetailcrmBotApiClient({
host: 'https://api.example.com',
token: 'your bot token',
apiVersion: 'v1' // optional
}).client;
2019-03-06 14:03:41 +03:00
const wsData = api.getWebsocketData([RetailcrmBotApiClient.types().wsMessageNew]);
2019-03-01 13:46:30 +03:00
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);
})
}
});
```