1.需求場景
1.1 在我們公司的國際官網上有一處手機客戶意見反饋的地方,見:http://www.alilo-world.com/contact.html 。我們售後的一個需求就是當客戶有反饋的時候能夠及時通知到他們,然後能準確的、實時的處理客戶的反饋。
2.解決方案
2.1 使用阿里的釘釘智能機器人在羣裏實時的將用戶的反饋通知到特定的人或者全部的人。
2.2 查閱阿里釘釘的自能機器人,見:https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq 。
2.3 先看下最終的一個效果圖:
2.4 實時步驟:
2.4.1 創建一個釘釘羣,在羣裏創建一個釘釘自動機人,後去機器人的webHook地址和secret
2.4.2 下載阿里的sdk
下載阿里的釘釘機器人的sdk的時候會有兩個文件(見下圖),有一個是jar包,另一個是源碼文件。我們只需要使用一個就好了,即:taobao-sdk-java-auto_1479188381469-20200409.jar 。下載地址:https://ding-doc.dingtalk.com/doc#/faquestions/vzbp02
2.4.3 編碼實現以及服務部署
我這邊實現的話是用一個web接口把我們的官網和剛剛創建的釘釘智能機器人鏈接起來的。
2.4.3.1 web接口部分:
@RestController
@RequestMapping("/feedback")
public class TbCustomerFeedbackController {
//這裏因爲業務邏輯簡單直接用Dao層來操作數據庫了
@Autowired
private TbCustomerFeedbackDao customerFeedbackDao;
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public Result addCustomerFeedBack(final TbCustomerFeedback customerFeedback, HttpServletRequest request) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
customerFeedback.setIp(HttpUtil.getRemortIP(request));
customerFeedback.setCreateTime(new Date());
int row = customerFeedbackDao.insert(customerFeedback);
//調用阿里的釘釘機器人的sdk發送消息到自己創建的機器人
ThreadPoolUtils.runTask(()->{
try {
DingTalkAutoRobotUtils.sendMsg(customerFeedback);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
});
return row > 0 ? Result.success() : Result.fail("0", "反饋信息失敗!");
}
}
2.4.3.2 釘釘機器人的代碼部分(access_token和secret替換成自己創建的機器人的即可):
public class DingTalkAutoRobotUtils {
public static void sendMsg(TbCustomerFeedback customerFeedback) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
String url = "https://oapi.dingtalk.com/robot/send?access_token=5695bfecf2302c9f3fe42db78f3378b7XXXXXXXXXXX655ad27638751×tamp=" + System.currentTimeMillis() + "&sign=" + getDingTalkaAutoRobotSing();
DingTalkClient client = new DefaultDingTalkClient(url);
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("text");
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
StringBuilder msg = new StringBuilder();
msg.append("反饋主題:").append(customerFeedback.getTheme());
msg.append("\n");
msg.append("反饋人:").append(customerFeedback.getName());
msg.append("\n");
msg.append("聯方式:").append(customerFeedback.getEmail());
msg.append("\n");
msg.append("反饋時間:").append(DateUtil.dateToDateStr(customerFeedback.getCreateTime()));
msg.append("\n");
msg.append("反饋內容:").append(customerFeedback.getContent());
msg.append("\n");
text.setContent(msg.toString());
request.setText(text);
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setIsAtAll("true");
request.setAt(at);
//下面是其他類型的消息,我只是用了text類型具體支持哪些參照:https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
// request.setMsgtype("link");
// OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
// link.setMessageUrl("https://www.dingtalk.com/");
// link.setPicUrl("");
// link.setTitle("時代的火車向前開");
// link.setText("這個即將發佈的新版本,創始人xx稱它爲“紅樹林”。\n" +
// "而在此之前,每當面臨重大升級,產品經理們都會取一個應景的代號,這一次,爲什麼是“紅樹林");
// request.setLink(link);
//
// request.setMsgtype("markdown");
// OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
// markdown.setTitle("杭州天氣");
// markdown.setText("#### 杭州天氣 @156xxxx8827\n" +
// "> 9度,西北風1級,空氣良89,相對溫度73%\n\n" +
// "> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n" +
// "> ###### 10點20分發布 [天氣](http://www.thinkpage.cn/) \n");
// request.setMarkdown(markdown);
try {
OapiRobotSendResponse response = client.execute(request);
} catch (ApiException e) {
e.printStackTrace();
}
}
private static String getDingTalkaAutoRobotSing() throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
Long timestamp = System.currentTimeMillis();
String secret = "SEC8c4bce59281ffbbc04XXXXXXXXXX43b92275477a01163f8ae";
String stringToSign = timestamp + "\n" + secret;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
return sign;
}
}