From 1e08c6c49f51c09b180e2f155b907309bd134ed0 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 23 Jul 2023 15:33:30 +0800 Subject: [PATCH] docs: add document about websocket bot --- website/docs/advanced-usage/openapp/bot.md | 4 +- website/docs/advanced-usage/openapp/ws.md | 49 +++++++++++++++++++ .../current/advanced-usage/openapp/bot.md | 4 +- .../current/advanced-usage/openapp/ws.md | 49 +++++++++++++++++++ 4 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 website/docs/advanced-usage/openapp/ws.md create mode 100644 website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/advanced-usage/openapp/ws.md diff --git a/website/docs/advanced-usage/openapp/bot.md b/website/docs/advanced-usage/openapp/bot.md index c5a3b8f9..266f5a9a 100644 --- a/website/docs/advanced-usage/openapp/bot.md +++ b/website/docs/advanced-usage/openapp/bot.md @@ -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 = ''; const appId = ''; const appSecret = ''; -const client = new TailchatClient(host, appId, appSecret) +const client = new TailchatHTTPClient(host, appId, appSecret) // ... diff --git a/website/docs/advanced-usage/openapp/ws.md b/website/docs/advanced-usage/openapp/ws.md new file mode 100644 index 00000000..a8da5753 --- /dev/null +++ b/website/docs/advanced-usage/openapp/ws.md @@ -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); +}); +``` diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/advanced-usage/openapp/bot.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/advanced-usage/openapp/bot.md index 3d10e87b..0b7f9069 100644 --- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/advanced-usage/openapp/bot.md +++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/advanced-usage/openapp/bot.md @@ -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 = ''; const appId = ''; const appSecret = ''; -const client = new TailchatClient(host, appId, appSecret) +const client = new TailchatHTTPClient(host, appId, appSecret) // ... diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/advanced-usage/openapp/ws.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/advanced-usage/openapp/ws.md new file mode 100644 index 00000000..08fad485 --- /dev/null +++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/advanced-usage/openapp/ws.md @@ -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); +}); +```