Skip to content

Commit

Permalink
🆕 #1290 小程序增加动态消息接口
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Feb 17, 2020
1 parent f90f454 commit 27d88a7
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package cn.binarywang.wx.miniapp.api;

import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage;
import cn.binarywang.wx.miniapp.bean.*;
import com.google.gson.JsonObject;
import me.chanjar.weixin.common.error.WxErrorException;

/**
Expand All @@ -18,13 +16,19 @@ public interface WxMaMsgService {
String TEMPLATE_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send";
String SUBSCRIBE_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send";
String UNIFORM_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send";
String ACTIVITY_ID_CREATE_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/activityid/create";
String UPDATABLE_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/updatablemsg/send";

/**
* <pre>
* 发送客服消息
* 详情请见: <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/customerServiceMessage.send.html">发送客服消息</a>
* 接口url格式:https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN
* </pre>
*
* @param message 客服消息
* @return .
* @throws WxErrorException .
*/
boolean sendKefuMsg(WxMaKefuMessage message) throws WxErrorException;

Expand Down Expand Up @@ -53,13 +57,43 @@ public interface WxMaMsgService {
*/
void sendSubscribeMsg(WxMaSubscribeMessage subscribeMessage) throws WxErrorException;


/**
* <pre>
* 下发小程序和公众号统一的服务消息
* 详情请见: <a href="https://developers.weixin.qq.com/miniprogram/dev/api/open-api/uniform-message/sendUniformMessage.html">下发小程序和公众号统一的服务消息</a>
* 接口url格式:https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=ACCESS_TOKEN
* </pre>
*
* @param uniformMessage 消息
* @throws WxErrorException .
*/
void sendUniformMsg(WxMaUniformMessage uniformMessage) throws WxErrorException;

/**
* <pre>
* 创建被分享动态消息的 activity_id.
* 动态消息: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share/updatable-message.html
*
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/updatable-message/updatableMessage.createActivityId.html
* 接口地址:GET https://api.weixin.qq.com/cgi-bin/message/wxopen/activityid/create?access_token=ACCESS_TOKEN
* </pre>
*
* @return .
* @throws WxErrorException .
*/
JsonObject createUpdatableMessageActivityId() throws WxErrorException;

/**
* <pre>
* 修改被分享的动态消息.
* 动态消息: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share/updatable-message.html
*
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/updatable-message/updatableMessage.setUpdatableMsg.html
* 接口地址:POST https://api.weixin.qq.com/cgi-bin/message/wxopen/activityid/create?access_token=ACCESS_TOKEN
* </pre>
*
* @param msg 动态消息
* @throws WxErrorException .
*/
void setUpdatableMsg(WxMaUpdatableMsg msg) throws WxErrorException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import cn.binarywang.wx.miniapp.api.WxMaMsgService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage;
import cn.binarywang.wx.miniapp.bean.*;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -55,4 +53,15 @@ public void sendUniformMsg(WxMaUniformMessage uniformMessage) throws WxErrorExce
}
}

@Override
public JsonObject createUpdatableMessageActivityId() throws WxErrorException {
final String responseContent = this.wxMaService.get(ACTIVITY_ID_CREATE_URL, null);
return JSON_PARSER.parse(responseContent).getAsJsonObject();
}

