1
0
mirror of synced 2024-11-21 20:16:03 +03:00
mg-bot-api-client-js/README.md

100 lines
2.6 KiB
Markdown
Raw Permalink Normal View History

2020-11-26 16:19:29 +03:00
[![Build Status](https://github.com/retailcrm/mg-bot-api-client-js/workflows/ci/badge.svg)](https://github.com/retailcrm/mg-bot-api-client-js/actions)
[![Coverage](https://img.shields.io/codecov/c/gh/retailcrm/mg-bot-api-client-js/master.svg?logo=codecov&logoColor=white)](https://codecov.io/gh/retailcrm/mg-bot-api-client-js)
[![Latest stable](https://img.shields.io/npm/v/mg-api-client.svg?logo=npm&logoColor=white)](https://npmjs.com/package/mg-api-client)
[![Node version](https://img.shields.io/node/v/mg-api-client.svg?logo=node.js&logoColor=white)](https://www.npmjs.com/package/mg-api-client)
2020-11-26 16:19:29 +03:00
[![JS Doc](https://img.shields.io/badge/doc-github_pages-green)](https://retailcrm.github.io/mg-bot-api-client-js/)
2019-02-08 14:53:02 +03:00
# Message Gateway Bot API JavaScript 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-03-06 16:38:47 +03:00
var MgBotApiClient = require('mg-api-client');
2019-02-06 14:54:15 +03:00
```
2019-03-06 14:03:41 +03:00
###### es6
```
2019-03-06 16:38:47 +03:00
import MgBotApiClient from 'mg-api-client';
2019-03-06 14:03:41 +03:00
```
2019-02-06 14:54:15 +03:00
# Usage
#### Get users
```javascript
2019-03-06 16:38:47 +03:00
const api = new MgBotApiClient({
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-06 16:38:47 +03:00
const api = new MgBotApiClient({
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');
2019-03-06 16:38:47 +03:00
const api = new MgBotApiClient({
2019-03-01 13:46:30 +03:00
host: 'https://api.example.com',
token: 'your bot token',
apiVersion: 'v1' // optional
}).client;
2019-03-06 16:38:47 +03:00
const wsData = api.getWebsocketData([MgBotApiClient.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);
})
}
});
```