日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

企业微信消息推送卡片按钮互动的使用

發布時間:2023/12/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 企业微信消息推送卡片按钮互动的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

企業微信回調推送消息互動模板

前置條件

# 企業微信后臺中設置了url

按以上設置

# 回調url: http://www.baidu.com# token: xxxxx# EncodingAESKey: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

此處填寫的URL需要有兩個接口, 一個為Get一個為Post Get作為驗證使用

Post作為驗證后的調用接口

import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.InputStream; import java.io.PrintWriter; import java.util.Map;@RestController @RequestMapping("/wxCallback") public class WxCallbackController {private Environment environment;public static final String token = "xxxxxx";public static final String encodingAesKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";/*** 企業微信回調審批* @return*/@ResponseBody@PostMapping(value = "/callbackCheck")public String callback(final HttpServletRequest request,@RequestParam(name = "msg_signature") final String sMsgSignature,@RequestParam(name = "timestamp") final String sTimestamp,@RequestParam(name = "nonce") final String sNonce) {try {String corpid = environment.getProperty("wx.corpid");InputStream inputStream = request.getInputStream();String sPostData = IOUtils.toString(inputStream, "UTF-8");WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token,corpid, encodingAesKey);//解密String sMsg = wxcpt.DecryptMsg(sMsgSignature, sTimestamp, sNonce, sPostData);//將post數據轉換為mapMap<String, String> dataMap = MessageUtil.parseXml(sMsg);//然后去操作你的業務邏輯} catch (Exception e) {e.printStackTrace();}return "success";}/*** get 請求 驗簽.** @param msgSignature 加密* @param timestamp 時間戳* @param nonce 隨機* @param echostr .* @param response .* @throws Exception .*/@GetMapping(value = "/callbackCheck")public void callbackCheck(@RequestParam(name = "msg_signature") final String msgSignature,@RequestParam(name = "timestamp") final String timestamp,@RequestParam(name = "nonce") final String nonce,@RequestParam(name = "echostr") final String echostr,final HttpServletResponse response) throws Exception {//企業回調的url-----該url不做任何的業務邏輯,僅僅微信查看是否可以調通.String corpid = environment.getProperty("wx.corpid");WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token,corpid, encodingAesKey);// 隨機字符串String sEchoStr = wxcpt.VerifyURL(msgSignature, timestamp, nonce, echostr);PrintWriter out = response.getWriter();try {//必須要返回解密之后的明文if (StringUtils.isBlank(sEchoStr)) {System.out.println("URL驗證失敗");} else {System.out.println("驗證成功!");}} catch (Exception e) {e.printStackTrace();}out.write(sEchoStr);out.flush();} }

其中所用到的加密解密方法都是由企業微信官方提供

https://open.work.weixin.qq.com/wwopen/downloadfile/java.zip

下載地址目前我只使用解密方式是xml的,估計是騰訊的開發大部分是c++

模板卡片事件推送

<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[FromUser]]></FromUserName> <CreateTime>123456789</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[template_card_event]]></Event> <EventKey><![CDATA[key111]]></EventKey> <TaskId><![CDATA[taskid111]]></TaskId> <CardType><![CDATA[text_notice]]></CardType> <ResponseCode><![CDATA[ResponseCode]]></ResponseCode> <AgentID>1</AgentID> <SelectedItems><SelectedItem><QuestionKey><![CDATA[QuestionKey1]]></QuestionKey><OpitonIds><OpitonId><![CDATA[OpitonId1]]></OpitonId><OpitonId><![CDATA[OpitonId2]]></OpitonId></OpitonIds></SelectedItem><SelectedItem><QuestionKey><![CDATA[QuestionKey2]]></QuestionKey><OpitonIds><OpitonId><![CDATA[OpitonId3]]></OpitonId><OpitonId><![CDATA[OpitonId4]]></OpitonId></OpitonIds></SelectedItem> </SelectedItems> </xml> 參數說明
ToUserName企業微信CorpID
FromUserName成員UserID
CreateTime消息創建時間(整型)
MsgType消息類型,此時固定為:event
Event事件類型:template_card_event,點擊任務卡片按鈕
EventKey與發送任務卡片消息時指定的按鈕btn:key值相同
TaskId與發送任務卡片消息時指定的task_id相同
CardType通用模板卡片的類型,類型有”text_notice”, “news_notice”, “button_interaction”, “vote_interaction”, “multiple_interaction”五種
ResponseCode用于調用更新卡片接口的ResponseCode,24小時內有效,且只能使用一次
AgentID企業應用的id,整型。可在應用的設置頁面查看
QuestionKey問題的key值
OpitonIds對應問題的選項列表

