docs: add document about websocket bot

pull/128/head
moonrailgun 2 years ago
parent 924f644d49
commit 1e08c6c49f

@ -71,13 +71,13 @@ At this point we execute `node server.js` to see that our application will be st
Now we need to add some logic, for example, if we want to implement a repeating bot, then modify the implementation of `/bot/callback` route as follows:
```js
import { TailchatClient, stripMentionTag } from 'tailchat-client-sdk';
import { TailchatHTTPClient, stripMentionTag } from 'tailchat-client-sdk';
const host = '<your tailchat instance backend host>';
const appId = '<appId>';
const appSecret = '<appSecret>';
const client = new TailchatClient(host, appId, appSecret)
const client = new TailchatHTTPClient(host, appId, appSecret)
// ...

@ -0,0 +1,49 @@
---
sidebar_position: 4
title: Websocket Bot
---
In addition to traditional HTTP callback bots, we also support bots based on the websocket long connection protocol.
The long-connection robot can listen to all messages like a normal user, and does not need to be invoke by `@`.
Of course, the disadvantage is that it is not convenient for some platforms that only support http requests, such as `serverless` and other platforms that do not support `websocket`. And currently only the `nodejs` version is implemented.
Here is an example in here:
```ts
import { TailchatWsClient } from 'tailchat-client-sdk';
const HOST = process.env.HOST;
const APPID = process.env.APPID;
const APPSECRET = process.env.APPSECRET;
if (!HOST || !APPID || !APPSECRET) {
console.log('require env: HOST, APPID, APPSECRET');
process. exit(1);
}
const client = new TailchatWsClient(HOST, APPID, APPSECRET);
client.connect().then(async() => {
console.log('Login Success!');
client.onMessage((message) => {
console.log('Receive message', message);
});
});
```
Among them, `HOST`, `APPID`, `APPSECRET` represent the server address, the id and secret key of the open platform respectively.
**Please do not upload your secret key on the public platform, the secret key is equivalent to the password**
This operation creates an event listener after connecting to the server, and when any message is received, the content of the message will be printed out.
Similarly, if we need to subscribe the update event of the message, we can use the subscribe message update operation
```ts
client.onMessageUpdate((message) => {
console.log('Receive message', message);
});
```

@ -71,13 +71,13 @@ app.listen(3000, () => {
现在我们要增加一些逻辑,比如我们想要实现一个复读机器人, 那么修改 `/bot/callback` 路由的实现如下:
```js
import { TailchatClient, stripMentionTag } from 'tailchat-client-sdk';
import { TailchatHTTPClient, stripMentionTag } from 'tailchat-client-sdk';
const host = '<your tailchat instance backend host>';
const appId = '<appId>';
const appSecret = '<appSecret>';
const client = new TailchatClient(host, appId, appSecret)
const client = new TailchatHTTPClient(host, appId, appSecret)
// ...

@ -0,0 +1,49 @@
---
sidebar_position: 4
title: Websocket 机器人
---
除了传统的HTTP回调机器人以外我们还支持基于websocket长连接协议的机器人。
长连接机器人可以如正常用户一样监听所有的消息,并无需通过 `@` 来唤起。
当然缺点在于在一些只支持http请求的平台如`serverless`等不支持`websocket`的平台不够便利。且目前只有`nodejs`版本的实现。
以下是一个操作示例:
```ts
import { TailchatWsClient } from 'tailchat-client-sdk';
const HOST = process.env.HOST;
const APPID = process.env.APPID;
const APPSECRET = process.env.APPSECRET;
if (!HOST || !APPID || !APPSECRET) {
console.log('require env: HOST, APPID, APPSECRET');
process.exit(1);
}
const client = new TailchatWsClient(HOST, APPID, APPSECRET);
client.connect().then(async () => {
console.log('Login Success!');
client.onMessage((message) => {
console.log('Receive message', message);
});
});
```
其中 `HOST`, `APPID`, `APPSECRET` 分别表示服务器地址开放平台的id与秘钥。
**注意请不要在公共平台上泄露你的秘钥,秘钥等价于密码**
该操作在连接到服务器后创建了一个事件监听当接收到任何消息的时候会把message的内容打印出来。
类似的,如果我们需要监听消息的更新事件的话,则可以使用监听消息更新操作
```ts
client.onMessageUpdate((message) => {
console.log('Receive message', message);
});
```
Loading…
Cancel
Save