-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1bfd731
commit f98b1c0
Showing
9 changed files
with
203 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { request } from '@shared/utils/request'; | ||
|
||
interface MsgTokenBot { | ||
token: string; | ||
name: string; | ||
group_uuid: string; | ||
channel_uuid: string | null; | ||
} | ||
|
||
/** | ||
* 创建简单机器人 | ||
*/ | ||
export async function createMsgTokenBot( | ||
name: string, | ||
groupUUID: string, | ||
channelUUID: string | null | ||
): Promise<MsgTokenBot> { | ||
const { data } = await request.post('/bot/msg/token/create', { | ||
name, | ||
groupUUID, | ||
channelUUID, | ||
}); | ||
|
||
return data.bot; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import { TMemo } from '@shared/components/TMemo'; | ||
import config from '@shared/project.config'; | ||
import { Typography } from 'antd'; | ||
|
||
interface BotResultTipProps { | ||
name: string; | ||
token: string; | ||
} | ||
|
||
/** | ||
* 创建机器人结果提示 | ||
*/ | ||
|
||
export const BotResultTip: React.FC<BotResultTipProps> = TMemo((props) => { | ||
const { name, token } = props; | ||
|
||
const requestUrl = `${config.url.api}/bot/msg/send`; | ||
const getUrl = `${requestUrl}?token=${token}&msg=${encodeURI('Hello World')}`; | ||
|
||
return ( | ||
<div> | ||
<span>创建机器人{name}完毕</span> | ||
<span>唯一标识: {token}</span> | ||
|
||
<div>尝试以下方式使用机器人</div> | ||
<div> | ||
<span>简单访问:</span> | ||
<Typography.Link href={getUrl} target="_blank"> | ||
{getUrl} | ||
</Typography.Link> | ||
<span>或发送POST请求到{requestUrl}</span> | ||
</div> | ||
</div> | ||
); | ||
}); | ||
BotResultTip.displayName = 'BotResultTip'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React, { useCallback, useMemo } from 'react'; | ||
import { TMemo } from '@shared/components/TMemo'; | ||
import { WebFastForm } from '../WebFastForm'; | ||
import { FastFormFieldMeta } from '@shared/components/FastForm/field'; | ||
import { useTranslation } from '@shared/i18n'; | ||
import { ModalWrapper, useModalContext } from '../Modal'; | ||
import { createMsgTokenBot } from '@shared/model/bot'; | ||
import { useGroupChannel } from '@redux/hooks/group'; | ||
import { showAlert, showToasts } from '@shared/manager/ui'; | ||
import { BotResultTip } from '../BotResultTip'; | ||
|
||
const baseFields: FastFormFieldMeta[] = [ | ||
{ type: 'text', name: 'name', label: '机器人名', maxLength: 16 }, | ||
]; | ||
|
||
interface BotCreateProps { | ||
groupUUID: string; | ||
} | ||
|
||
/** | ||
* 创建机器人 | ||
*/ | ||
export const BotCreate: React.FC<BotCreateProps> = TMemo((props) => { | ||
const { groupUUID } = props; | ||
const { t } = useTranslation(); | ||
const channels = useGroupChannel(groupUUID); | ||
const { closeModal } = useModalContext(); | ||
|
||
const fields = useMemo(() => { | ||
return [ | ||
...baseFields, | ||
{ | ||
type: 'select', | ||
name: 'channelUUID', | ||
label: '频道', | ||
options: [ | ||
{ | ||
label: '大厅', | ||
value: null, | ||
}, | ||
...channels.map((channel) => ({ | ||
label: channel.name, | ||
value: channel.uuid, | ||
})), | ||
], | ||
}, | ||
]; | ||
}, [channels]); | ||
|
||
const handleCreateBot = useCallback( | ||
async (values) => { | ||
try { | ||
const bot = await createMsgTokenBot( | ||
values.name, | ||
groupUUID, | ||
values.channelUUID | ||
); | ||
|
||
closeModal(); | ||
showAlert({ | ||
message: <BotResultTip name={bot.name} token={bot.token} />, | ||
}); | ||
} catch (err) { | ||
showToasts(err, 'error'); | ||
} | ||
}, | ||
[groupUUID, closeModal] | ||
); | ||
|
||
return ( | ||
<ModalWrapper title={t('创建机器人')}> | ||
<WebFastForm fields={fields} onSubmit={handleCreateBot} /> | ||
</ModalWrapper> | ||
); | ||
}); | ||
BotCreate.displayName = 'BotCreate'; |
34 changes: 34 additions & 0 deletions
34
src/web/routes/Main/Content/Group/GroupInfoDetail/GroupBotManage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, { useCallback } from 'react'; | ||
import { TMemo } from '@shared/components/TMemo'; | ||
import { Button, Typography } from 'antd'; | ||
import { useTranslation } from '@shared/i18n'; | ||
import { ModalWrapper, openModal } from '@web/components/Modal'; | ||
import { BotCreate } from '@web/components/modal/BotCreate'; | ||
|
||
interface GroupBotManageProps { | ||
groupUUID: string; | ||
} | ||
export const GroupBotManage: React.FC<GroupBotManageProps> = TMemo( | ||
(props: GroupBotManageProps) => { | ||
const { groupUUID } = props; | ||
const { t } = useTranslation(); | ||
|
||
const handleCreateBot = useCallback(() => { | ||
openModal(<BotCreate groupUUID={groupUUID} />); | ||
}, [groupUUID]); | ||
|
||
return ( | ||
<div> | ||
<Typography.Title level={3}>{t('机器人')}</Typography.Title> | ||
|
||
<Button type="primary" onClick={handleCreateBot}> | ||
{t('创建机器人')} | ||
</Button> | ||
|
||
{/* TODO */} | ||
{/* <div>机器人列表</div> */} | ||
</div> | ||
); | ||
} | ||
); | ||
GroupBotManage.displayName = 'GroupBotManage'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters