import React, { useEffect, useState } from 'react';
import { Button, Form, Header, Message, Segment } from 'semantic-ui-react';
import { useParams } from 'react-router-dom';
import { API, showError, showSuccess } from '../../helpers';
import { CHANNEL_OPTIONS } from '../../constants';
import axios from 'axios';
const EditChannel = () => {
const params = useParams();
const channelId = params.id;
const isEditing = channelId !== undefined;
const [loading, setLoading] = useState(isEditing);
const originInputs = {
type: 'none',
name: '',
description: '',
secret: '',
app_id: '',
account_id: '',
url: '',
other: '',
corp_id: '', // only for corp_app
agent_id: '', // only for corp_app
};
const [inputs, setInputs] = useState(originInputs);
const { type, name, description, secret, app_id, account_id, url, other } =
inputs;
const handleInputChange = (e, { name, value }) => {
setInputs((inputs) => ({ ...inputs, [name]: value }));
};
const loadChannel = async () => {
let res = await API.get(`/api/channel/${channelId}`);
const { success, message, data } = res.data;
if (success) {
if (data.type === 'corp_app') {
const [corp_id, agent_id] = data.app_id.split('|');
data.corp_id = corp_id;
data.agent_id = agent_id;
}
setInputs(data);
} else {
showError(message);
}
setLoading(false);
};
useEffect(() => {
if (isEditing) {
loadChannel().then();
}
}, []);
const submit = async () => {
if (!name) return;
let res = undefined;
let localInputs = { ...inputs };
switch (inputs.type) {
case 'corp_app':
localInputs.app_id = `${inputs.corp_id}|${inputs.agent_id}`;
break;
case 'bark':
if (localInputs.url === '') {
localInputs.url = 'https://api.day.app';
}
break;
case 'one_bot':
if (localInputs.url.endsWith('/')) {
localInputs.url = localInputs.url.slice(0, -1);
}
break;
case 'group':
let channels = localInputs.app_id.split('|');
let targets = localInputs.account_id.split('|');
if (localInputs.account_id === '') {
for (let i = 0; i < channels.length - 1; i++) {
localInputs.account_id += '|';
}
} else if (channels.length !== targets.length) {
showError(
'群组通道的子通道数量与目标数量不匹配,对于不需要指定的目标请直接留空'
);
return;
}
break;
case 'custom':
if (!localInputs.url.startsWith('https://')) {
showError('自定义通道的 URL 必须以 https:// 开头!');
return;
}
try {
JSON.parse(localInputs.other);
}catch (e) {
showError('JSON 格式错误:' + e.message);
return;
}
break;
}
if (isEditing) {
res = await API.put(`/api/channel/`, {
...localInputs,
id: parseInt(channelId),
});
} else {
res = await API.post(`/api/channel`, localInputs);
}
const { success, message } = res.data;
if (success) {
if (isEditing) {
showSuccess('通道信息更新成功!');
} else {
showSuccess('通道创建成功!');
setInputs(originInputs);
}
} else {
showError(message);
}
};
const getTelegramChatId = async () => {
if (inputs.telegram_bot_token === '') {
showError('请先输入 Telegram 机器人令牌!');
return;
}
let res = await axios.get(
`https://api.telegram.org/bot${inputs.secret}/getUpdates`
);
const { ok } = res.data;
if (ok) {
let result = res.data.result;
if (result.length === 0) {
showError(`请先向你的机器人发送一条任意消息!`);
} else {
let id = result[0]?.message?.chat?.id;
id = id.toString();
setInputs((inputs) => ({ ...inputs, account_id: id }));
showSuccess('会话 ID 获取成功!');
}
} else {
showError(`发生错误:${res.description}`);
}
};
const renderChannelForm = () => {
switch (type) {
case 'email':
return (
<>
需要新增测试模板,模板标题推荐填写为「消息推送」,模板内容填写为:
标题:{' {{'}title.DATA{'}}'}
描述:{' {{'}description.DATA{'}}'}
内容:{' {{'}content.DATA{'}}'}
https://api.day.app/wrsVSDRANDOM/Body Text
,其中 wrsVSDRANDOM
就是你的推送 key。
123456789||@wechat
,两个连续的分隔符表示跳过该渠道。
类型:ID
,详见飞书
开发文档
中查询参数一节。
$title
,$description
,$content
,$url
,$to
。