You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
5.0 KiB
116 lines
5.0 KiB
package com.fr.plugin.dingding.webhook.handle; |
|
|
|
import com.fr.base.Formula; |
|
import com.fr.base.PropertiesUtils; |
|
import com.fr.io.utils.ResourceIOUtils; |
|
import com.fr.json.JSONObject; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.plugin.dingding.webhook.entity.OutputWebHook; |
|
import com.fr.plugin.dingding.webhook.util.DesECBUtil; |
|
import com.fr.plugin.dingding.webhook.util.HttpUtil; |
|
import com.fr.schedule.base.constant.ScheduleConstants; |
|
import com.fr.schedule.feature.output.OutputActionHandler; |
|
import com.fr.stable.CodeUtils; |
|
import com.fr.stable.StringUtils; |
|
|
|
import java.net.URLEncoder; |
|
import java.util.List; |
|
import java.util.Map; |
|
|
|
/** |
|
* @Author fr.open |
|
* @Date 2020/9/15 |
|
* @Description |
|
**/ |
|
public class WebHookHandle extends OutputActionHandler<OutputWebHook> { |
|
private static final String key = "xxxx"; |
|
|
|
private static final String file = "/resources/Robot.png"; |
|
|
|
@Override |
|
public void doAction(OutputWebHook outputWebHook, Map<String, Object> map) throws Exception { |
|
String[] files = (String[]) map.get(ScheduleConstants.OUTPUT_FILES); |
|
getUrl(outputWebHook, map); |
|
String mediaId = StringUtils.EMPTY; |
|
String imageFile = StringUtils.EMPTY; |
|
if(ResourceIOUtils.exist(file)){ |
|
FineLoggerFactory.getLogger().info("read default image {}",file); |
|
imageFile = file; |
|
}else { |
|
for (String file : files) { |
|
if (file.endsWith("png")) { |
|
imageFile = file; |
|
continue; |
|
} |
|
} |
|
} |
|
|
|
JSONObject object = HttpUtil.uploadMedia(imageFile); |
|
FineLoggerFactory.getLogger().info("upload media res is {}", object); |
|
if (HttpUtil.isOk(object)) { |
|
mediaId = object.getJSONObject("url").getString("media_id"); |
|
} |
|
if (StringUtils.isBlank(mediaId)) { |
|
throw new Exception("not contain media or upload failed"); |
|
} |
|
String taskName = CodeUtils.encodeURIComponent((String) map.get(ScheduleConstants.TASK_NAME)); |
|
String path = CodeUtils.encodeURIComponent((String) map.get(ScheduleConstants.SAVE_DIRECTORY)); |
|
path = path.replaceAll("\\+", "%20"); |
|
String showType = (String) map.get(ScheduleConstants.SHOW_TYPE); |
|
int taskType = (Integer) map.get(ScheduleConstants.TASK_TYPE); |
|
String scheduleUsername = (String) map.get(ScheduleConstants.USERNAME); |
|
String resultUrl = getParam(map, "resultUrl"); |
|
String mobileUrl = outputWebHook.getResultURL()+"/url/mobile/schedule/result?taskName=" + taskName + "&username=" + scheduleUsername + "&path=" + path + "&showType=" + showType + "&taskType=" + taskType; |
|
HttpUtil.sendWebhook(mediaId, outputWebHook.getHookUrl(), getParam(map, "title"), getParam(map, "text"), delaSign(StringUtils.isNotBlank(resultUrl)?resultUrl:mobileUrl)); |
|
} |
|
|
|
private String delaSign(String url) { |
|
String path = url; |
|
try { |
|
String user = PropertiesUtils.getProperties("dingtalk").getProperty("resultUser"); |
|
String encode = URLEncoder.encode(DesECBUtil.encryptDES(user, key), "UTF-8"); |
|
if (path.indexOf("?") != -1) { |
|
path += "&result_sign=" + encode; |
|
} else { |
|
path += "?result_sign=" + encode; |
|
} |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
} |
|
FineLoggerFactory.getLogger().info("push dingding url is {}",path); |
|
return path+"&op=h5"; |
|
} |
|
|
|
private String getParam(Map<String, Object> map, String p) { |
|
List<Map> list = (List) map.get(ScheduleConstants.RECORD_LIST); |
|
Object title = StringUtils.EMPTY; |
|
Object param = list.get(0).get(p); |
|
if (param instanceof Formula) { |
|
Formula formula = (Formula) param; |
|
title = formula.getResult(); |
|
} else { |
|
title = param.toString(); |
|
} |
|
return title.toString(); |
|
} |
|
|
|
private String getUrl(OutputWebHook outputWebHook, Map<String, Object> map) { |
|
String taskName = CodeUtils.encodeURIComponent((String) map.get(ScheduleConstants.TASK_NAME)); |
|
String path = CodeUtils.encodeURIComponent((String) map.get(ScheduleConstants.SAVE_DIRECTORY)); |
|
path = path.replaceAll("\\+", "%20"); |
|
String showType = (String) map.get(ScheduleConstants.SHOW_TYPE); |
|
int taskType = (Integer) map.get(ScheduleConstants.TASK_TYPE); |
|
String scheduleUsername = (String) map.get(ScheduleConstants.USERNAME); |
|
return outputWebHook.getResultURL() + "/url/mobile/schedule/result?taskName=" + taskName + "&username=" + scheduleUsername + "&path=" + path + "&showType=" + showType + "&taskType=" + taskType; |
|
} |
|
|
|
private JSONObject getJSONObject(String md5, String base) { |
|
JSONObject obj = JSONObject.create(); |
|
obj.put("msgtype", "image"); |
|
JSONObject image = JSONObject.create(); |
|
image.put("base64", base); |
|
image.put("md5", md5); |
|
obj.put("image", image); |
|
return obj; |
|
} |
|
}
|
|
|