根據騰訊官方的文檔,我這次調用需要的就是模板卡板的按鈕交互事件
那么格式如下

參數是否必須說明
touser成員ID列表(消息接收者,多個接收者用‘|’分隔,最多支持1000個)。特殊情況:指定為@all,則向關注該企業應用的全部成員發送
toparty部門ID列表,多個接收者用‘|’分隔,最多支持100個。當touser為@all時忽略本參數
totag標簽ID列表,多個接收者用‘|’分隔,最多支持100個。當touser為@all時忽略本參數
msgtype消息類型,此時固定為:template_card
agentid企業應用的id,整型。企業內部開發,可在應用的設置頁面查看;第三方服務商,可通過接口 獲取企業授權信息 獲取該參數值
card_type模板卡片類型,按鈕交互型卡片填寫”button_interaction”
source卡片來源樣式信息,不需要來源樣式可不填寫
source.icon_url來源圖片的url
source.desc來源圖片的描述,建議不超過20個字
main_title.title一級標題,建議不超過36個字
main_title.desc標題輔助信息,建議不超過44個字
sub_title_text二級普通文本,建議不超過160個字
horizontal_content_list二級標題+文本列表,該字段可為空數組,但有數據的話需確認對應字段是否必填,列表長度不超過6
horizontal_content_list.type鏈接類型,0或不填代表不是鏈接,1 代表跳轉url,2 代表下載附件
horizontal_content_list.keyname二級標題,建議不超過5個字
horizontal_content_list.value二級文本,如果horizontal_content_list.type是2,該字段代表文件名稱(要包含文件類型),建議不超過30個字
horizontal_content_list.url鏈接跳轉的url,horizontal_content_list.type是1時必填
horizontal_content_list.media_id附件的media_id,horizontal_content_list.type是2時必填
card_action整體卡片的點擊跳轉事件
card_action.type跳轉事件類型,0或不填代表不是鏈接,1 代表跳轉url,2 代表打開小程序
card_action.url跳轉事件的url,card_action.type是1時必填
card_action.appid跳轉事件的小程序的appid,必須是與當前應用關聯的小程序,card_action.type是2時必填
card_action.pagepath跳轉事件的小程序的pagepath,card_action.type是2時選填
task_id任務id,同一個應用任務id不能重復,只能由數字、字母和“_-@”組成,最長128字節
button_list按鈕列表,該字段可為空數組,但有數據的話需確認對應字段是否必填,列表長度不超過6
button_list.text按鈕文案,建議不超過10個字
button_list.style按鈕樣式,目前可填1~4,不填或錯填默認1
button_list.key按鈕key值,用戶點擊后,會產生回調事件將本參數作為EventKey返回,回調事件會帶上該key值,最長支持1024字節,不可重復

所以修改為以下代碼作為測試展示信息

