SemanticApi.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. 文件名:SemanticApi.cs
  17. 文件功能描述:语意理解接口
  18. 创建标识:Senparc - 20150211
  19. 修改标识:Senparc - 20150303
  20. 修改描述:整理接口
  21. 修改标识:Senparc - 20150312
  22. 修改描述:开放代理请求超时时间
  23. 修改标识:Senparc - 20170707
  24. 修改描述:v14.5.1 完善异步方法async/await
  25. ----------------------------------------------------------------*/
  26. /*
  27. API:http://mp.weixin.qq.com/wiki/0/0ce78b3c9524811fee34aba3e33f3448.html
  28. 文档下载:http://mp.weixin.qq.com/wiki/static/assets/f48efdb46b4bca35caed4f01ca92e7da.zip
  29. */
  30. using System.Threading.Tasks;
  31. using Senparc.NeuChar;
  32. using Senparc.Weixin.CommonAPIs;
  33. using Senparc.Weixin.Entities;
  34. using Senparc.Weixin.MP.AdvancedAPIs.Semantic;
  35. using Senparc.Weixin.MP.CommonAPIs;
  36. namespace Senparc.Weixin.MP.AdvancedAPIs
  37. {
  38. /// <summary>
  39. /// 语意理解接口
  40. /// </summary>
  41. public static class SemanticApi
  42. {
  43. #region 同步方法
  44. /// <summary>
  45. /// 发送语义理解请求
  46. /// </summary>
  47. /// <typeparam name="T">语意理解返回的结果类型,在 AdvancedAPIs/Semantic/SemanticResult </typeparam>
  48. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  49. /// <param name="semanticPostData">语义理解请求需要post的数据</param>
  50. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  51. /// <returns></returns>
  52. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "SemanticApi.SemanticSend", true)]
  53. public static T SemanticSend<T>(string accessTokenOrAppId, SemanticPostData semanticPostData, int timeOut = Config.TIME_OUT) where T : WxJsonResult
  54. {
  55. return ApiHandlerWapper.TryCommonApi(accessToken =>
  56. {
  57. var urlFormat = Config.ApiMpHost + "/semantic/semproxy/search?access_token={0}";
  58. //switch (semanticPostData.category)
  59. //{
  60. // case "restaurant":
  61. // BaseSemanticResultJson as Semantic_RestaurantResult;
  62. //}
  63. return CommonJsonSend.Send<T>(accessToken, urlFormat, semanticPostData, timeOut: timeOut);
  64. }, accessTokenOrAppId);
  65. }
  66. #endregion
  67. #if !NET35 && !NET40
  68. #region 异步方法
  69. /// <summary>
  70. /// 【异步方法】发送语义理解请求
  71. /// </summary>
  72. /// <typeparam name="T">语意理解返回的结果类型,在 AdvancedAPIs/Semantic/SemanticResult </typeparam>
  73. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  74. /// <param name="semanticPostData">语义理解请求需要post的数据</param>
  75. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  76. /// <returns></returns>
  77. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "SemanticApi.SemanticSendAsync", true)]
  78. public static async Task<T> SemanticSendAsync<T>(string accessTokenOrAppId, SemanticPostData semanticPostData, int timeOut = Config.TIME_OUT) where T : WxJsonResult
  79. {
  80. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  81. {
  82. var urlFormat = Config.ApiMpHost + "/semantic/semproxy/search?access_token={0}";
  83. //switch (semanticPostData.category)
  84. //{
  85. // case "restaurant":
  86. // BaseSemanticResultJson as Semantic_RestaurantResult;
  87. //}
  88. return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<T>(accessToken, urlFormat, semanticPostData, timeOut: timeOut);
  89. }, accessTokenOrAppId);
  90. }
  91. #endregion
  92. #endif
  93. }
  94. }