1. 背景
在筆者開(kāi)發(fā)的大數(shù)據(jù)平臺(tái)XSailboat中有 消息中心 模塊,用來(lái)全平臺(tái)的消息收集,整理分拆、訂閱發(fā)送等功能。消息推送方式支持釘釘群聊、釘釘單聊、短信通知?,F(xiàn)記錄一下企業(yè)機(jī)器人消息單聊推送的實(shí)現(xiàn)過(guò)程。
2. 釘釘開(kāi)發(fā)文檔
這是官方的開(kāi)發(fā)文檔地址:《機(jī)器人發(fā)送、查詢和撤回單聊消息》
將其中的主要過(guò)程抄錄一下:
步驟一:登錄開(kāi)發(fā)者后臺(tái),點(diǎn)擊應(yīng)用開(kāi)發(fā)-企業(yè)內(nèi)部開(kāi)發(fā),創(chuàng)建機(jī)器人。
步驟二:點(diǎn)擊機(jī)器人應(yīng)用-基礎(chǔ)信息,獲取AppKey和AppSecret。
步驟三:添加接口調(diào)用權(quán)限,點(diǎn)擊“機(jī)器人”,申請(qǐng)企業(yè)內(nèi)機(jī)器人發(fā)送消息權(quán)限。申請(qǐng)權(quán)限無(wú)需審批,默認(rèn)開(kāi)通。
步驟四:上線機(jī)器人。進(jìn)入版本管理與發(fā)布頁(yè)面,點(diǎn)擊上線,機(jī)器人的狀態(tài)變更為已發(fā)布。
步驟五:獲取應(yīng)用訪問(wèn)憑證獲取企業(yè)內(nèi)部應(yīng)用的accessToken。調(diào)用接口時(shí),通過(guò)accessToken鑒權(quán)調(diào)用者身份。
步驟六:調(diào)用機(jī)器人單聊相關(guān)的API:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-823624.html
- 調(diào)用服務(wù)端API-批量發(fā)送單聊消息,獲取消息processQueryKey。
- 根據(jù)消息processQueryKey,調(diào)用服務(wù)端API-批量查詢單聊機(jī)器人是否已讀,查詢機(jī)器人發(fā)送的單聊消息對(duì)方是否已讀。
- 根據(jù)消息processQueryKey,調(diào)用服務(wù)端API-批量撤回單聊消息,撤回機(jī)器人發(fā)送的單聊消息。
3. 記錄自己的操作過(guò)程
- 上企業(yè)的 開(kāi)發(fā)者后臺(tái) 創(chuàng)建應(yīng)用(應(yīng)用名:大數(shù)據(jù)平臺(tái)-消息中心)。
- 創(chuàng)建了應(yīng)用之后,再點(diǎn)進(jìn)應(yīng)用,創(chuàng)建機(jī)器人,創(chuàng)建好了之后發(fā)布。
- 申請(qǐng)“機(jī)器人”里面的 “企業(yè)內(nèi)機(jī)器人發(fā)送消息權(quán)限” 和“通訊錄管理”中的 “根據(jù)手機(jī)號(hào)姓名獲取成員信息的接口訪問(wèn)權(quán)限”。
- 根絕Appkey和AppSecret獲取token。
- 《獲取企業(yè)內(nèi)部應(yīng)用的access_token》
- 定向向某人發(fā)送釘釘消息時(shí)需要指定userId,而消息中心配置的消息訂閱人設(shè)置的是手機(jī)號(hào)。所以需要將手機(jī)號(hào)翻譯成userId。
- 《根據(jù)手機(jī)號(hào)查詢企業(yè)賬號(hào)用戶》
- 發(fā)送消息的API。
- 《批量發(fā)送人與機(jī)器人會(huì)話中機(jī)器人消息》
4. 示例代碼
package com.cimstech.sailboat.ms.msg;
import com.cimstech.sailboat.ms.msg.sender.Letter;
import com.cimstech.xfront.common.http.HttpClient;
import com.cimstech.xfront.common.http.Request;
import com.cimstech.xfront.common.json.JSONArray;
import com.cimstech.xfront.common.json.JSONObject;
import com.cimstech.xfront.common.text.XString;
import com.cimstech.xfront.common.time.XTime;
public class Test
{
/**
* 獲取token,查詢userId的服務(wù)地址
*/
static final String sDingService = "https://oapi.dingtalk.com" ;
/**
* 發(fā)送釘釘消息的服務(wù)地址
*/
static final String sDingSendService = "https://api.dingtalk.com" ;
/**
* 憑借appKey和appSecret獲取token
*/
static final String sPath_GetToken = "/gettoken" ;
/**
* 通過(guò)手機(jī)號(hào)獲取企業(yè)賬號(hào)用戶
*/
static final String sPath_GetUserByMobile = "/topapi/v2/user/getbymobile" ;
static final String sPath_SendMsgOfSingleChat = "/v1.0/robot/oToMessages/batchSend" ;
static final String sDetailPtn_n = "[以及另外{}條詳情..]({})" ;
static final String sDetailPtn_1 = "[詳情..]({})" ;
static final String sNoLink = "以及另外{}條" ;
static String[] sColors = new String[] {"#FF0000"
, "#ffa500"
, "#0fdbdb"
, "#12e512"} ;
static String sDetailLink = "http://XXXXX/sailmsg/msgrow?sendBatch={}&totalAmount={}" ;
static JSONObject toJSON(Letter aLetter)
{
int level = aLetter.getLevel() ;
String color = level < sColors.length? sColors[level]:null ;
StringBuilder strBld = new StringBuilder("【消息中心-").append(aLetter.getSubscriberItemName())
.append("】")
.append("-[") ;
if(color != null)
strBld.append("<font color=").append(color).append(">") ;
strBld.append(level).append("級(jí)") ;
if(color != null)
strBld.append("</font>") ;
// String partition = aLetter.getPartition() ;
strBld.append("]") ; // .append(sSiteMap.getOrDefault(partition , partition)) ;
String title = strBld.toString() ;
strBld.setLength(0);
strBld.append("**").append(title).append("** \n > ")
.append("<font color=#000000>")
.append(aLetter.getContent())
.append("</font>")
.append(" \n \n ") ;
strBld.append("<font color=#666666>")
.append(aLetter.getEventTime())
.append("</font>")
.append(" ") ;
if(XString.isEmpty(sDetailLink))
{
if(aLetter.getReduceAmount() > 0)
strBld.append(XString.msgFmt(sNoLink , aLetter.getReduceAmount())) ;
}
else
{
if(aLetter.getReduceAmount() > 1)
{
strBld.append(XString.msgFmt(sDetailPtn_n, aLetter.getReduceAmount()
, XString.msgFmt(sDetailLink , aLetter.getSendBatch() , aLetter.getReduceAmount()))) ;
}
else
{
strBld.append(XString.msgFmt(sDetailPtn_1
, XString.msgFmt(sDetailLink , aLetter.getSendBatch() , aLetter.getReduceAmount()))) ;
}
}
String content = strBld.toString() ;
return new JSONObject().put("title" , title)
.put("text", content)
;
}
public static void main(String[] args) throws Exception
{
String appKey = "ddddddddddddddd" ;
String appSecret = "ssssssssssssssssssssssssssssssss" ;
String mobile = "130XXXXXXXXX" ;
String robotCode = "dingXXXXXXXXX" ;
// 1.獲取AccessToken
HttpClient client = HttpClient.ofUrl(sDingService) ;
long expireTime = System.currentTimeMillis() ;
/**
* {
* "errcode": 0,
* "access_token": "aaaaaaaaaaaaaaaaa",
* "errmsg": "ok",
* "expires_in": 7200
* }
*/
JSONObject jo = client.askJo(Request.GET().path(sPath_GetToken)
.queryParam("appkey" , appKey)
.queryParam("appsecret", appSecret)) ;
System.out.println("獲取到的Token:"+jo) ;
String accessToken = jo.optString("access_token") ;
expireTime += jo.optLong("expires_in") * 1000 ;
/**
* {
* "errcode": 0,
* "errmsg": "ok",
* "result":
* {
* "exclusive_account_userid_list": [],
* "userid": "bbbbbbbbbbbbb"
* },
* "request_id": "ccccccc"
* }
*/
// 2. 查詢userId
jo = client.askJo(Request.POST().path(sPath_GetUserByMobile).queryParam("access_token" , accessToken)
.setJsonEntity(new JSONObject().put("mobile" , mobile)
.put("support_exclusive_account_search" , true))) ;
System.out.println("獲取到的User信息:"+jo);
String userId = jo.pathString(null , "result" , "userid") ;
Letter letter = new Letter(null) ;
letter.setLevel(5);
letter.setContent("單聊測(cè)試") ;
letter.setEventTime(XTime.current$yyyyMMddHHmmss());
letter.setReduceAmount(1) ;
letter.setSendBatch("xxxxxxxxxxxxxxx") ;
letter.setSubscriberItemName("單聊測(cè)試訂閱項(xiàng)");
String msgParam = toJSON(letter).toJSONString() ;
/**
* {
* "flowControlledStaffIdList": [],
* "invalidStaffIdList": [],
* "processQueryKey": "xxxxxxxxxxxxxx"
* }
*/
// 3.發(fā)送消息
jo = HttpClient.ofUrl(sDingSendService).askJo(Request.POST().path(sPath_SendMsgOfSingleChat)
.header("x-acs-dingtalk-access-token" , accessToken)
.setJsonEntity(new JSONObject().put("robotCode" , robotCode)
.put("userIds", new JSONArray().put(userId))
.put("msgKey", "sampleMarkdown")
.put("msgParam" , msgParam))) ;
System.out.println("發(fā)送消息收到的回復(fù):"+jo);
}
}
效果:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-823624.html
到了這里,關(guān)于釘釘企業(yè)機(jī)器人單聊消息發(fā)送實(shí)踐-大數(shù)據(jù)平臺(tái)(XSailboat)消息中心消息推送的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!