Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 机器人管理 #238

Open
wants to merge 1 commit into
base: pro
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions app/Http/Controllers/Api/SystemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1518,4 +1518,47 @@ public function prefetch()
return url($url);
}, array_values(array_filter($array)));
}

/**
* @api {get} api/system/bot/apidoc 27. 机器人API文档
*
* @apiVersion 1.0.0
* @apiGroup system
* @apiName bot__apidoc
*
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function bot__apidoc()
{
$version = Base::getVersion();
$content = <<<EOF
## Webhook说明:
### 机器人收到消息后会将消息POST推送到Webhook地址,请求超时为10秒,请求参数如下:
- text: 消息文本
- token: 机器人Token
- dialog_id: 对话ID
- dialog_type: 对话类型
- msg_id: 消息ID
- msg_uid: 消息发送人ID
- mention: 是否被@到
- bot_uid: 机器人ID
- version: 系统版本

### 发送文本消息:
```bash
curl --request POST 'http(s)://your_domain/api/dialog/msg/sendtext' \
--header 'version: {$version}' \
--header 'token: 机器人Token' \
--form 'dialog_id="对话ID"' \
--form 'text="消息内容"'
--form 'text_type="[html|md]"'
--form 'key="搜索词 (留空自动生成)"'
--form 'silence="[yes|no]"'
--form 'reply_id="回复指定消息ID"'
```
EOF;
return Base::retSuccess('success', ['content' => $content]);
}
}
202 changes: 198 additions & 4 deletions app/Http/Controllers/Api/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1881,7 +1881,50 @@ public function key__client()
}

/**
* @api {get} api/users/bot/info 31. 机器人信息
* @api {get} api/users/bot/list 31. 机器人列表
*
* @apiDescription 需要token身份,获取我的机器人列表
* @apiVersion 1.0.0
* @apiGroup users
* @apiName bot__list
*
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function bot__list()
{
$user = User::auth();
// 查询自己创建的机器人
$search = trim(Request::input('search'));
//
$query = User::select([
'users.userid',
'users.nickname as name',
'user_bots.bot_id',
'user_bots.clear_day',
'user_bots.clear_at',
'user_bots.webhook_url',
'user_bots.webhook_num'
])
->join('user_bots', 'users.userid', '=', 'user_bots.bot_id')
->where('users.bot', 1)
->where('user_bots.userid', $user->userid);
if ($search) {
$query->where('users.nickname', 'like', '%' . $search . '%');
}
$list = $query->orderByDesc('id')->paginate(Base::getPaginate(50, 20));
//
foreach ($list->items() as $item) {
$data = UserBot::botManagerOne($item->bot_id, $user->userid);
$item->token = User::generateToken($data);
}

return Base::retSuccess('success', $list);
}

/**
* @api {get} api/users/bot/info 32. 机器人信息
*
* @apiDescription 需要token身份,获取我的机器人信息
* @apiVersion 1.0.0
Expand Down Expand Up @@ -1927,12 +1970,53 @@ public function bot__info()
if ($userBot) {
$data['clear_day'] = $userBot->clear_day;
$data['webhook_url'] = $userBot->webhook_url;
$user = UserBot::botManagerOne($botId, $user->userid);
$data['token'] = User::generateToken($user);
}
return Base::retSuccess('success', $data);
}

/**
* @api {post} api/users/bot/edit 32. 编辑机器人
* @api {post} api/users/bot/add 33. 创建机器人
*
* @apiDescription 需要token身份,创建机器人
* @apiVersion 1.0.0
* @apiGroup users
* @apiName bot__add
*
* @apiParam {String} name 机器人名称
*
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function bot__add()
{
$user = User::auth();
//
$data = Request::input();
if (User::select(['users.*'])
->join('user_bots', 'users.userid', '=', 'user_bots.bot_id')
->where('users.bot', 1)
->where('user_bots.userid', $user->userid)
->count() >= 50) {
return Base::retError('超过最大创建数量。');
}
if (strlen($data['name']) < 2 || strlen($data['name']) > 20) {
return Base::retError('机器人名称由2-20个字符组成。');
}
$data = User::botGetOrCreate("user-" . Base::generatePassword(), [
'nickname' => $data['name']
], $user->userid);
if (empty($data)) {
return Base::retError('创建失败。');
}

return Base::retSuccess('创建成功', $data);
}

/**
* @api {post} api/users/bot/edit 34. 编辑机器人
*
* @apiDescription 需要token身份,编辑 我的机器人 或 管理员修改系统机器人 信息
* @apiVersion 1.0.0
Expand Down Expand Up @@ -2018,7 +2102,117 @@ public function bot__edit()
}

/**
* @api {get} api/users/share/list 33. 获取分享列表
* @api {post} api/users/bot/delete 35. 删除机器人
*
* @apiDescription 需要token身份,删除机器人
* @apiVersion 1.0.0
* @apiGroup users
* @apiName bot__delete
*
* @apiParam {Number} id 机器人ID
*
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
* @apiSuccess {String} msg 返回信息(错误描述)
*/
public function bot__delete()
{
$user = User::auth();
//
$botId = intval(Request::input('id'));
$botUser = User::whereUserid($botId)->whereBot(1)->first();
$userBot = UserBot::whereBotId($botUser->userid)->whereUserid($user->userid)->first();
if (empty($userBot)) {
if (UserBot::systemBotName($botUser->email)) {
// 系统机器人(仅限管理员)
if (!$user->isAdmin()) {
return Base::retError('权限不足');
}
} else {
// 其他用户的机器人(仅限主人)
return Base::retError('不是你的机器人');
}
}
//
UserBot::whereBotId($botUser->userid)->whereUserid($user->userid)->delete();
$botUser->deleteUser('delete bot');
return Base::retSuccess('删除成功');
}

