Skip to content

Commit 1e08c6c

Browse files
committed
docs: add document about websocket bot
1 parent 924f644 commit 1e08c6c

File tree

4 files changed

+102
-4
lines changed
  • website
    • docs/advanced-usage/openapp
    • i18n/zh-Hans/docusaurus-plugin-content-docs/current/advanced-usage/openapp

4 files changed

+102
-4
lines changed

website/docs/advanced-usage/openapp/bot.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ At this point we execute `node server.js` to see that our application will be st
7171
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:
7272

7373
```js
74-
import { TailchatClient, stripMentionTag } from 'tailchat-client-sdk';
74+
import { TailchatHTTPClient, stripMentionTag } from 'tailchat-client-sdk';
7575

7676
const host = '<your tailchat instance backend host>';
7777
const appId = '<appId>';
7878
const appSecret = '<appSecret>';
7979

80-
const client = new TailchatClient(host, appId, appSecret)
80+
const client = new TailchatHTTPClient(host, appId, appSecret)
8181

8282
// ...
8383

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
sidebar_position: 4
3+
title: Websocket Bot
4+
---
5+
6+
In addition to traditional HTTP callback bots, we also support bots based on the websocket long connection protocol.
7+
8+
The long-connection robot can listen to all messages like a normal user, and does not need to be invoke by `@`.
9+
10+
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.
11+
12+
Here is an example in here:
13+
14+
```ts
15+
import { TailchatWsClient } from 'tailchat-client-sdk';
16+
17+
const HOST = process.env.HOST;
18+
const APPID = process.env.APPID;
19+
const APPSECRET = process.env.APPSECRET;
20+
21+
if (!HOST || !APPID || !APPSECRET) {
22+
console.log('require env: HOST, APPID, APPSECRET');
23+
process. exit(1);
24+
}
25+
26+
const client = new TailchatWsClient(HOST, APPID, APPSECRET);
27+
28+
client.connect().then(async() => {
29+
console.log('Login Success!');
30+
31+
client.onMessage((message) => {
32+
console.log('Receive message', message);
33+
});
34+
});
35+
```
36+
37+
Among them, `HOST`, `APPID`, `APPSECRET` represent the server address, the id and secret key of the open platform respectively.
38+
39+
**Please do not upload your secret key on the public platform, the secret key is equivalent to the password**
40+
41+
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.
42+
43+
Similarly, if we need to subscribe the update event of the message, we can use the subscribe message update operation
44+
45+
```ts
46+
client.onMessageUpdate((message) => {
47+
console.log('Receive message', message);
48+
});
49+
```

website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/advanced-usage/openapp/bot.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ app.listen(3000, () => {
7171
现在我们要增加一些逻辑,比如我们想要实现一个复读机器人, 那么修改 `/bot/callback` 路由的实现如下:
7272

7373
```js
74-
import { TailchatClient, stripMentionTag } from 'tailchat-client-sdk';
74+
import { TailchatHTTPClient, stripMentionTag } from 'tailchat-client-sdk';
7575

7676
const host = '<your tailchat instance backend host>';
7777
const appId = '<appId>';
7878
const appSecret = '<appSecret>';
7979

80-
const client = new TailchatClient(host, appId, appSecret)
80+
const client = new TailchatHTTPClient(host, appId, appSecret)
8181

8282
// ...
8383

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
sidebar_position: 4
3+
title: Websocket 机器人
4+
---
5+
6+
除了传统的HTTP回调机器人以外,我们还支持基于websocket长连接协议的机器人。
7+
8+
长连接机器人可以如正常用户一样监听所有的消息,并无需通过 `@` 来唤起。
9+
10+
当然,缺点在于在一些只支持http请求的平台如`serverless`等不支持`websocket`的平台不够便利。且目前只有`nodejs`版本的实现。
11+
12+
以下是一个操作示例:
13+
14+
```ts
15+
import { TailchatWsClient } from 'tailchat-client-sdk';
16+
17+
const HOST = process.env.HOST;
18+
const APPID = process.env.APPID;
19+
const APPSECRET = process.env.APPSECRET;
20+
21+
if (!HOST || !APPID || !APPSECRET) {
22+
console.log('require env: HOST, APPID, APPSECRET');
23+
process.exit(1);
24+
}
25+
26+
const client = new TailchatWsClient(HOST, APPID, APPSECRET);
27+
28+
client.connect().then(async () => {
29+
console.log('Login Success!');
30+
31+
client.onMessage((message) => {
32+
console.log('Receive message', message);
33+
});
34+
});
35+
```
36+
37+
其中 `HOST`, `APPID`, `APPSECRET` 分别表示服务器地址,开放平台的id与秘钥。
38+
39+
**注意请不要在公共平台上泄露你的秘钥,秘钥等价于密码**
40+
41+
该操作在连接到服务器后创建了一个事件监听,当接收到任何消息的时候会把message的内容打印出来。
42+
43+
类似的,如果我们需要监听消息的更新事件的话,则可以使用监听消息更新操作
44+
45+
```ts
46+
client.onMessageUpdate((message) => {
47+
console.log('Receive message', message);
48+
});
49+
```

0 commit comments

Comments
 (0)