/*** 企業微信回調審批* @return*/@ResponseBody@PostMapping(value = "/callbackCheck")public String callback(final HttpServletRequest request,@RequestParam(name = "msg_signature") final String sMsgSignature,@RequestParam(name = "timestamp") final String sTimestamp,@RequestParam(name = "nonce") final String sNonce) {try {String corpid = environment.getProperty("wx.corpid");InputStream inputStream = request.getInputStream();String sPostData = IOUtils.toString(inputStream, "UTF-8");WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token,corpid, encodingAesKey);//解密String sMsg = wxcpt.DecryptMsg(sMsgSignature, sTimestamp, sNonce, sPostData);//將post數據轉換為mapMap<String, String> dataMap = MessageUtil.parseXml(sMsg);//企業內成員idString fromUserName = dataMap.get("FromUserName");// 事件需要 template_card_eventString event = dataMap.get("Event");// EventKey id需要對應上 button_key_1 button_key_2 目前設置兩個按鈕一個為通過 一個為不通過String eventKey = dataMap.get("EventKey");// TODO 在此提供擴展函數log.info("操作人ID:{} , 發起事件:{} , 觸發按鈕:{} ",fromUserName,event,eventKey);// log.info(JSONObject.toJSONString(dataMap));//然后去操作你的業務邏輯} catch (Exception e) {e.printStackTrace();}return "success";}

發送消息模板代碼

/*** 推送交互按鈕卡片模板* @param userId* @param visitorName* @param startTime* @param teacherName* @param appointId*/public void sendMessageNotify(String userId,String visitorName,String startTime,String teacherName,String appointId){String status = environment.getProperty("csadi.msg.status");WxTempLateBtnVo wxTempLateBtnVo = new WxTempLateBtnVo();wxTempLateBtnVo.setCard_type("button_interaction");WxTempLateBtnVo.SourceBean sourceBean = new WxTempLateBtnVo.SourceBean();//消息推送圖片url 沒有不填// sourceBean.setIcon_url();//消息展示信息 在title條上的主題字段sourceBean.setDesc("審核通知");wxTempLateBtnVo.setSource(sourceBean);WxTempLateBtnVo.MainTitleBean mainTitleBean = new WxTempLateBtnVo.MainTitleBean();mainTitleBean.setTitle("待審核");//一級標題 不需要wxTempLateBtnVo.setMain_title(mainTitleBean);// 二級標題wxTempLateBtnVo.setSub_title_text("審核人: "+teacherName);// 整體跳轉的行為 不需要wxTempLateBtnVo.setCard_action(null);String uuid = IdUtil.randomUUID();// 任務編號,隨機生成wxTempLateBtnVo.setTask_id(uuid);LinkedList<WxTempLateBtnVo.HorizontalContentListBean> horizontalContentListBeans = new LinkedList<>();// 第一條信息 鏈接跳轉的url,horizontal_content_list.type是1時必填 2為文件WxTempLateBtnVo.HorizontalContentListBean horizontalContentListBean = new WxTempLateBtnVo.HorizontalContentListBean();horizontalContentListBean.setKeyname("申請人");horizontalContentListBean.setValue(visitorName);horizontalContentListBeans.add(horizontalContentListBean);//第二條信息WxTempLateBtnVo.HorizontalContentListBean horizontalContentListBean1 = new WxTempLateBtnVo.HorizontalContentListBean();horizontalContentListBean1.setKeyname("申請時間");horizontalContentListBean1.setValue(startTime);horizontalContentListBeans.add(horizontalContentListBean1);//第三條信息WxTempLateBtnVo.HorizontalContentListBean horizontalContentListBean2 = new WxTempLateBtnVo.HorizontalContentListBean();horizontalContentListBean2.setType(1);horizontalContentListBean2.setKeyname("審核頁面");horizontalContentListBean2.setValue("點擊訪問");horizontalContentListBean2.setUrl("http://fk.csadi.cn:6060/csadi-visitor-web/mgecheck.html?userId="+userId);horizontalContentListBeans.add(horizontalContentListBean2);wxTempLateBtnVo.setHorizontal_content_list(horizontalContentListBeans);//設置按鈕LinkedList<WxTempLateBtnVo.ButtonListBean> buttonListBeans = new LinkedList<>();WxTempLateBtnVo.ButtonListBean buttonListBean = new WxTempLateBtnVo.ButtonListBean();buttonListBean.setText("審核通過");buttonListBean.setStyle(1);buttonListBean.setKey("button_key_1");buttonListBeans.add(buttonListBean);WxTempLateBtnVo.ButtonListBean buttonListBean1 = new WxTempLateBtnVo.ButtonListBean();buttonListBean1.setText("審核不通過");buttonListBean1.setStyle(3);buttonListBean1.setKey("button_key_2");buttonListBeans.add(buttonListBean1);wxTempLateBtnVo.setButton_list(buttonListBeans);if("1".equals(status)){wxTempLateBtnVo.setAppointId(appointId);//存儲 模板按鈕提供24小時訪問時限,redis同樣設置為24小時String rJson = JSONObject.toJSONString(wxTempLateBtnVo);System.out.printf("redisKey=%s \t\n",userId+uuid,rJson);//這里存著是為了回調用的redisTemplate.opsForValue().set(userId+uuid,rJson,24, TimeUnit.HOURS);sendNotifyBtn(userId,wxTempLateBtnVo);}}

WxSendTeacherVo是我封裝的前端請求參數

public class WxSendTeacherVo {private String openId;private String visitorName;private String reason;private String date;private String phone;private String linkUrl;private String reviewDate;private String teacherName;public String getOpenId() {return openId;}public void setOpenId(String openId) {this.openId = openId;}public String getVisitorName() {return visitorName;}public void setVisitorName(String visitorName) {this.visitorName = visitorName;}public String getReason() {return reason;}public void setReason(String reason) {this.reason = reason;}public String getDate() {return date;}public void setDate(String date) {this.date = date;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getLinkUrl() {return linkUrl;}public void setLinkUrl(String linkUrl) {this.linkUrl = linkUrl;}public String getReviewDate() {return reviewDate;}public void setReviewDate(String reviewDate) {this.reviewDate = reviewDate;}public String getTeacherName() {return teacherName;}public void setTeacherName(String teacherName) {this.teacherName = teacherName;} }

WxTempLateBtnVo是 消息事件模板按鈕互動卡片的發送參數實體類

/*** @author fudon* @version 1.0* @date 2021-08-25 16:09*/public class WxTempLateBtnVo {/*** card_type : button_interaction* source : {"icon_url":"圖片的url","desc":"企業微信"}* main_title : {"title":"歡迎使用企業微信","desc":"您的好友正在邀請您加入企業微信"}* sub_title_text : 下載企業微信還能搶紅包!* horizontal_content_list : [{"keyname":"邀請人","value":"張三"},{"type":1,"keyname":"企業微信官網","value":"點擊訪問","url":"https://work.weixin.qq.com"},{"type":2,"keyname":"企業微信下載","value":"企業微信.apk","media_id":"文件的media_id"}]* card_action : {"type":2,"url":"https://work.weixin.qq.com","appid":"小程序的appid","pagepath":"/index.html"}* task_id : task_id* button_list : [{"text":"按鈕1","style":1,"key":"button_key_1"},{"text":"按鈕2","style":2,"key":"button_key_2"}]*/private String card_type;private SourceBean source;private MainTitleBean main_title;private String sub_title_text;private CardActionBean card_action;private String task_id;private List<HorizontalContentListBean> horizontal_content_list;private List<ButtonListBean> button_list;public static void main(String[] args) {WxTempLateBtnVo wxTempLateBtnVo = new WxTempLateBtnVo();wxTempLateBtnVo.setCard_type("");wxTempLateBtnVo.setSource(new SourceBean());wxTempLateBtnVo.setMain_title(new MainTitleBean());wxTempLateBtnVo.setSub_title_text("");wxTempLateBtnVo.setCard_action(new CardActionBean());wxTempLateBtnVo.setTask_id("");wxTempLateBtnVo.setHorizontal_content_list(Lists.newArrayList());wxTempLateBtnVo.setButton_list(Lists.newArrayList());String s = JSONObject.toJSONString(wxTempLateBtnVo);System.out.println(s );}public String getCard_type() {return card_type;}public void setCard_type(String card_type) {this.card_type = card_type;}public SourceBean getSource() {return source;}public void setSource(SourceBean source) {this.source = source;}public MainTitleBean getMain_title() {return main_title;}public void setMain_title(MainTitleBean main_title) {this.main_title = main_title;}public String getSub_title_text() {return sub_title_text;}public void setSub_title_text(String sub_title_text) {this.sub_title_text = sub_title_text;}public CardActionBean getCard_action() {return card_action;}public void setCard_action(CardActionBean card_action) {this.card_action = card_action;}public String getTask_id() {return task_id;}public void setTask_id(String task_id) {this.task_id = task_id;}public List<HorizontalContentListBean> getHorizontal_content_list() {return horizontal_content_list;}public void setHorizontal_content_list(List<HorizontalContentListBean> horizontal_content_list) {this.horizontal_content_list = horizontal_content_list;}public List<ButtonListBean> getButton_list() {return button_list;}public void setButton_list(List<ButtonListBean> button_list) {this.button_list = button_list;}public static class SourceBean {/*** icon_url : 圖片的url* desc : 企業微信*/private String icon_url;private String desc;public String getIcon_url() {return icon_url;}public void setIcon_url(String icon_url) {this.icon_url = icon_url;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}}public static class MainTitleBean {/*** title : 歡迎使用企業微信* desc : 您的好友正在邀請您加入企業微信*/private String title;private String desc;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}}public static class CardActionBean {/*** type : 2* url : https://work.weixin.qq.com* appid : 小程序的appid* pagepath : /index.html*/private int type;private String url;private String appid;private String pagepath;public int getType() {return type;}public void setType(int type) {this.type = type;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getAppid() {return appid;}public void setAppid(String appid) {this.appid = appid;}public String getPagepath() {return pagepath;}public void setPagepath(String pagepath) {this.pagepath = pagepath;}}public static class HorizontalContentListBean {/*** keyname : 邀請人* value : 張三* type : 1* url : https://work.weixin.qq.com* media_id : 文件的media_id*/private String keyname;private String value;private int type;private String url;private String media_id;public String getKeyname() {return keyname;}public void setKeyname(String keyname) {this.keyname = keyname;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}public int getType() {return type;}public void setType(int type) {this.type = type;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getMedia_id() {return media_id;}public void setMedia_id(String media_id) {this.media_id = media_id;}}public static class ButtonListBean {/*** text : 按鈕1* style : 1* key : button_key_1*/private String text;private int style;private String key;public String getText() {return text;}public void setText(String text) {this.text = text;}public int getStyle() {return style;}public void setStyle(int style) {this.style = style;}public String getKey() {return key;}public void setKey(String key) {this.key = key;}}//按鈕模板消息}

最終發送方法

// 發送消息通知 帶交互按鈕public void sendNotifyBtn(String userId, WxTempLateBtnVo wxMessageVo){String token = queryAccessToken();HttpRequest post = HttpRequest.post(" https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+token);Map<String,Object> map = new LinkedHashMap<>();map.put("touser",userId);//群組信息 不需要// map.put("toparty",null);//標簽信息 不需要// map.put("totag",null);//enable_id_trans enable_duplicate_check duplicate_check_interval 均有默認值,不填map.put("msgtype","template_card");map.put("agentid",environment.getProperty("wx.agentId"));//設置模板信息map.put("template_card",wxMessageVo);String param = JSON.toJSONString(map);post.body(param);HttpResponse execute = post.execute();String body = execute.body();logger.info("企業微信消息通知入參:"+param);logger.info("企業微信消息通知返參:"+body);}

更新消息模板

按鈕展示了有觸發了,現在有個問題,處理完了原先的頁面怎么搞,畢竟沒有頁面的改變事件,找了一下在消息推送的文檔下面一欄還有個更新消息模板

參數說明:

參數是否必須說明
userids企業的成員ID列表(最多支持1000個)
partyids企業的部門ID列表(最多支持100個)
tagids企業的標簽ID列表(最多支持100個)
atall更新整個任務接收人員
agentid應用的agentid
response_code更新卡片所需要消費的code,可通過發消息接口和回調接口返回值獲取,一個code只能調用一次該接口,且只能在24小時內調用
card_type模板卡片類型,按鈕交互型卡片填寫”button_interaction”
source卡片來源樣式信息,不需要來源樣式可不填寫
source.icon_url來源圖片的url
source.desc來源圖片的描述,建議不超過20個字
main_title.title一級標題,建議不超過36個字
main_title.desc標題輔助信息,建議不超過44個字
sub_title_text二級普通文本,建議不超過160個字
horizontal_content_list二級標題+文本列表,該字段可為空數組,但有數據的話需確認對應字段是否必填,列表長度不超過6
horizontal_content_list.type鏈接類型,0或不填代表不是鏈接,1 代表跳轉url,2 代表下載附件
horizontal_content_list.keyname二級標題,建議不超過5個字
horizontal_content_list.value二級文本,如果horizontal_content_list.type是2,該字段代表文件名稱(要包含文件類型),建議不超過30個字
horizontal_content_list.url鏈接跳轉的url,horizontal_content_list.type是1時必填
horizontal_content_list.media_id附件的media_id,horizontal_content_list.type是2時必填
card_action整體卡片的點擊跳轉事件
card_action.type跳轉事件類型,0或不填代表不是鏈接,1 代表跳轉url,2 代表打開小程序
card_action.url跳轉事件的url,card_action.type是1時必填
card_action.appid跳轉事件的小程序的appid,card_action.type是2時必填
card_action.pagepath跳轉事件的小程序的pagepath,card_action.type是2時選填
button_list按鈕列表,該字段可為空數組,但有數據的話需確認對應字段是否必填,列表長度不超過6
button_list.text按鈕文案,建議不超過10個字
button_list.style按鈕樣式,目前可填1~4,不填或錯填默認1
button_list.key按鈕key值,用戶點擊后,會產生回調事件將本參數作為EventKey返回,最長支持1024字節,不可重復
replace_text按鈕替換文案,填寫本字段后會展現灰色不可點擊按鈕
/*** 更新交互模板* 該函數與sendNotifyBtn只有一處url不同* @param userId* @param wxMessageVo*/public void updateTemplateBtnMsg(String userId,String response_code, WxTempLateBtnVo wxMessageVo){String token = queryAccessToken();HttpRequest post = HttpRequest.post("https://qyapi.weixin.qq.com/cgi-bin/message/update_template_card?access_token="+token);Map<String,Object> map = new LinkedHashMap<>();ArrayList<String> list = new ArrayList<>();// 修改對象的userId,這里是傳遞的一個數組,可以做批量修改list.add(userId);map.put("userids",list);map.put("response_code",response_code);//群組信息 不需要//標簽信息 不需要//enable_id_trans enable_duplicate_check duplicate_check_interval 均有默認值,不填 // map.put("msgtype","template_card");map.put("agentid",environment.getProperty("wx.agentId"));//設置模板信息map.put("template_card",wxMessageVo);//設置第二個按鈕為空 為了展示消息String param = JSON.toJSONString(map);post.body(param);HttpResponse execute = post.execute();String body = execute.body();logger.info("更新模板交互:"+param);logger.info("模板交互信息返回:"+body);}

這里為為了業務方便塞了幾個id進去方便后續操作

queryAccessToken();是封裝的拿企業微信操作token的方法,這個看官方文檔吧,第一步應該就是

這個參數實體類進行了修改

import java.util.List;/*** @author fudon* @version 1.0* @date 2021-08-25 16:09*/public class WxTempLateBtnVo {/*** card_type : button_interaction* source : {"icon_url":"圖片的url","desc":"企業微信"}* main_title : {"title":"歡迎使用企業微信","desc":"您的好友正在邀請您加入企業微信"}* sub_title_text : 下載企業微信還能搶紅包!* horizontal_content_list : [{"keyname":"邀請人","value":"張三"},{"type":1,"keyname":"企業微信官網","value":"點擊訪問","url":"https://work.weixin.qq.com"},{"type":2,"keyname":"企業微信下載","value":"企業微信.apk","media_id":"文件的media_id"}]* card_action : {"type":2,"url":"https://work.weixin.qq.com","appid":"小程序的appid","pagepath":"/index.html"}* task_id : task_id* button_list : [{"text":"按鈕1","style":1,"key":"button_key_1"},{"text":"按鈕2","style":2,"key":"button_key_2"}]*/private String card_type;private SourceBean source;private MainTitleBean main_title;private String sub_title_text;private CardActionBean card_action;private String appointId;public String getAppointId() {return appointId;}public void setAppointId(String appointId) {this.appointId = appointId;}public String getReplace_text() {return replace_text;}public void setReplace_text(String replace_text) {this.replace_text = replace_text;}private String task_id;private List<HorizontalContentListBean> horizontal_content_list;private List<ButtonListBean> button_list;private String replace_text;public static void main(String[] args) {WxTempLateBtnVo wxTempLateBtnVo = new WxTempLateBtnVo();wxTempLateBtnVo.setCard_type("");wxTempLateBtnVo.setSource(new SourceBean());wxTempLateBtnVo.setMain_title(new MainTitleBean());wxTempLateBtnVo.setSub_title_text("");wxTempLateBtnVo.setCard_action(new CardActionBean());wxTempLateBtnVo.setTask_id("");wxTempLateBtnVo.setHorizontal_content_list(Lists.newArrayList());wxTempLateBtnVo.setButton_list(Lists.newArrayList());String s = JSONObject.toJSONString(wxTempLateBtnVo);System.out.println(s);}public String getCard_type() {return card_type;}public void setCard_type(String card_type) {this.card_type = card_type;}public SourceBean getSource() {return source;}public void setSource(SourceBean source) {this.source = source;}public MainTitleBean getMain_title() {return main_title;}public void setMain_title(MainTitleBean main_title) {this.main_title = main_title;}public String getSub_title_text() {return sub_title_text;}public void setSub_title_text(String sub_title_text) {this.sub_title_text = sub_title_text;}public CardActionBean getCard_action() {return card_action;}public void setCard_action(CardActionBean card_action) {this.card_action = card_action;}public String getTask_id() {return task_id;}public void setTask_id(String task_id) {this.task_id = task_id;}public List<HorizontalContentListBean> getHorizontal_content_list() {return horizontal_content_list;}public void setHorizontal_content_list(List<HorizontalContentListBean> horizontal_content_list) {this.horizontal_content_list = horizontal_content_list;}public List<ButtonListBean> getButton_list() {return button_list;}public void setButton_list(List<ButtonListBean> button_list) {this.button_list = button_list;}public static class SourceBean {/*** icon_url : 圖片的url* desc : 企業微信*/private String icon_url;private String desc;public String getIcon_url() {return icon_url;}public void setIcon_url(String icon_url) {this.icon_url = icon_url;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}}public static class MainTitleBean {/*** title : 歡迎使用企業微信* desc : 您的好友正在邀請您加入企業微信*/private String title;private String desc;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}}public static class CardActionBean {/*** type : 2* url : https://work.weixin.qq.com* appid : 小程序的appid* pagepath : /index.html*/private int type;private String url;private String appid;private String pagepath;public int getType() {return type;}public void setType(int type) {this.type = type;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getAppid() {return appid;}public void setAppid(String appid) {this.appid = appid;}public String getPagepath() {return pagepath;}public void setPagepath(String pagepath) {this.pagepath = pagepath;}}public static class HorizontalContentListBean {/*** keyname : 邀請人* value : 張三* type : 1* url : https://work.weixin.qq.com* media_id : 文件的media_id*/private String keyname;private String value;private int type;private String url;private String media_id;public String getKeyname() {return keyname;}public void setKeyname(String keyname) {this.keyname = keyname;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}public int getType() {return type;}public void setType(int type) {this.type = type;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getMedia_id() {return media_id;}public void setMedia_id(String media_id) {this.media_id = media_id;}}public static class ButtonListBean {/*** text : 按鈕1* style : 1* key : button_key_1*/private String text;private int style;private String key;public String getText() {return text;}public void setText(String text) {this.text = text;}public int getStyle() {return style;}public void setStyle(int style) {this.style = style;}public String getKey() {return key;}public void setKey(String key) {this.key = key;}}//按鈕模板消息}

只增加兩個字段,公用一個實體,懶得再做新建了

不要問為什么不用lombok,這個是Json工具生成的實體類,加的比較麻煩

回調處理變更如下:
/*** 企業微信回調審批** @return*/@ResponseBody@PostMapping(value = "/callbackCheck")public String callback(final HttpServletRequest request,@RequestParam(name = "msg_signature") final String sMsgSignature,@RequestParam(name = "timestamp") final String sTimestamp,@RequestParam(name = "nonce") final String sNonce) {try {String corpid = environment.getProperty("wx.corpid");InputStream inputStream = request.getInputStream();String sPostData = IOUtils.toString(inputStream, "UTF-8");WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAesKey, corpid);//解密String sMsg = wxcpt.DecryptMsg(sMsgSignature, sTimestamp, sNonce, sPostData);//將post數據轉換為mapMap<String, String> dataMap = MessageUtil.parseXml(sMsg);//企業內成員idString fromUserName = dataMap.get("FromUserName");// 事件需要 template_card_eventString event = dataMap.get("Event");// 獲取任務執行號 我將部分參數傳遞在對應json數據中String taskId = dataMap.get("TaskId");// EventKey id需要對應上 button_key_1 button_key_2 目前設置兩個按鈕一個為通過 一個為不通過String eventKey = dataMap.get("EventKey");// 過濾該事件if(StrUtil.compare("button_key_3",eventKey,true)!=0){return "success";}String ResponseCode = dataMap.get("ResponseCode");log.info("操作人ID:{} , 發起事件:{} , 觸發按鈕:{} ", fromUserName, event, eventKey);// TODO: 2021-08-26String rJson = (String) redisTemplate.opsForValue().get(fromUserName + taskId);log.info("redisKey={}",fromUserName + taskId);//找不到對象也需要修改樣式 /* if (StrUtil.isBlank(rJson)) {log.error("找不到操作對象 UserId={}",fromUserName);return "success";}*/redisTemplate.delete(fromUserName + taskId);WxTempLateBtnVo wxTempLateBtnVo = JSONObject.parseObject(rJson, WxTempLateBtnVo.class);WxTempLateBtnVo.MainTitleBean mainTitleBean = new WxTempLateBtnVo.MainTitleBean();mainTitleBean.setTitle("已處理");wxTempLateBtnVo.setMain_title(mainTitleBean);//設置按鈕LinkedList<WxTempLateBtnVo.ButtonListBean> buttonListBeans = new LinkedList<>();WxTempLateBtnVo.ButtonListBean buttonListBean = new WxTempLateBtnVo.ButtonListBean();// 設置為沒有字僅僅是因為我沒放按鈕他報錯了,所以設置為空buttonListBean.setText("");// 白色樣式看起來融入一體了,加上上面為空的顯示buttonListBean.setStyle(4);// 用于過濾請求的keyeventbuttonListBean.setKey("button_key_3");buttonListBeans.add(buttonListBean);//改為一個按鈕wxTempLateBtnVo.setButton_list(buttonListBeans); // wxTempLateBtnVo.setButton_list(new LinkedList<>());//替換按鈕為文字wxTempLateBtnVo.setReplace_text("已處理");//更新模板weixinUtils.updateTemplateBtnMsg(fromUserName,ResponseCode, wxTempLateBtnVo);String appointId = wxTempLateBtnVo.getAppointId();Appoint byId = appointService.getById(appointId);// 此處填寫事件處理 以下都是業務代碼,和實現無關switch (eventKey){// 審核通過case "button_key_1":byId.setStatus(2);teacherController.updateAppointStatus(byId);break;//審核不通過case "button_key_2":byId.setStatus(0);teacherController.updateAppointStatus(byId);break;default:break;}} catch (Exception e) {e.printStackTrace();}return "success";}


這里這個處理完成按鈕不知道為什么截圖出來是灰色的,但是在app上顯示是白色的,可能是有色差吧

總結

以上是生活随笔為你收集整理的企业微信消息推送卡片按钮互动的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。