Skip to content

Commit

Permalink
#1046 企业微信增加支持最新添加的任务卡片消息
Browse files Browse the repository at this point in the history
  • Loading branch information
domainname authored and binarywang committed May 17, 2019
1 parent 70a7781 commit e9e7f6e
Show file tree
Hide file tree
Showing 15 changed files with 420 additions and 32 deletions.
Expand Up @@ -88,6 +88,11 @@ public static class KefuMsgType {
* 小程序卡片(要求小程序与公众号已关联)
*/
public static final String MINIPROGRAMPAGE = "miniprogrampage";

/**
* 任务卡片消息
*/
public static final String TASKCARD = "taskcard";
}

/**
Expand Down
Expand Up @@ -84,6 +84,11 @@ public static class EventType {
*/
public static final String LOCATION_SELECT = "location_select";

/**
* 任务卡片事件推送.
*/
public static final String TASKCARD_CLICK = "taskcard_click";

}

/**
Expand Down
Expand Up @@ -301,6 +301,13 @@ public interface WxCpService {
*/
WxCpChatService getChatService();

/**
* 获取任务卡片服务
*
* @return 任务卡片服务
*/
WxCpTaskCardService getTaskCardService();

WxCpAgentService getAgentService();

WxCpOAService getOAService();
Expand Down
@@ -0,0 +1,30 @@
package me.chanjar.weixin.cp.api;

import me.chanjar.weixin.common.error.WxErrorException;

import java.util.List;

/**
* <pre>
* 任务卡片管理接口.
* Created by Jeff on 2019-05-16.
* </pre>
*
* @author <a href="https://github.com/domainname">Jeff</a>
* @date 2019-05-16
*/
public interface WxCpTaskCardService {
/**
* <pre>
* 更新任务卡片消息状态
* 详情请见: https://work.weixin.qq.com/api/doc#90000/90135/91579
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre>
*
* @param userIds 企业的成员ID列表
* @param taskId 任务卡片ID
* @param clickedKey 已点击按钮的Key
*/
void update(List<String> userIds, String taskId, String clickedKey) throws WxErrorException;
}
Expand Up @@ -46,6 +46,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
private WxCpTagService tagService = new WxCpTagServiceImpl(this);
private WxCpAgentService agentService = new WxCpAgentServiceImpl(this);
private WxCpOAService oaService = new WxCpOAServiceImpl(this);
private WxCpTaskCardService taskCardService = new WxCpTaskCardServiceImpl(this);