/**
* @api {post} api/users/bot/revoke 36. 撤销Token
*
* @apiDescription 需要token身份,撤销机器人Token
* @apiVersion 1.0.0
* @apiGroup users
* @apiName bot__revoke
*
* @apiParam {Number} id 机器人ID
*
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
* @apiSuccess {String} msg 返回信息(错误描述)
*/
public function bot__revoke()
{
$user = User::auth();
//
$botId = intval(Request::input('id'));
$botUser = User::whereUserid($botId)->whereBot(1)->first();
$userBot = UserBot::whereBotId($botUser->userid)->whereUserid($user->userid)->first();
if (empty($userBot)) {
return Base::retError('不是你的机器人');
}
$botUser->encrypt = Base::generatePassword(6);
$botUser->password = Doo::md5s(Base::generatePassword(32), $botUser->encrypt);
$botUser->save();

$token = User::generateToken($botUser);
return Base::retSuccess('重置成功', [
'id' => $botId,
'token' => $token
]);
}

/**
* @api {post} api/users/bot/session 37. 获取会话列表
*
* @apiDescription 需要token身份,获取会话列表
* @apiVersion 1.0.0
* @apiGroup users
* @apiName bot__session
*
* @apiParam {Number} id 机器人ID
* @apiParam {String} [name] 会话名称关键字
*
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function bot__session()
{
$user = User::auth();
//
$botId = intval(Request::input('id'));
$nameKey = trim(Request::input('name'));
$bot = UserBot::botManagerOne($botId, $user->userid);
$list = DB::table('web_socket_dialog_users as u')
->select(['d.*', 'u.top_at', 'u.last_at', 'u.mark_unread', 'u.silence', 'u.hide', 'u.color', 'u.updated_at as user_at'])
->join('web_socket_dialogs as d', 'u.dialog_id', '=', 'd.id')
->where('u.userid', $bot->userid)
->where('d.name', 'LIKE', "%{$nameKey}%")
->whereNull('d.deleted_at')
->orderByDesc('u.top_at')
->orderByDesc('u.last_at')
->take(20)
->get()
->map(function($item) use ($bot) {
return WebSocketDialog::synthesizeData($item, $bot->userid);
})
->all();
return Base::retSuccess('success', $list);
}

/**
* @api {get} api/users/share/list 38. 获取分享列表
*
* @apiVersion 1.0.0
* @apiGroup users
Expand Down Expand Up @@ -2103,7 +2297,7 @@ public function share__list()
}

/**
* @api {get} api/users/annual/report 34. 年度报告
* @api {get} api/users/annual/report 39. 年度报告
*
* @apiVersion 1.0.0
* @apiGroup users
Expand Down
35 changes: 35 additions & 0 deletions app/Models/UserBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@
class UserBot extends AbstractModel
{

/**
* 关联用户表
* @return BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class, 'bot_id', 'userid');
}

/**
* 判断是否系统机器人
* @param $email
Expand Down Expand Up @@ -415,4 +424,30 @@ public static function anonBotQuickMsg($command)
default => [],
};
}

/**
* @param $botId
* @param $userid
* @return User
*/
public static function botManagerOne($botId, $userid)
{
$botId = intval($botId);
$userid = intval($userid);
if ($botId > 0) {
return User::select([
'users.*',
'user_bots.clear_day',
'user_bots.clear_at',
'user_bots.webhook_url',
'user_bots.webhook_num'
])
->join('user_bots', 'users.userid', '=', 'user_bots.bot_id')
->where('users.bot', 1)
->where('user_bots.bot_id', $botId)
->where('user_bots.userid', $userid)
->first();
}
return null;
}
}
14 changes: 14 additions & 0 deletions language/original-web.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1827,3 +1827,17 @@ WiFi签到延迟时长为±1分钟。

