TemplateApi.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:TemplateAPI.cs
  17. 文件功能描述:模板消息接口
  18. 创建标识:Senparc - 20150211
  19. 修改标识:Senparc - 20150303
  20. 修改描述:整理接口
  21. 修改标识:Senparc - 20150312
  22. 修改描述:开放代理请求超时时间
  23. 修改标识:Senparc - 20160719
  24. 修改描述:增加其接口的异步方法
  25. 修改标识:Senparc - 20160808
  26. 修改描述:去掉SendTemplateMessage,SendTemplateMessageAsync中的topcolor参数
  27. 修改标识:Senparc - 20170707
  28. 修改描述:v14.5.1 完善异步方法async/await
  29. 修改标识:Senparc - 20170707
  30. 修改描述:v14.5.4 添加“一次性订阅消息”相关接口
  31. ----------------------------------------------------------------*/
  32. /*
  33. API:http://mp.weixin.qq.com/wiki/17/304c1885ea66dbedf7dc170d84999a9d.html
  34. */
  35. using System;
  36. using System.Collections.Generic;
  37. using System.Threading.Tasks;
  38. using Senparc.NeuChar;
  39. using Senparc.Weixin.CommonAPIs;
  40. using Senparc.Weixin.Entities;
  41. using Senparc.Weixin.Entities.TemplateMessage;
  42. using Senparc.Weixin.HttpUtility;
  43. using Senparc.Weixin.MP.AdvancedAPIs.TemplateMessage;
  44. using Senparc.Weixin.MP.CommonAPIs;
  45. namespace Senparc.Weixin.MP.AdvancedAPIs
  46. {
  47. /// <summary>
  48. /// 模板消息接口
  49. /// </summary>
  50. public static class TemplateApi
  51. {
  52. /// <summary>
  53. /// 获取URL:一次性订阅消息,第一步引导用户打开链接进行授权
  54. /// 文档地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1500374289_66bvB
  55. /// </summary>
  56. /// <param name="appId">公众号的唯一标识</param>
  57. /// <param name="scene">重定向后会带上scene参数,开发者可以填0-10000的整形值,用来标识订阅场景值</param>
  58. /// <param name="templateId">订阅消息模板ID,登录公众平台后台,在接口权限列表处可查看订阅模板ID</param>
  59. /// <param name="redirectUrl">授权后重定向的回调地址,请使用UrlEncode对链接进行处理。注:要求redirect_url的域名要跟登记的业务域名一致,且业务域名不能带路径</param>
  60. /// <param name="reserved">(非必填)用于保持请求和回调的状态,授权请后原样带回给第三方。该参数可用于防止csrf攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加session进行校验,开发者可以填写a-zA-Z0-9的参数值,最多128字节</param>
  61. /// <param name="action">直接填get_confirm即可,保留默认值</param>
  62. /// <returns></returns>
  63. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.GetSubscribeMsgUrl", true)]
  64. public static string GetSubscribeMsgUrl(string appId, int scene, string templateId, string redirectUrl, string reserved = null, string action = "get_confirm")
  65. {
  66. //无论直接打开还是做页面302重定向时,必须带#wechat_redirect参数
  67. return string.Format("https://mp.weixin.qq.com/mp/subscribemsg?action={0}&appid={1}&scene={2}&template_id={3}&redirect_url={4}&reserved={5}#wechat_redirect",
  68. action, appId, scene, templateId, redirectUrl, reserved);
  69. }
  70. #region 同步方法
  71. /// <summary>
  72. /// 模板消息接口
  73. /// </summary>
  74. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  75. /// <param name="openId">填接收消息的用户openid</param>
  76. /// <param name="templateId">订阅消息模板ID</param>
  77. /// <param name="url">(非必须)点击消息跳转的链接,需要有ICP备案</param>
  78. /// <param name="data">消息正文,value为消息内容文本(200字以内),没有固定格式,可用\n换行,color为整段消息内容的字体颜色(目前仅支持整段消息为一种颜色)</param>
  79. /// <param name="miniProgram">(非必须)跳小程序所需数据,不需跳小程序可不用传该数据</param>
  80. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  81. /// <returns></returns>
  82. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.SendTemplateMessage", true)]
  83. public static SendTemplateMessageResult SendTemplateMessage(string accessTokenOrAppId, string openId, string templateId, string url, object data, TempleteModel_MiniProgram miniProgram = null, int timeOut = Config.TIME_OUT)
  84. {
  85. //文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1500374289_66bvB
  86. return ApiHandlerWapper.TryCommonApi(accessToken =>
  87. {
  88. string urlFormat = Config.ApiMpHost + "/cgi-bin/message/template/send?access_token={0}";
  89. var msgData = new TempleteModel()
  90. {
  91. touser = openId,
  92. template_id = templateId,
  93. // topcolor = topcolor,
  94. url = url,
  95. miniprogram = miniProgram,
  96. data = data,
  97. };
  98. return CommonJsonSend.Send<SendTemplateMessageResult>(accessToken, urlFormat, msgData, timeOut: timeOut);
  99. }, accessTokenOrAppId);
  100. }
  101. /// <summary>
  102. /// 模板消息接口
  103. /// </summary>
  104. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  105. /// <param name="openId"></param>
  106. /// <param name="templateMessageData"></param>
  107. /// <param name="miniProgram">跳小程序所需数据,不需跳小程序可不用传该数据</param>
  108. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  109. /// <returns></returns>
  110. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.SendTemplateMessage", true)]
  111. public static SendTemplateMessageResult SendTemplateMessage(string accessTokenOrAppId, string openId, ITemplateMessageBase templateMessageData, TempleteModel_MiniProgram miniProgram = null, int timeOut = Config.TIME_OUT)
  112. {
  113. return SendTemplateMessage(accessTokenOrAppId, openId, templateMessageData.TemplateId,
  114. templateMessageData.Url, templateMessageData, miniProgram, timeOut);
  115. }
  116. /// <summary>
  117. /// 设置所属行业
  118. /// </summary>
  119. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  120. /// <param name="industry_id1">公众号模板消息所属行业编号</param>
  121. /// <param name="industry_id2">公众号模板消息所属行业编号</param>
  122. /// <param name="timeOut"></param>
  123. /// <returns></returns>
  124. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.SetIndustry", true)]
  125. public static WxJsonResult SetIndustry(string accessTokenOrAppId, IndustryCode industry_id1, IndustryCode industry_id2, int timeOut = Config.TIME_OUT)
  126. {
  127. return ApiHandlerWapper.TryCommonApi(accessToken =>
  128. {
  129. string urlFormat = Config.ApiMpHost + "/cgi-bin/template/api_set_industry?access_token={0}";
  130. var msgData = new
  131. {
  132. industry_id1 = ((int)industry_id1).ToString(),
  133. industry_id2 = ((int)industry_id2).ToString()
  134. };
  135. return CommonJsonSend.Send<WxJsonResult>(accessToken, urlFormat, msgData, timeOut: timeOut);
  136. }, accessTokenOrAppId);
  137. }
  138. /// <summary>
  139. /// 获取设置的行业信息
  140. /// </summary>
  141. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  142. /// <param name="timeOut"></param>
  143. /// <returns></returns>
  144. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.GetIndustry", true)]
  145. public static GetIndustryJsonResult GetIndustry(string accessTokenOrAppId, int timeOut = Config.TIME_OUT)
  146. {
  147. return ApiHandlerWapper.TryCommonApi(accessToken =>
  148. {
  149. string urlFormat = Config.ApiMpHost + "/cgi-bin/template/get_industry?access_token={0}";
  150. return CommonJsonSend.Send<GetIndustryJsonResult>(accessToken, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  151. }, accessTokenOrAppId);
  152. }
  153. /// <summary>
  154. /// 添加模板并获得模板ID
  155. /// </summary>
  156. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  157. /// <param name="template_id_short">模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式</param>
  158. /// <param name="timeOut"></param>
  159. /// <returns></returns>
  160. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.Addtemplate", true)]
  161. public static AddtemplateJsonResult Addtemplate(string accessTokenOrAppId, string template_id_short, int timeOut = Config.TIME_OUT)
  162. {
  163. return ApiHandlerWapper.TryCommonApi(accessToken =>
  164. {
  165. string urlFormat = Config.ApiMpHost + "/cgi-bin/template/api_add_template?access_token={0}";
  166. var msgData = new
  167. {
  168. template_id_short = template_id_short
  169. };
  170. return CommonJsonSend.Send<AddtemplateJsonResult>(accessToken, urlFormat, msgData, CommonJsonSendType.POST, timeOut: timeOut);
  171. }, accessTokenOrAppId);
  172. }
  173. /// <summary>
  174. /// 获取模板列表
  175. /// </summary>
  176. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  177. /// <param name="timeOut"></param>
  178. /// <returns></returns>
  179. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.GetPrivateTemplate", true)]
  180. public static GetPrivateTemplateJsonResult GetPrivateTemplate(string accessTokenOrAppId, int timeOut = Config.TIME_OUT)
  181. {
  182. return ApiHandlerWapper.TryCommonApi(accessToken =>
  183. {
  184. string urlFormat = Config.ApiMpHost + "/cgi-bin/template/get_all_private_template?access_token={0}";
  185. return CommonJsonSend.Send<GetPrivateTemplateJsonResult>(accessToken, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  186. }, accessTokenOrAppId);
  187. }
  188. /// <summary>
  189. /// 删除模板
  190. /// </summary>
  191. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  192. /// <param name="template_id">公众帐号下模板消息ID</param>
  193. /// <param name="timeOut"></param>
  194. /// <returns></returns>
  195. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.DelPrivateTemplate", true)]
  196. public static WxJsonResult DelPrivateTemplate(string accessTokenOrAppId, string template_id, int timeOut = Config.TIME_OUT)
  197. {
  198. return ApiHandlerWapper.TryCommonApi(accessToken =>
  199. {
  200. string urlFormat = Config.ApiMpHost + "/cgi-bin/template/del_private_template?access_token={0}";
  201. var msgData = new
  202. {
  203. template_id = template_id
  204. };
  205. return CommonJsonSend.Send<WxJsonResult>(accessToken, urlFormat, msgData, CommonJsonSendType.POST, timeOut: timeOut);
  206. }, accessTokenOrAppId);
  207. }
  208. /// <summary>
  209. /// 通过API推送订阅模板消息给到授权微信用户
  210. /// </summary>
  211. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  212. /// <param name="toUserOpenId">填接收消息的用户openid</param>
  213. /// <param name="templateId">订阅消息模板ID</param>
  214. /// <param name="scene">订阅场景值</param>
  215. /// <param name="title">消息标题,15字以内</param>
  216. /// <param name="data">消息正文,value为消息内容,color为颜色,200字以内</param>
  217. /// <param name="url">点击消息跳转的链接,需要有ICP备案</param>
  218. /// <param name="timeOut"></param>
  219. /// <returns></returns>
  220. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.Subscribe", true)]
  221. public static WxJsonResult Subscribe(string accessTokenOrAppId, string toUserOpenId, string templateId, string scene, string title, object data, string url = null, int timeOut = Config.TIME_OUT)
  222. {
  223. return ApiHandlerWapper.TryCommonApi(accessToken =>
  224. {
  225. string urlFormat = Config.ApiMpHost + "/cgi-bin/message/template/subscribe?access_token={0}";
  226. var msgData = new SubscribeMsgTempleteModel()
  227. {
  228. touser = toUserOpenId,
  229. template_id = templateId,
  230. url = url,
  231. scene = scene,
  232. title = title,
  233. data = data
  234. };
  235. return CommonJsonSend.Send<WxJsonResult>(accessToken, urlFormat, msgData, CommonJsonSendType.POST, timeOut: timeOut);
  236. }, accessTokenOrAppId);
  237. }
  238. #endregion
  239. #if !NET35 && !NET40
  240. #region 异步方法
  241. /// <summary>
  242. /// 【异步方法】模板消息接口
  243. /// </summary>
  244. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  245. /// <param name="openId">填接收消息的用户openid</param>
  246. /// <param name="templateId">订阅消息模板ID</param>
  247. /// <param name="url">(非必须)点击消息跳转的链接,需要有ICP备案</param>
  248. /// <param name="data">消息正文,value为消息内容文本(200字以内),没有固定格式,可用\n换行,color为整段消息内容的字体颜色(目前仅支持整段消息为一种颜色)</param>
  249. /// <param name="miniProgram">(非必须)跳小程序所需数据,不需跳小程序可不用传该数据</param>
  250. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  251. /// <returns></returns> /// <returns></returns>
  252. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.SendTemplateMessageAsync", true)]
  253. public static async Task<SendTemplateMessageResult> SendTemplateMessageAsync(string accessTokenOrAppId, string openId, string templateId, string url, object data, TempleteModel_MiniProgram miniProgram = null, int timeOut = Config.TIME_OUT)
  254. {
  255. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  256. {
  257. string urlFormat = Config.ApiMpHost + "/cgi-bin/message/template/send?access_token={0}";
  258. var msgData = new TempleteModel()
  259. {
  260. touser = openId,
  261. template_id = templateId,
  262. // topcolor = topcolor,
  263. url = url,
  264. miniprogram = miniProgram,
  265. data = data,
  266. };
  267. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<SendTemplateMessageResult>(accessToken, urlFormat, msgData, timeOut: timeOut);
  268. }, accessTokenOrAppId);
  269. }
  270. /// <summary>
  271. /// 【异步方法】模板消息接口
  272. /// </summary>
  273. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  274. /// <param name="openId"></param>
  275. /// <param name="miniProgram">跳小程序所需数据,不需跳小程序可不用传该数据</param>
  276. /// <param name="templateMessageData"></param>
  277. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  278. /// <returns></returns>
  279. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.SendTemplateMessageAsync", true)]
  280. public static async Task<SendTemplateMessageResult> SendTemplateMessageAsync(string accessTokenOrAppId, string openId, ITemplateMessageBase templateMessageData, TempleteModel_MiniProgram miniProgram = null, int timeOut = Config.TIME_OUT)
  281. {
  282. return await SendTemplateMessageAsync(accessTokenOrAppId, openId, templateMessageData.TemplateId,
  283. templateMessageData.Url, templateMessageData, miniProgram, timeOut);
  284. }
  285. /// <summary>
  286. /// 【异步方法】设置所属行业
  287. /// </summary>
  288. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  289. /// <param name="industry_id1">公众号模板消息所属行业编号</param>
  290. /// <param name="industry_id2">公众号模板消息所属行业编号</param>
  291. /// <param name="timeOut"></param>
  292. /// <returns></returns>
  293. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.SetIndustryAsync", true)]
  294. public static async Task<WxJsonResult> SetIndustryAsync(string accessTokenOrAppId, IndustryCode industry_id1, IndustryCode industry_id2, int timeOut = Config.TIME_OUT)
  295. {
  296. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  297. {
  298. string urlFormat = Config.ApiMpHost + "/cgi-bin/template/api_set_industry?access_token={0}";
  299. var msgData = new
  300. {
  301. industry_id1 = ((int)industry_id1).ToString(),
  302. industry_id2 = ((int)industry_id2).ToString()
  303. };
  304. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<WxJsonResult>(accessToken, urlFormat, msgData, timeOut: timeOut);
  305. }, accessTokenOrAppId);
  306. }
  307. /// <summary>
  308. /// 【异步方法】获取设置的行业信息
  309. /// </summary>
  310. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  311. /// <param name="timeOut"></param>
  312. /// <returns></returns>
  313. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.GetIndustryAsync", true)]
  314. public static async Task<GetIndustryJsonResult> GetIndustryAsync(string accessTokenOrAppId, int timeOut = Config.TIME_OUT)
  315. {
  316. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  317. {
  318. string urlFormat = Config.ApiMpHost + "/cgi-bin/template/get_industry?access_token={0}";
  319. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<GetIndustryJsonResult>(accessToken, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  320. }, accessTokenOrAppId);
  321. }
  322. /// <summary>
  323. /// 【异步方法】获得模板ID
  324. /// </summary>
  325. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  326. /// <param name="template_id_short">模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式</param>
  327. /// <param name="timeOut"></param>
  328. /// <returns></returns>
  329. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.AddtemplateAsync", true)]
  330. public static async Task<AddtemplateJsonResult> AddtemplateAsync(string accessTokenOrAppId, string template_id_short, int timeOut = Config.TIME_OUT)
  331. {
  332. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  333. {
  334. string urlFormat = Config.ApiMpHost + "/cgi-bin/template/api_add_template?access_token={0}";
  335. var msgData = new
  336. {
  337. template_id_short = template_id_short
  338. };
  339. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<AddtemplateJsonResult>(accessToken, urlFormat, msgData, CommonJsonSendType.POST, timeOut: timeOut);
  340. }, accessTokenOrAppId);
  341. }
  342. /// <summary>
  343. ///【异步办法】 获取模板列表
  344. /// </summary>
  345. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  346. /// <param name="timeOut"></param>
  347. /// <returns></returns>
  348. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.GetPrivateTemplateAsync", true)]
  349. public static async Task<GetPrivateTemplateJsonResult> GetPrivateTemplateAsync(string accessTokenOrAppId, int timeOut = Config.TIME_OUT)
  350. {
  351. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  352. {
  353. string urlFormat = Config.ApiMpHost + "/cgi-bin/template/get_all_private_template?access_token={0}";
  354. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<GetPrivateTemplateJsonResult>(accessToken, urlFormat, null, CommonJsonSendType.GET, timeOut: timeOut);
  355. }, accessTokenOrAppId);
  356. }
  357. /// <summary>
  358. /// 【异步方法】删除模板
  359. /// </summary>
  360. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  361. /// <param name="template_id">公众帐号下模板消息ID</param>
  362. /// <param name="timeOut"></param>
  363. /// <returns></returns>
  364. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.DelPrivateTemplateAsync", true)]
  365. public static async Task<WxJsonResult> DelPrivateTemplateAsync(string accessTokenOrAppId, string template_id, int timeOut = Config.TIME_OUT)
  366. {
  367. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  368. {
  369. string urlFormat = Config.ApiMpHost + "/cgi-bin/template/del_private_template?access_token={0}";
  370. var msgData = new
  371. {
  372. template_id = template_id
  373. };
  374. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<WxJsonResult>(accessToken, urlFormat, msgData, CommonJsonSendType.POST, timeOut: timeOut);
  375. }, accessTokenOrAppId);
  376. }
  377. /// <summary>
  378. /// 【异步方法】通过API推送订阅模板消息给到授权微信用户
  379. /// </summary>
  380. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  381. /// <param name="toUserOpenId">填接收消息的用户openid</param>
  382. /// <param name="templateId">订阅消息模板ID</param>
  383. /// <param name="scene">订阅场景值</param>
  384. /// <param name="title">消息标题,15字以内</param>
  385. /// <param name="data">消息正文,value为消息内容,color为颜色,200字以内</param>
  386. /// <param name="url">点击消息跳转的链接,需要有ICP备案</param>
  387. /// <param name="timeOut"></param>
  388. /// <returns></returns>
  389. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "TemplateApi.SubscribeAsync", true)]
  390. public static async Task<WxJsonResult> SubscribeAsync(string accessTokenOrAppId, string toUserOpenId, string templateId, string scene, string title, object data, string url = null, int timeOut = Config.TIME_OUT)
  391. {
  392. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  393. {
  394. string urlFormat = Config.ApiMpHost + "/cgi-bin/message/template/subscribe?access_token={0}";
  395. var msgData = new SubscribeMsgTempleteModel()
  396. {
  397. touser = toUserOpenId,
  398. template_id = templateId,
  399. url = url,
  400. scene = scene,
  401. title = title,
  402. data = data
  403. };
  404. return await CommonJsonSend.SendAsync<WxJsonResult>(accessToken, urlFormat, msgData, CommonJsonSendType.POST, timeOut: timeOut);
  405. }, accessTokenOrAppId);
  406. }
  407. #endregion
  408. #endif
  409. }
  410. }