@Override
public void setUpdatableMsg(WxMaUpdatableMsg msg) throws WxErrorException {
this.wxMaService.post(UPDATABLE_MSG_SEND_URL, WxMaGsonBuilder.create().toJson(msg));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package cn.binarywang.wx.miniapp.bean;

import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.experimental.Accessors;

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

/**
* 动态消息.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2020-02-17
*/
@Data
@Accessors(chain = true)
public class WxMaUpdatableMsg implements Serializable {
private static final long serialVersionUID = 6231957192034798165L;

/**
* 动态消息的 ID,通过 updatableMessage.createActivityId 接口获取
*/
@SerializedName("activity_id")
private String activityId;

/**
* 动态消息修改后的状态
* 0 未开始
* 1 已开始
*/
@SerializedName("target_state")
private Integer targetState;

/**
* 动态消息对应的模板信息
*/
@SerializedName("template_info")
private TemplateInfo templateInfo;

@Data
@Accessors(chain = true)
public static class TemplateInfo implements Serializable {
private static final long serialVersionUID = -9218473401759062841L;

/**
* 模板中需要修改的参数
*/
@SerializedName("parameter_list")
private List<Parameter> parameterList;
}

@Data
@Accessors(chain = true)
public static class Parameter implements Serializable {
private static final long serialVersionUID = 7444716050341038046L;

/**
* 要修改的参数名
* <pre>
* 合法值:
* member_count target_state = 0 时必填,文字内容模板中 member_count 的值
* room_limit target_state = 0 时必填,文字内容模板中 room_limit 的值
* path target_state = 1 时必填,点击「进入」启动小程序时使用的路径。对于小游戏,没有页面的概念,可以用于传递查询字符串(query),如 "?foo=bar"
* version_type target_state = 1 时必填,点击「进入」启动小程序时使用的版本。
* 有效参数值为:develop(开发版),trial(体验版),release(正式版)
* </pre>
*/
private String name;

/**
* 修改后的参数值
*/
private String value;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package cn.binarywang.wx.miniapp.api.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.binarywang.wx.miniapp.bean.*;
import org.testng.annotations.*;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.*;
import cn.binarywang.wx.miniapp.test.ApiTestModule;
import cn.binarywang.wx.miniapp.test.TestConfig;
import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

import java.text.SimpleDateFormat;
import java.util.Date;

import static org.assertj.core.api.Assertions.assertThat;

/**
* 测试消息相关接口
Expand All @@ -25,7 +28,8 @@ public class WxMaMsgServiceImplTest {
@Inject
private WxMaService wxService;

public void testSendKefuMessage() throws WxErrorException {
@Test
public void testSendKefuMsg() throws WxErrorException {
TestConfig config = (TestConfig) this.wxService.getWxMaConfig();
WxMaKefuMessage message = WxMaKefuMessage.newTextBuilder()
.toUser(config.getOpenid())
Expand Down Expand Up @@ -74,7 +78,7 @@ public void testSendSubscribeMsg() throws WxErrorException {
@Test
public void testSendUniformMsg() throws WxErrorException {
TestConfig config = (TestConfig) this.wxService.getWxMaConfig();
WxMaUniformMessage message = WxMaUniformMessage.builder()
WxMaUniformMessage message = WxMaUniformMessage.builder()
.isMpTemplateMsg(false)
.toUser(config.getOpenid())
.page("page/page/index")
Expand All @@ -89,4 +93,22 @@ public void testSendUniformMsg() throws WxErrorException {

this.wxService.getMsgService().sendUniformMsg(message);
}

@Test
public void testCreateUpdatableMessageActivityId() throws WxErrorException {
final JsonObject jsonObject = this.wxService.getMsgService().createUpdatableMessageActivityId();
assertThat(jsonObject).isNotNull();
assertThat(jsonObject.get("activity_id")).isNotNull();
System.out.println(jsonObject.get("activity_id"));
assertThat(jsonObject.get("expiration_time")).isNotNull();
}

@Test
public void testSetUpdatableMsg() throws WxErrorException {
this.wxService.getMsgService().setUpdatableMsg(new WxMaUpdatableMsg()
.setActivityId("1048_4f61uDloWPZl9pAs1dGx07vDiHKZ7FwJ0suohS1iMH5z8zhFktYk4nRqqBY~")
.setTargetState(1)
.setTemplateInfo(new WxMaUpdatableMsg.TemplateInfo()
.setParameterList(Lists.newArrayList(new WxMaUpdatableMsg.Parameter().setName("member_count").setValue("1")))));
}
}

0 comments on commit 27d88a7

Please sign in to comment.