自动归档
是否保存编辑内容?

机器人管理
请输入机器人名称
编辑机器人
重置机器人Token令牌
机器人会话
清除天数
请输入清除天数
请输入Webhook地址
清理时间
请输入会话名称
会话ID
会话名称
API文档
46 changes: 23 additions & 23 deletions public/docs/assets/main.bundle.js

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions public/docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<meta name="description" content="APP接口文档">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="assets/bootstrap.min.css?v=1732093144762" rel="stylesheet" media="screen">
<link href="assets/prism.css?v=1732093144762" rel="stylesheet" />
<link href="assets/prism-toolbar.css?v=1732093144762" rel="stylesheet" />
<link href="assets/prism-diff-highlight.css?v=1732093144762" rel="stylesheet" />
<link href="assets/main.css?v=1732093144762" rel="stylesheet" media="screen, print">
<link href="assets/favicon.ico?v=1732093144762" rel="icon" type="image/x-icon">
<link href="assets/apple-touch-icon.png?v=1732093144762" rel="apple-touch-icon" sizes="180x180">
<link href="assets/favicon-32x32.png?v=1732093144762" rel="icon" type="image/png" sizes="32x32">
<link href="assets/favicon-16x16.png?v=1732093144762" rel="icon" type="image/png" sizes="16x16">
<link href="assets/bootstrap.min.css?v=1733637071617" rel="stylesheet" media="screen">
<link href="assets/prism.css?v=1733637071617" rel="stylesheet" />
<link href="assets/prism-toolbar.css?v=1733637071617" rel="stylesheet" />
<link href="assets/prism-diff-highlight.css?v=1733637071617" rel="stylesheet" />
<link href="assets/main.css?v=1733637071617" rel="stylesheet" media="screen, print">
<link href="assets/favicon.ico?v=1733637071617" rel="icon" type="image/x-icon">
<link href="assets/apple-touch-icon.png?v=1733637071617" rel="apple-touch-icon" sizes="180x180">
<link href="assets/favicon-32x32.png?v=1733637071617" rel="icon" type="image/png" sizes="32x32">
<link href="assets/favicon-16x16.png?v=1733637071617" rel="icon" type="image/png" sizes="16x16">
</head>

<body class="container-fluid">
Expand Down Expand Up @@ -1042,6 +1042,6 @@ <h2>{{__ "Request Body"}}</h2>
</div>
</div>

<script src="assets/main.bundle.js?v=1732093144762"></script>
<script src="assets/main.bundle.js?v=1733637071617"></script>
</body>
</html>
1 change: 1 addition & 0 deletions public/images/application/robot-manage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading