Skip to content

Commit

Permalink
send msg support
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzm0809 committed Dec 19, 2023
1 parent 59f83a1 commit 4aed73e
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.dinky.alert.http;

import java.util.Map;
import org.dinky.alert.AbstractAlert;
import org.dinky.alert.AlertResult;

Expand All @@ -45,12 +46,7 @@ public String getType() {
@Override
public AlertResult send(String title, String content) {
HttpSender sender = new HttpSender(getConfig().getParam());
try {
String built = buildContent(sender.buildTemplateParams(title, content));
return sender.send(built);
} catch (TemplateException | IOException e) {
log.error("{}'message send error, Reason:{}", getType(), e.getMessage());
throw new RuntimeException(e);
}
Map<String, Object> templateParams = sender.buildTemplateParams(title, content);
return sender.send(templateParams);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class HttpConstants {
public static final String TYPE = "Http";
public static final String ALERT_TEMPLATE_TITLE = "title";
public static final String ALERT_TEMPLATE_MSG = "msg";
public static final String ALERT_TEMPLATE_CONTENT = "content";

public static final String REQUEST_TYPE_POST = "POST";
public static final String REQUEST_TYPE_GET = "GET";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class HttpSender {

HttpSender(Map<String, Object> config) {
this.httpParams = JSONUtil.toBean(JSONUtil.toJsonStr(config), HttpParams.class);
Asserts.checkNotNull(httpParams, "dingTalkParams is null");
Asserts.checkNotNull(httpParams, "httpParams is null");
}

/**
Expand All @@ -75,7 +75,7 @@ public class HttpSender {
public Map<String, Object> buildTemplateParams(String title, String content) {
Map<String, Object> params = new HashMap<>();
params.put(HttpConstants.ALERT_TEMPLATE_TITLE, title);
params.put(HttpConstants.ALERT_TEMPLATE_MSG, content);
params.put(HttpConstants.ALERT_TEMPLATE_CONTENT, content);
return params;
}

Expand All @@ -85,11 +85,11 @@ public Map<String, Object> buildTemplateParams(String title, String content) {
* @param content: send msg content
* @return AlertResult
*/
public AlertResult send(String content) {
public AlertResult send(Map<String, Object> templateParams ) {
AlertResult alertResult = new AlertResult();

try {
createHttpRequest(content);
createHttpRequest(templateParams);
} catch (MalformedURLException | URISyntaxException e) {
throw new RuntimeException(e);
}
Expand All @@ -113,13 +113,13 @@ public AlertResult send(String content) {
return alertResult;
}

private void createHttpRequest(String msg) throws MalformedURLException, URISyntaxException {
private void createHttpRequest(Map<String, Object> templateParams ) throws MalformedURLException, URISyntaxException {
if (HttpConstants.REQUEST_TYPE_POST.equals(httpParams.getMethod())) {
httpRequest = new HttpPost(httpParams.getUrl());
buildRequestHeader();
buildMsgToRequestBody(msg);
buildMsgToRequestBody( templateParams );
} else if (HttpConstants.REQUEST_TYPE_GET.equals(httpParams.getMethod())) {
buildMsgToUrl(msg);
buildMsgToUrl(templateParams);
URL unencodeUrl = new URL(httpParams.getUrl());
URI uri = new URI(
unencodeUrl.getProtocol(),
Expand All @@ -143,14 +143,18 @@ public String getResponseString(HttpRequestBase httpRequest) throws IOException
/**
* add msg param in url
*/
private void buildMsgToUrl(String msg) {
private void buildMsgToUrl(Map<String, Object> templateParams) {

String line = "&";
String line;
// check splice char is & or ?
if (!httpParams.getUrl().contains("?")) {
line = "?";
} else {
line = "&";
}
httpParams.setUrl(String.format("%s%smsg=%s", httpParams.getUrl(), line, msg));
templateParams.forEach((k, v) -> {
httpParams.setUrl(String.format("%s%s%s=%s", httpParams.getUrl(), line, k, v));
});
}

private void buildRequestHeader() {
Expand All @@ -165,9 +169,15 @@ private void buildRequestHeader() {
/**
* set body params
*/
private void buildMsgToRequestBody(String msg) {
private void buildMsgToRequestBody(Map<String, Object> templateParams ) {
try {
JSONObject jsonObject = JSONUtil.createObj().set(HttpConstants.ALERT_TEMPLATE_MSG, msg);
JSONObject jsonObject = JSONUtil.createObj();
templateParams.forEach(jsonObject::set);
if (CollUtil.isNotEmpty(httpParams.getBody())) {
httpParams.getBody().forEach(configItem -> {
jsonObject.set(configItem.getKey(), configItem.getValue());
});
}
StringEntity entity = new StringEntity(JSONUtil.toJsonStr(jsonObject), HttpConstants.DEFAULT_CHARSET);
((HttpPost) httpRequest).setEntity(entity);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ public class HttpParams {
private String url = "";
private String method = "";
private List<ConfigItem> headers = new ArrayList<>();
private List<ConfigItem> body = new ArrayList<>();
}
2 changes: 1 addition & 1 deletion dinky-alert/dinky-alert-http/src/main/resources/Http.ftl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
${title}
${msg}
${content}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,27 @@ public class HttpSenderTest {
private static Map<String, Object> httpConfig = new HashMap<>();

@Before
public void initFeiShuConfig() {
public void initConfig() {
HttpParams httpParams = new HttpParams();
httpParams.setUrl("http://127.0.0.1:8080/alert");
httpParams.setUrl("https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxx");
httpParams.setMethod(HttpConstants.REQUEST_TYPE_POST);
ConfigItem configItem = new ConfigItem("Content-Type", "application/json");
httpParams.setHeaders(Arrays.asList(configItem));

ConfigItem msgType = new ConfigItem("msgtype", "markdown");
ConfigItem title = new ConfigItem("title", AlertBaseConstant.ALERT_TEMPLATE_TITLE);
ConfigItem content = new ConfigItem("content", AlertBaseConstant.ALERT_TEMPLATE_MSG);
ConfigItem markdown = new ConfigItem("markdown", JSONUtil.toJsonStr(new HashMap<String, Object>() {
{
put("title", AlertBaseConstant.ALERT_TEMPLATE_TITLE);
put("text", AlertBaseConstant.ALERT_TEMPLATE_MSG);
}
}));

Arrays.asList(msgType, content, title, markdown).forEach(item -> {
httpParams.getBody().add(item);
});

httpConfig = JsonUtils.toMap(JSONUtil.toJsonStr(httpParams), String.class, Object.class);
}

Expand Down
1 change: 1 addition & 0 deletions dinky-web/src/locales/en-US/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ export default {
'rc.ai.http.method': 'Request method',
'rc.ai.http.methodPleaseHolder': 'Please select the request method',
'rc.ai.http.headers': 'Request headers',
'rc.ai.http.body': 'Request body',
'rc.alert.template.create': 'Create Template',
'rc.alert.template.modify': 'Modify Template',
'rc.alert.template.new': 'Create an alert template',
Expand Down
1 change: 1 addition & 0 deletions dinky-web/src/locales/zh-CN/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ export default {
'rc.ai.http.method': '请求方法',
'rc.ai.http.methodPleaseHolder': '请选择请求方法',
'rc.ai.http.headers': '请求头',
'rc.ai.http.body': '请求体',
'rc.alert.template.create': '创建模板',
'rc.alert.template.modify': '修改模板',
'rc.alert.template.new': '新建告警模板',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ const Http: React.FC<HttpProps> = (props) => {
</Space>
</ProFormGroup>
</ProFormList>

{/*这有 bug,待解决*/}
<ProFormList
name={['params', 'body']}
label={l('rc.ai.http.body')}
copyIconProps={false}
required
initialValue={[{ key: '', value: '' }]}
deleteIconProps={{
tooltipText: l('rc.cc.deleteConfig')
}}
creatorButtonProps={{
style: { width: '100%' },
creatorButtonText: l('rc.cc.addConfig')
}}
>
<ProFormGroup key='bodyGroup' style={{ width: '100%' }}>
<Space key={'config'} style={{ width: '100%' }} align='baseline'>
<ProFormText width={'md'} name='key' placeholder={l('rc.cc.key')} />
<ProFormText width={'lg'} name='value' placeholder={l('rc.cc.value')} />
</Space>
</ProFormGroup>
</ProFormList>
</>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import Http from '@/pages/RegCenter/Alert/AlertInstance/components/AlertTypeChoo
import Sms from '@/pages/RegCenter/Alert/AlertInstance/components/AlertTypeChoose/InstanceForm/Sms';
import WeChat from '@/pages/RegCenter/Alert/AlertInstance/components/AlertTypeChoose/InstanceForm/WeChat';
import { ALERT_TYPE_LIST_OPTIONS } from '@/pages/RegCenter/Alert/AlertInstance/constans';
import { Alert, ALERT_TYPE } from '@/types/RegCenter/data.d';
import { Alert,ALERT_TYPE } from '@/types/RegCenter/data.d';
import { l } from '@/utils/intl';
import { ProForm, ProFormSelect, ProFormText } from '@ant-design/pro-components';
import { ProForm,ProFormSelect,ProFormText } from '@ant-design/pro-components';
import { FormInstance } from 'antd/es/form/hooks/useForm';
import { Values } from 'async-validator';
import React, { useState } from 'react';
import React,{ useState } from 'react';
import DingTalk from './DingTalk';

type InstanceFormProps = {
Expand Down Expand Up @@ -87,10 +87,12 @@ const InstanceForm: React.FC<InstanceFormProps> = (props) => {

return (
<>
<ProForm.Group>
{renderPreForm()}
{renderFormByType(values, alertType)}
</ProForm.Group>
<ProForm form={form} submitter={false}>
<ProForm.Group>
{renderPreForm()}
{renderFormByType(values, alertType)}
</ProForm.Group>
</ProForm>
</>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import InstanceForm from '@/pages/RegCenter/Alert/AlertInstance/components/Alert
import { NORMAL_MODAL_OPTIONS } from '@/services/constants';
import { Alert } from '@/types/RegCenter/data.d';
import { l } from '@/utils/intl';
import { ModalForm } from '@ant-design/pro-components';
import {ModalForm, ProForm} from '@ant-design/pro-components';

/**
* update form props
Expand Down Expand Up @@ -131,7 +131,7 @@ const AlertTypeChoose: React.FC<UpdateFormProps> = (props) => {
submitter={{ render: () => [...renderFooter()] }}
syncToInitialValues
>
<InstanceForm form={form} values={formValues} />
<InstanceForm form={form} values={formValues} />
</ModalForm>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export const ALERT_TYPE_LIST_OPTIONS: DefaultOptionType[] = [
];

export const RequestMethod = [
{ label: 'POST', value: 'POST' },
{ label: 'GET', value: 'GET' }
{ label: 'POST', value: 'POST', key: 'POST', disabled: false },
{ label: 'GET', value: 'GET' , key: 'GET', disabled: true },
];
export enum SMS_TYPE {
ALIBABA = 'alibaba',
Expand Down

0 comments on commit 4aed73e

Please sign in to comment.