View Javadoc
1   package com.foxinmy.weixin4j.wxa.api;
2   
3   import java.util.Map;
4   import java.util.Properties;
5   
6   import com.foxinmy.weixin4j.exception.WeixinException;
7   import com.foxinmy.weixin4j.token.TokenManager;
8   import com.foxinmy.weixin4j.wxa.model.custommessage.Command;
9   import com.foxinmy.weixin4j.wxa.model.custommessage.CustomMessage;
10  
11  /**
12   * 客服消息。
13   *
14   * @since 1.8
15   */
16  public class CustomMessageApi extends TokenManagerApi {
17  
18  	public CustomMessageApi(TokenManager tokenManager) {
19  		super(tokenManager);
20  	}
21  
22  	public CustomMessageApi(TokenManager tokenManager, Properties properties) {
23  		super(tokenManager, properties);
24  	}
25  
26  	/**
27  	 * 发送客服消息。
28  	 *
29  	 * <p>
30  	 *   当用户和小程序客服产生特定动作的交互时(具体动作列表请见下方说明),
31  	 *   微信将会把消息数据推送给开发者,开发者可以在一段时间内(目前修改为48小时)调用客服接口,
32  	 *   通过POST一个JSON数据包来发送消息给普通用户。
33  	 *   此接口主要用于客服等有人工消息处理环节的功能,方便开发者为用户提供更加优质的服务。
34  	 * </p>
35  	 *
36  	 * @param customMessage the {@link CustomMessage}.
37  	 * @throws WeixinException indicates getting access token failed, or sending custom message failed.
38  	 * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/customer-message/send.html">发送客服消息</a>
39  	 */
40  	public void sendCustomMessage(final CustomMessage customMessage)
41  			throws WeixinException {
42  		final Map<String, Object> params = CustomMessageAdapters.toMap(customMessage);
43  		final WxaApiResult r = this.post("message_custom_send",
44  			params, WxaApiResult.TYPE_REFERENCE);
45  		r.checkErrCode();
46  	}
47  
48  	/**
49  	 * 客服输入状态。
50  	 *
51  	 * <p>开发者可通过调用“客服输入状态接口”,返回客服当前输入状态给用户。</p>
52  	 *
53  	 * <ol>
54  	 *   <li>此接口需要客服消息接口权限。</li>
55  	 *   <li>如果不满足发送客服消息的触发条件,则无法下发输入状态。</li>
56  	 *   <li>下发输入状态,需要客服之前30秒内跟用户有过消息交互。</li>
57  	 *   <li>在输入状态中(持续15s),不可重复下发输入态。</li>
58  	 *   <li>在输入状态中,如果向用户下发消息,会同时取消输入状态。</li>
59  	 * </ol>
60  	 *
61  	 * @param toUser 普通用户(openid)
62  	 * @param command "Typing":对用户下发“正在输入"状态;"CancelTyping":取消对用户的”正在输入"状态
63  	 * @throws WeixinException indicates getting access token failed, or sending typing status failed.
64  	 * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/customer-message/typing.html">客服输入状态</a>
65  	 */
66  	public void typingCustomMessage(String toUser, Command command)
67  			throws WeixinException {
68  		final Map<String, String> params = CustomMessageAdapters.toMap(toUser, command);
69  		final WxaApiResult r = this.post("message_custom_typing",
70  			params, WxaApiResult.TYPE_REFERENCE);
71  		r.checkErrCode();
72  	}
73  
74  }