/**
* 全局的是否正在刷新access token的锁
Expand Down Expand Up @@ -392,6 +393,11 @@ public WxCpOAService getOAService() {
return oaService;
}

@Override
public WxCpTaskCardService getTaskCardService() {
return taskCardService;
}

@Override
public RequestHttp<?, ?> getRequestHttp() {
return this;
Expand Down
@@ -0,0 +1,39 @@
package me.chanjar.weixin.cp.api.impl;

import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.WxCpTaskCardService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* <pre>
* 任务卡片管理接口.
* Created by Jeff on 2019-05-16.
* </pre>
*
* @author <a href="https://github.com/domainname">Jeff</a>
* @date 2019-05-16
*/
@RequiredArgsConstructor
public class WxCpTaskCardServiceImpl implements WxCpTaskCardService {
private final WxCpService mainService;

@Override
public void update(List<String> userIds, String taskId, String clickedKey) throws WxErrorException {
Integer agentId = this.mainService.getWxCpConfigStorage().getAgentId();

Map<String, Object> data = new HashMap<>(4);
data.put("userids", userIds);
data.put("agentid", agentId);
data.put("task_id", taskId);
data.put("clicked_key", clickedKey);

String url = "https://qyapi.weixin.qq.com/cgi-bin/message/update_taskcard";
this.mainService.post(url, WxGsonBuilder.create().toJson(data));
}
}
@@ -1,26 +1,18 @@
package me.chanjar.weixin.cp.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import lombok.Data;
import me.chanjar.weixin.common.api.WxConsts.KefuMsgType;
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
import me.chanjar.weixin.cp.bean.article.NewArticle;
import me.chanjar.weixin.cp.bean.messagebuilder.FileBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.ImageBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.MarkdownMsgBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.MpnewsBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.NewsBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.TextBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.TextCardBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.VideoBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.VoiceBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.*;
import me.chanjar.weixin.cp.bean.taskcard.TaskCardButton;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
* 消息.
Expand Down Expand Up @@ -49,6 +41,12 @@ public class WxCpMessage implements Serializable {
private List<NewArticle> articles = new ArrayList<>();
private List<MpnewsArticle> mpnewsArticles = new ArrayList<>();

/**
* 任务卡片特有的属性
*/
private String taskId;
private List<TaskCardButton> taskButtons = new ArrayList<>();

/**
* 获得文本消息builder.
*/
Expand Down Expand Up @@ -112,6 +110,13 @@ public static FileBuilder FILE() {
return new FileBuilder();
}

/**
* 获得任务卡片消息builder.
*/
public static TaskCardBuilder TASKCARD() {
return new TaskCardBuilder();
}


/**
* <pre>
Expand All @@ -124,6 +129,7 @@ public static FileBuilder FILE() {
* {@link KefuMsgType#NEWS}
* {@link KefuMsgType#MPNEWS}
* {@link KefuMsgType#MARKDOWN}
* {@link KefuMsgType#TASKCARD}
* </pre>
*
* @param msgType 消息类型
Expand Down Expand Up @@ -249,6 +255,42 @@ private void handleMsgType(JsonObject messageJson) {
messageJson.add("mpnews", newsJsonObject);
break;
}
case KefuMsgType.TASKCARD: {
JsonObject text = new JsonObject();
text.addProperty("title", this.getTitle());
text.addProperty("description", this.getDescription());

if (StringUtils.isNotBlank(this.getUrl())) {
text.addProperty("url", this.getUrl());
}

text.addProperty("task_id", this.getTaskId());

JsonArray buttonJsonArray = new JsonArray();
for (TaskCardButton button : this.getTaskButtons()) {
JsonObject buttonJson = new JsonObject();
buttonJson.addProperty("key", button.getKey());
buttonJson.addProperty("name", button.getName());

if (StringUtils.isNotBlank(button.getReplaceName())) {
buttonJson.addProperty("replace_name", button.getReplaceName());
}

if (StringUtils.isNotBlank(button.getColor())) {
buttonJson.addProperty("color", button.getColor());
}

if (button.getBold() != null) {
buttonJson.addProperty("is_bold", button.getBold());
}

buttonJsonArray.add(buttonJson);
}
text.add("btn", buttonJsonArray);

messageJson.add("taskcard", text);
break;
}
default: {
// do nothing
}
Expand Down
@@ -0,0 +1,42 @@
package me.chanjar.weixin.cp.bean;

import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

import java.io.Serializable;
import java.util.List;

/**
* <pre>
* 更新任务卡片消息状态的返回类
* 参考文档:https://work.weixin.qq.com/api/doc#90000/90135/91579
* Created by Jeff on 2019-05-16.
* </pre>
*
* @author <a href="https://github.com/domainname">Jeff</a>
* @date 2019-05-16
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class WxCpTaskCardUpdateResult implements Serializable {

@SerializedName("errcode")
private Integer errcode;

@SerializedName("errmsg")
private String errmsg;

/**
* 用户列表
*/
@SerializedName("invaliduser")
private List<String> invalidUsers;

public static WxCpTaskCardUpdateResult fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpTaskCardUpdateResult.class);
}
}
@@ -1,18 +1,8 @@
package me.chanjar.weixin.cp.bean;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.thoughtworks.xstream.annotations.XStreamImplicit;
import org.apache.commons.io.IOUtils;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
Expand All @@ -22,6 +12,15 @@
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import me.chanjar.weixin.cp.util.xml.XStreamTransformer;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* <pre>
Expand Down Expand Up @@ -157,6 +156,10 @@ public class WxCpXmlMessage implements Serializable {
@XStreamConverter(value = XStreamCDataConverter.class)
private String recognition;

@XStreamAlias("TaskId")
@XStreamConverter(value = XStreamCDataConverter.class)
private String taskId;

/**
* 通讯录变更事件.
* 请参考常量 me.chanjar.weixin.cp.WxCpConsts.ContactChangeType
Expand Down
@@ -0,0 +1,68 @@
package me.chanjar.weixin.cp.bean.messagebuilder;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.taskcard.TaskCardButton;

import java.util.List;

/**
* <pre>
* 任务卡片消息Builder
* 用法: WxCustomMessage m = WxCustomMessage.TASKCARD().title(...)....toUser(...).build();
* </pre>
*
* @author <a href="https://github.com/domainname">Jeff</a>
* @date 2019-05-16
*/
public class TaskCardBuilder extends BaseBuilder<TaskCardBuilder> {
private String title;
private String description;
private String url;
private String taskId;
/**
* 按钮个数为1~2个
*/
private List<TaskCardButton> buttons;

public TaskCardBuilder() {
this.msgType = WxConsts.KefuMsgType.TASKCARD;
}

public TaskCardBuilder title(String title) {
this.title = title;
return this;
}

public TaskCardBuilder description(String description) {
this.description = description;
return this;
}

public TaskCardBuilder url(String url) {
this.url = url;
return this;
}

public TaskCardBuilder taskId(String taskId) {
this.taskId = taskId;
return this;
}

public TaskCardBuilder buttons(List<TaskCardButton> buttons) {
this.buttons = buttons;
return this;
}

@Override
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setSafe(null);
m.setTitle(this.title);
m.setDescription(this.description);
m.setUrl(this.url);
m.setTaskId(this.taskId);
m.setTaskButtons(this.buttons);
return m;
}
}

0 comments on commit e9e7f6e

Please sign in to comment.