mirror of https://github.com/msgbyte/tailchat
docs: add document about open app
parent
b419f44d19
commit
1d54eee362
@ -0,0 +1,4 @@
|
||||
{
|
||||
"label": "Open App",
|
||||
"position": 20
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
---
|
||||
sidebar_position: 1
|
||||
title: About Open App
|
||||
---
|
||||
|
||||
Open platform is a common and traditional way of interacting between applications. For some simple requirements, we can implement data transfer between applications through an open platform.
|
||||
|
||||
In Tailchat. At present, it mainly provides two forms of open platform application capabilities: `OAuth` and `Bot`
|
||||
|
||||
## Features
|
||||
|
||||
### OAuth
|
||||
|
||||
`OAuth` enables external applications to log in through `Tailchat` accounts, just like `Google`, `Github` login methods, which can facilitate users to create a unified user platform based on `Tailchat`
|
||||
|
||||
:::info
|
||||
The difference from the `com.msgbyte.iam` plugin: `iam` plugin provides a way to log in to `Tailchat` with an external account, such as using a `Github` account to log in to `Tailchat`, while the OAuth capability of the open platform is based on `Tailchat` account to log in to other platforms.
|
||||
:::
|
||||
|
||||
### Bot
|
||||
|
||||
`Bot` endows chatbots with interactive application capabilities, which means that Tailchat can not only passively receive external messages, but also actively forward internal chat requests to external applications for processing.
|
||||
|
||||
[Learn more](./bot)
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Before using the relevant capabilities of the open platform, please ensure that the corresponding plug-in is installed, and ensure that the administrator has deployed the relevant capabilities of the open platform.
|
||||
|
||||
As a user, you need to install the `com.msgbyte.integration` plugin to add the application to your group
|
||||
|
||||
As a developer of open platform applications, you need to additionally install `com.msgbyte.openapi` to display the interfaces required by open platform applications
|
@ -0,0 +1,229 @@
|
||||
---
|
||||
sidebar_position: 3
|
||||
title: Bot
|
||||
---
|
||||
|
||||
Open platform bot are interactive robot solutions
|
||||
|
||||
Main process below:
|
||||
|
||||
- Create openapp
|
||||
- Get appid and appsecret
|
||||
- Enter the bot page and enable the bot ability
|
||||
- Fill in the callback address with the public http service address that can be accessed
|
||||
- Go back to the group page, enter the appid in the group details integration function, find the app you just created, and add it to the group
|
||||
- In the group @bot and enter text, tailchat will send an http request with message content to the address shown in the callback address
|
||||
- Receive the request sent by tailchat in the bot service, and send a response to the corresponding panel of the corresponding group through the bot
|
||||
|
||||
Of course, [create in Tailchat before you start anything](./create)
|
||||
|
||||
Let's take a look at the actual development process of the bot:
|
||||
|
||||
## Development with SDK (Node.js)
|
||||
|
||||
Tailchat provides sdk as a rapid development tool, `tailchat-client-sdk`, you can install it with the following command
|
||||
|
||||
```bash
|
||||
npm install tailchat-client-sdk
|
||||
```
|
||||
|
||||
Taking `koa` as an example, we first create a simple `koa` service as follows:
|
||||
|
||||
Create a node project:
|
||||
|
||||
```bash
|
||||
mkdir tailchat-bot && cd tailchat-bot
|
||||
npm init -y
|
||||
npm install koa koa-router tailchat-client-sdk
|
||||
```
|
||||
|
||||
Create `server.js` file:
|
||||
|
||||
```js
|
||||
const Koa = require('koa');
|
||||
const Router = require('koa-router');
|
||||
const app = new Koa();
|
||||
const router = new Router();
|
||||
|
||||
// Define route
|
||||
router.get('/', async (ctx) => {
|
||||
ctx.body = 'Hello, World!';
|
||||
});
|
||||
|
||||
router.post('/bot/callback', async (ctx) => {
|
||||
ctx.body = 'Bot Callback Page';
|
||||
});
|
||||
|
||||
// Register middleware
|
||||
app.use(router.routes());
|
||||
app.use(router.allowedMethods());
|
||||
|
||||
// Start server
|
||||
app.listen(3000, () => {
|
||||
console.log('Server is running on http://localhost:3000');
|
||||
});
|
||||
```
|
||||
|
||||
At this point we have established two basic routes, `/` and `/bot/callback`, and listened to `3000` port, note that `/bot/callback` listens to **POST** requests
|
||||
|
||||
At this point we execute `node server.js` to see that our application will be started.
|
||||
|
||||
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';
|
||||
|
||||
const host = '<your tailchat instance backend host>';
|
||||
const appId = '<appId>';
|
||||
const appSecret = '<appSecret>';
|
||||
|
||||
const client = new TailchatClient(host, appId, appSecret)
|
||||
|
||||
// ...
|
||||
|
||||
router.post('/bot/callback', async (ctx) => {
|
||||
const type = ctx.body.type;
|
||||
|
||||
if (type === 'message') {
|
||||
const payload = ctx.body.payload;
|
||||
try {
|
||||
const message = await client.replyMessage({
|
||||
messageId: payload.messageId,
|
||||
author: payload.messageAuthor,
|
||||
content: payload.messageSnippet
|
||||
}, {
|
||||
groupId: payload.groupId,
|
||||
converseId: payload.converseId,
|
||||
content: `Your message: ${stripMentionTag(payload.messageSnippet)}`,
|
||||
})
|
||||
|
||||
console.log('send message success:', message)
|
||||
} catch (err) {
|
||||
console.log('send message failed:', err)
|
||||
}
|
||||
}
|
||||
|
||||
ctx.body = 'Bot Callback Page';
|
||||
});
|
||||
```
|
||||
|
||||
Please fill in `host`, `appId` and `appSecret` into the `appId` and `appSecret` obtained during creation, and `host` into the address of the `Tailchat` server, the official address of `nightly` is `https //tailchat-nightly.moonrailgun.com`
|
||||
|
||||
The content of the reply is not important, just make sure not to actively return an error message, Tailchat does not care about the returned content
|
||||
|
||||
**Please note that if you want to share your code, please keep your `appSecret`, which is equivalent to your account password**
|
||||
|
||||
The logic is very simple. Get the message content, author, id, group id, and converse id from the request. Send content as a reply
|
||||
|
||||
Deploy the application online to see the effect.
|
||||
|
||||
:::info
|
||||
Before testing, please make sure you have enabled the bot ability and filled in the correct callback address
|
||||
:::
|
||||
|
||||
## Develop in other languages
|
||||
|
||||
Since it is a network application, it is of course not limited to `nodejs`. The following are the formats of some network requests that need to be used, mainly sending requests to the Tailchat server as an open platform bot.
|
||||
|
||||
The official `nightly` api address is `https://tailchat-nightly.moonrailgun.com`, please replace it with your own backend address for self-deployment
|
||||
|
||||
### Login
|
||||
|
||||
Before all requests, you need to log in to obtain the jwt token to indicate your identity, and you need to send the following content:
|
||||
|
||||
```
|
||||
POST /api/openapi/bot/login
|
||||
|
||||
Header
|
||||
Content-Type: application/json
|
||||
|
||||
Body
|
||||
{
|
||||
appId: <your app id>,
|
||||
token: <md5(appId+appSecret)>,
|
||||
}
|
||||
|
||||
Response
|
||||
{
|
||||
data: {
|
||||
jwt: ".........."
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The `token` of the request body is a fixed value, which needs to be encrypted with the `md5` algorithm after splicing `appId` and `appSecret`. Finally get `jwt`, `jwt` must be on the request header in all subsequent requests
|
||||
|
||||
```
|
||||
Header
|
||||
X-Token: <your-jwt>
|
||||
```
|
||||
|
||||
### Call
|
||||
|
||||
Robots can call the interface like ordinary users, such as:
|
||||
|
||||
```
|
||||
POST /api/xxx
|
||||
|
||||
Header
|
||||
Content-Type: application/json
|
||||
X-Token: <your-jwt>
|
||||
|
||||
Body
|
||||
{
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
You can treat the bot as an ordinary user. The bot can do everything that ordinary users can do, and the bot will also be subject to the permission restrictions that ordinary users need to be subject to.
|
||||
|
||||
The difference is that regular users interact with visualizations, while bots interact with APIs.
|
||||
|
||||
#### Send Message
|
||||
|
||||
```
|
||||
POST /api/chat/message/sendMessage
|
||||
|
||||
Header
|
||||
Content-Type: application/json
|
||||
X-Token: <your-jwt>
|
||||
|
||||
Body
|
||||
{
|
||||
"converseId": "",
|
||||
"groupId": "",
|
||||
"content": "",
|
||||
"plain": "",
|
||||
"meta": {},
|
||||
}
|
||||
```
|
||||
|
||||
#### Reply message
|
||||
|
||||
```
|
||||
POST /api/chat/message/sendMessage
|
||||
|
||||
Header
|
||||
Content-Type: application/json
|
||||
X-Token: <your-jwt>
|
||||
|
||||
Body
|
||||
{
|
||||
"converseId": "<converId/panelId>",
|
||||
"groupId": "<groupId, optional in DM>",
|
||||
"content": "<your message content>",
|
||||
"plain": "<your plained message, optional>",
|
||||
"meta": {
|
||||
mentions: ["<replyMessageAuthorId>"],
|
||||
reply: {
|
||||
_id: "<replyMessageId>",
|
||||
author: "<replyMessageAuthor>",
|
||||
content: "<replyMessageContent>",
|
||||
},
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
## Additional Documentation
|
||||
|
||||
- [Tailchat x Laf: Develop a chatbot in 10 minutes](/blog/tailchat-laf-bot)
|
@ -0,0 +1,14 @@
|
||||
---
|
||||
sidebar_position: 2
|
||||
title: Create Open Application
|
||||
---
|
||||
|
||||
After installing the `com.msgbyte.openapi` plug-in, you can see an additional open API function on the settings page in the lower left corner
|
||||
|
||||

|
||||
|
||||
Now, fill in the application name and application description
|
||||
|
||||

|
||||
|
||||
After success, you can see your open platform application in your application list
|
@ -1,4 +0,0 @@
|
||||
{
|
||||
"label": "进阶使用",
|
||||
"position": 30
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 83 KiB |
Binary file not shown.
Before Width: | Height: | Size: 116 KiB |
@ -0,0 +1,32 @@
|
||||
---
|
||||
sidebar_position: 1
|
||||
title: 关于开放平台
|
||||
---
|
||||
|
||||
开放平台是常见且传统的应用与应用之间进行交互的方式,对于一些简单的需求我们可以通过开放平台来实现应用之间的数据传递。
|
||||
|
||||
在 Tailchat 中。目前主要提供两种形式的开放平台应用能力: `OAuth` 与 `Bot`
|
||||
|
||||
## 功能介绍
|
||||
|
||||
### OAuth
|
||||
|
||||
`OAuth` 能够使外部应用能够通过`Tailchat`的账号登录,就像是 `Google`, `Github` 登录方式一样,可以方便用户打造基于 `Tailchat` 的统一用户平台
|
||||
|
||||
:::info
|
||||
与 `com.msgbyte.iam` 插件的区别: `iam`插件是提供外部账号登录`Tailchat`的方式,如使用`Github`账号登录`Tailchat`, 而开放平台的OAuth能力则是以`Tailchat`账号登录其他平台。
|
||||
:::
|
||||
|
||||
### Bot
|
||||
|
||||
`Bot` 赋予聊天机器人可交互的应用能力,这意味着 Tailchat 不仅仅可以被动接收来自外部的消息,也可以主动将内部聊天的请求转发到外部应用代为处理。
|
||||
|
||||
[了解更多](./bot)
|
||||
|
||||
## 使用前提
|
||||
|
||||
在使用开放平台的相关能力前,请确保安装了对应的插件,并确保管理员已经部署与开放了相关的开放平台的能力。
|
||||
|
||||
作为用户,你需要安装 `com.msgbyte.integration` 插件来将应用加入到自己的群组
|
||||
|
||||
作为开放平台应用的开发者,你需要额外安装 `com.msgbyte.openapi` 来展示开放平台应用相关所需要的界面
|
@ -0,0 +1,14 @@
|
||||
---
|
||||
sidebar_position: 2
|
||||
title: 创建开放平台应用
|
||||
---
|
||||
|
||||
安装 `com.msgbyte.openapi` 插件后可以在左下角设置页面看到多出来一个开放API的功能
|
||||
|
||||

|
||||
|
||||
填入应用名称与应用描述即可
|
||||
|
||||

|
||||
|
||||
成功后你可以在你的应用列表中看到你的开放平台应用
|
Binary file not shown.
After Width: | Height: | Size: 26 KiB |
Binary file not shown.
After Width: | Height: | Size: 31 KiB |
Loading…
Reference in New Issue