1
0
mirror of synced 2025-12-23 18:48:00 +08:00

#907 企业微信增加应用推送消息到群聊会话的接口

This commit is contained in:
Binary Wang
2019-01-26 15:03:44 +08:00
parent 761e88201c
commit a47d91fc24
7 changed files with 389 additions and 59 deletions

View File

@@ -126,4 +126,46 @@ public class WxCpConsts {
public static final String UPDATE_TAG = "update_tag";
}
/**
* 应用推送消息的消息类型.
*/
public static class AppChatMsgType {
/**
* 文本消息.
*/
public static final String TEXT = "text";
/**
* 图片消息.
*/
public static final String IMAGE = "image";
/**
* 语音消息.
*/
public static final String VOICE = "voice";
/**
* 视频消息.
*/
public static final String VIDEO = "video";
/**
* 发送文件CP专用.
*/
public static final String FILE = "file";
/**
* 文本卡片消息CP专用.
*/
public static final String TEXTCARD = "textcard";
/**
* 图文消息(点击跳转到外链).
*/
public static final String NEWS = "news";
/**
* 图文消息(点击跳转到图文消息页面).
*/
public static final String MPNEWS = "mpnews";
/**
* markdown消息.
*/
public static final String MARKDOWN = "markdown";
}
}

View File

@@ -3,46 +3,60 @@ package me.chanjar.weixin.cp.api;
import java.util.List;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpAppChatMessage;
import me.chanjar.weixin.cp.bean.WxCpChat;
/**
* 群聊服务
* 群聊服务.
*
* @author gaigeshen
*/
public interface WxCpChatService {
String APPCHAT_CREATE = "https://qyapi.weixin.qq.com/cgi-bin/appchat/create";
String APPCHAT_UPDATE = "https://qyapi.weixin.qq.com/cgi-bin/appchat/update";
String APPCHAT_GET_CHATID = "https://qyapi.weixin.qq.com/cgi-bin/appchat/get?chatid=";
/**
* 创建群聊会话,注意:刚创建的群,如果没有下发消息,在企业微信不会出现该群
* 创建群聊会话,注意:刚创建的群,如果没有下发消息,在企业微信不会出现该群.
*
* @param name 群聊名最多50个utf8字符超过将截断
* @param owner 指定群主的id。如果不指定系统会随机从userlist中选一人作为群主
* @param users 群成员id列表。至少2人至多500人
* @param name 群聊名最多50个utf8字符超过将截断
* @param owner 指定群主的id。如果不指定系统会随机从userlist中选一人作为群主
* @param users 群成员id列表。至少2人至多500人
* @param chatId 群聊的唯一标志不能与已有的群重复字符串类型最长32个字符。只允许字符0-9及字母a-zA-Z。如果不填系统会随机生成群id
* @return 创建群聊会话的结果,群聊的唯一标志
* @return 创建群聊会话chatId
* @throws WxErrorException 发生异常
*/
String chatCreate(String name, String owner, List<String> users, String chatId) throws WxErrorException;
/**
* 修改群聊会话
*
* @param chatId 群聊id
* @param name 新的群聊名。若不需更新请忽略此参数null or empty。最多50个utf8字符超过将截断
* @param owner 新群主的id。若不需更新请忽略此参数null or empty
* @param usersToAdd 添加成员的id列表若不需要更新则传递空对象或者空集合
* 修改群聊会话.
*
* @param chatId 群聊id
* @param name 新的群聊名。若不需更新请忽略此参数null or empty。最多50个utf8字符超过将截断
* @param owner 新群主的id。若不需更新请忽略此参数null or empty
* @param usersToAdd 添加成员的id列表若不需要更新则传递空对象或者空集合
* @param usersToDelete 踢出成员的id列表若不需要更新则传递空对象或者空集合
* @throws WxErrorException 发生异常
*/
void chatUpdate(String chatId, String name, String owner, List<String> usersToAdd, List<String> usersToDelete) throws WxErrorException;
/**
* 获取群聊会话
*
* 获取群聊会话.
*
* @param chatId 群聊编号
* @return 群聊会话
* @throws WxErrorException 发生异常
*/
WxCpChat chatGet(String chatId) throws WxErrorException;
/**
* 应用支持推送文本、图片、视频、文件、图文等类型.
* 请求方式: POSTHTTPS
* 请求地址: https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=ACCESS_TOKEN
* 文档地址https://work.weixin.qq.com/api/doc#90000/90135/90248
*
* @param message 要发送的消息内容对象
*/
void sendMsg(WxCpAppChatMessage message) throws WxErrorException;
}

