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   
9   /**
10   * 发送模板消息。
11   *
12   * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html">发送模板消息</a>
13   * @since 1.8
14   */
15  public class TemplateMessageApi extends TokenManagerApi {
16  
17  	public TemplateMessageApi(TokenManager tokenManager) {
18  		this(tokenManager, null);
19  	}
20  
21  	public TemplateMessageApi(TokenManager tokenManager, Properties properties) {
22  		super(tokenManager, properties);
23  	}
24  
25  	/**
26  	 * 发送模板消息
27  	 *
28  	 * @param toUser 接收者(用户)的 openid。
29  	 * @param templateId 所需下发的模板消息的 id。
30  	 * @param page 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
31  	 * @param formId 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id。
32  	 * @param data 模板内容,不填则下发空模板。
33  	 * @param emphasisKeyword 模板需要放大的关键词,不填则默认无放大。
34  	 * @throws WeixinException indicates getting access token failed, or sending template message failed.
35  	 * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/template-message/templateMessage.send.html">发送模板消息</a>
36  	 */
37  	public void sendTemplateMessage(
38  		final String toUser,
39  		final String templateId,
40  		final String page,
41  		final String formId,
42  		final Map<String, String> data,
43  		final String emphasisKeyword
44  	) throws WeixinException {
45  		final TemplateMessageParameter message = new TemplateMessageParameter(
46  			toUser, templateId, page, formId, data, emphasisKeyword
47  		);
48  		final WxaApiResult r = this.post(
49  			"wxopen_template_message_send",
50  			message,
51  			WxaApiResult.TYPE_REFERENCE
52  		);
53  		r.checkErrCode();
54  	}
55  
56  }