View File

@@ -7,30 +7,30 @@ import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import com.google.gson.JsonParser;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.cp.api.WxCpChatService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpAppChatMessage;
import me.chanjar.weixin.cp.bean.WxCpChat;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
/**
* 群聊服务实现
* 群聊服务实现.
*
* @author gaigeshen
*/
public class WxCpChatServiceImpl implements WxCpChatService {
public class WxCpChatServiceImpl implements WxCpChatService {
private static final JsonParser JSON_PARSER = new JsonParser();
private final WxCpService cpService;
private final WxCpService internalService;
/**
* 创建群聊服务实现的实例
*
* @param internalService 企业微信的服务
* 创建群聊服务实现的实例.
*
* @param cpService 企业微信的服务
*/
public WxCpChatServiceImpl(WxCpService internalService) {
this.internalService = internalService;
WxCpChatServiceImpl(WxCpService cpService) {
this.cpService = cpService;
}
@Override
@@ -48,12 +48,13 @@ public class WxCpChatServiceImpl implements WxCpChatService {
if (StringUtils.isNotBlank(chatId)) {
data.put("chatid", chatId);
}
String result = internalService.post("https://qyapi.weixin.qq.com/cgi-bin/appchat/create", WxGsonBuilder.create().toJson(data));
String result = this.cpService.post(APPCHAT_CREATE, WxGsonBuilder.create().toJson(data));
return new JsonParser().parse(result).getAsJsonObject().get("chatid").getAsString();
}
@Override
public void chatUpdate(String chatId, String name, String owner, List<String> usersToAdd, List<String> usersToDelete) throws WxErrorException {
public void chatUpdate(String chatId, String name, String owner, List<String> usersToAdd, List<String> usersToDelete)
throws WxErrorException {
Map<String, Object> data = new HashMap<>(5);
if (StringUtils.isNotBlank(chatId)) {
data.put("chatid", chatId);
@@ -70,14 +71,20 @@ public class WxCpChatServiceImpl implements WxCpChatService {
if (usersToDelete != null && !usersToDelete.isEmpty()) {
data.put("del_user_list", usersToDelete);
}
internalService.post("https://qyapi.weixin.qq.com/cgi-bin/appchat/update", WxGsonBuilder.create().toJson(data));
this.cpService.post(APPCHAT_UPDATE, WxGsonBuilder.create().toJson(data));
}
@Override
public WxCpChat chatGet(String chatId) throws WxErrorException {
String result = internalService.get("https://qyapi.weixin.qq.com/cgi-bin/appchat/get?chatid=" + chatId, null);
return WxCpGsonBuilder.create().fromJson(
new JsonParser().parse(result).getAsJsonObject().getAsJsonObject("chat_info").toString(), WxCpChat.class);
String result = this.cpService.get(APPCHAT_GET_CHATID + chatId, null);
return WxCpGsonBuilder.create()
.fromJson(JSON_PARSER.parse(result).getAsJsonObject().getAsJsonObject("chat_info").toString(), WxCpChat.class);
}
@Override
public void sendMsg(WxCpAppChatMessage message) throws WxErrorException {
this.cpService.post("https://qyapi.weixin.qq.com/cgi-bin/appchat/send", message.toJson());
}
}

View File

@@ -0,0 +1,154 @@
package me.chanjar.weixin.cp.bean;
import java.io.Serializable;
import java.util.List;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.WxCpConsts;
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
import me.chanjar.weixin.cp.bean.article.NewArticle;
/**
* <pre>
* 应用推送消息
* Created by Binary Wang on 2019/1/26.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class WxCpAppChatMessage implements Serializable {
private static final long serialVersionUID = -5469013416372240229L;
private String msgType;
private String content;
private String chatId;
private String mediaId;
private String title;
private String description;
private Boolean safe;
private String url;
private String btnTxt;
private List<NewArticle> articles;
private List<MpnewsArticle> mpnewsArticles;
/**
* 构建文本消息.
*/
public static WxCpAppChatMessage buildTextMsg(String chatId, String content, boolean safe) {
final WxCpAppChatMessage message = new WxCpAppChatMessage();
message.setMsgType(WxCpConsts.AppChatMsgType.TEXT);
message.setContent(content);
message.setChatId(chatId);
message.setSafe(safe);
return message;
}
/**
* 生成json字符串.
*/
public String toJson() {
JsonObject messageJson = new JsonObject();
messageJson.addProperty("msgtype", this.getMsgType());
messageJson.addProperty("chatid", this.getChatId());
if (WxConsts.KefuMsgType.TEXT.equals(this.getMsgType())) {
JsonObject text = new JsonObject();
text.addProperty("content", this.getContent());
messageJson.add("text", text);
}
if (WxConsts.KefuMsgType.MARKDOWN.equals(this.getMsgType())) {
JsonObject text = new JsonObject();
text.addProperty("content", this.getContent());
messageJson.add("markdown", text);
}
if (WxConsts.KefuMsgType.TEXTCARD.equals(this.getMsgType())) {
JsonObject text = new JsonObject();
text.addProperty("title", this.getTitle());
text.addProperty("description", this.getDescription());
text.addProperty("url", this.getUrl());
text.addProperty("btntxt", this.getBtnTxt());
messageJson.add("textcard", text);
}
if (WxConsts.KefuMsgType.IMAGE.equals(this.getMsgType())) {
JsonObject image = new JsonObject();
image.addProperty("media_id", this.getMediaId());
messageJson.add("image", image);
}
if (WxConsts.KefuMsgType.FILE.equals(this.getMsgType())) {
JsonObject image = new JsonObject();
image.addProperty("media_id", this.getMediaId());
messageJson.add("file", image);
}
if (WxConsts.KefuMsgType.VOICE.equals(this.getMsgType())) {
JsonObject voice = new JsonObject();
voice.addProperty("media_id", this.getMediaId());
messageJson.add("voice", voice);
}
if (this.getSafe() != null && this.getSafe()) {
messageJson.addProperty("safe", 1);
}
if (WxConsts.KefuMsgType.VIDEO.equals(this.getMsgType())) {
JsonObject video = new JsonObject();
video.addProperty("media_id", this.getMediaId());
video.addProperty("title", this.getTitle());
video.addProperty("description", this.getDescription());
messageJson.add("video", video);
}
if (WxConsts.KefuMsgType.NEWS.equals(this.getMsgType())) {
JsonObject newsJsonObject = new JsonObject();
JsonArray articleJsonArray = new JsonArray();
for (NewArticle article : this.getArticles()) {
JsonObject articleJson = new JsonObject();
articleJson.addProperty("title", article.getTitle());
articleJson.addProperty("description", article.getDescription());
articleJson.addProperty("url", article.getUrl());
articleJson.addProperty("picurl", article.getPicUrl());
articleJsonArray.add(articleJson);
}
newsJsonObject.add("articles", articleJsonArray);
messageJson.add("news", newsJsonObject);
}
if (WxConsts.KefuMsgType.MPNEWS.equals(this.getMsgType())) {
JsonObject newsJsonObject = new JsonObject();
if (this.getMediaId() != null) {
newsJsonObject.addProperty("media_id", this.getMediaId());
} else {
JsonArray articleJsonArray = new JsonArray();
for (MpnewsArticle article : this.getMpnewsArticles()) {
JsonObject articleJson = new JsonObject();
articleJson.addProperty("title", article.getTitle());
articleJson.addProperty("thumb_media_id", article.getThumbMediaId());
articleJson.addProperty("author", article.getAuthor());
articleJson.addProperty("content_source_url", article.getContentSourceUrl());
articleJson.addProperty("content", article.getContent());
articleJson.addProperty("digest", article.getDigest());
articleJsonArray.add(articleJson);
}
newsJsonObject.add("articles", articleJsonArray);
}
messageJson.add("mpnews", newsJsonObject);
}
return messageJson.toString();
}
}

View File

@@ -1,9 +1,12 @@
package me.chanjar.weixin.cp.bean.article;
import lombok.Data;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* <pre>
* Created by BinaryWang on 2017/3/27.
@@ -12,6 +15,9 @@ import java.io.Serializable;
* @author Binary Wang
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class NewArticle implements Serializable {
private static final long serialVersionUID = 4087852055781140659L;