MpApiEnlightener.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Senparc.NeuChar;
  6. using Senparc.NeuChar.ApiHandlers;
  7. using Senparc.NeuChar.Entities;
  8. using Senparc.Weixin.Exceptions;
  9. using Senparc.Weixin.MP.AdvancedAPIs.GroupMessage;
  10. namespace Senparc.Weixin.MP.AdvancedAPIs
  11. {
  12. public class MpApiEnlightener : ApiEnlightener
  13. {
  14. public static ApiEnlightener Instance = new MpApiEnlightener();
  15. public override NeuChar.PlatformType PlatformType { get; set; } = NeuChar.PlatformType.WeChat_OfficialAccount;
  16. /// <summary>
  17. /// 发送文本客服消息
  18. /// </summary>
  19. /// <param name="accessTokenOrAppId"></param>
  20. /// <param name="openId"></param>
  21. /// <param name="content"></param>
  22. /// <returns></returns>
  23. public override ApiResult SendText(string accessTokenOrAppId, string openId, string content)
  24. {
  25. try
  26. {
  27. var result = AdvancedAPIs.CustomApi.SendText(accessTokenOrAppId, openId, content);
  28. return new ApiResult((int)result.errcode, result.errmsg, result);
  29. }
  30. catch (ErrorJsonResultException ex)
  31. {
  32. return new ApiResult(ex.JsonResult.ErrorCodeValue, ex.JsonResult.errmsg, ex.JsonResult);
  33. }
  34. }
  35. /// <summary>
  36. /// 发送图片客服消息
  37. /// </summary>
  38. /// <param name="accessTokenOrAppId"></param>
  39. /// <param name="openId"></param>
  40. /// <param name="mediaId"></param>
  41. /// <returns></returns>
  42. public override ApiResult SendImage(string accessTokenOrAppId, string openId, string mediaId)
  43. {
  44. try
  45. {
  46. var result = AdvancedAPIs.CustomApi.SendImage(accessTokenOrAppId, openId, mediaId);
  47. return new ApiResult((int)result.errcode, result.errmsg, result);
  48. }
  49. catch (ErrorJsonResultException ex)
  50. {
  51. return new ApiResult(ex.JsonResult.ErrorCodeValue, ex.JsonResult.errmsg, ex.JsonResult);
  52. }
  53. }
  54. public override ApiResult SendNews(string accessTokenOrAppId, string openId, List<Article> articleList)
  55. {
  56. try
  57. {
  58. var news = articleList.Select(z => new NewsModel()
  59. {
  60. title = z.Title,
  61. content = "点击【阅读原文】访问",//内容暂时无法获取到
  62. digest = z.Description,
  63. content_source_url = z.Url,
  64. thumb_url = z.PicUrl
  65. }).ToArray();
  66. //上传临时素材
  67. var newsResult = AdvancedAPIs.MediaApi.UploadTemporaryNews(accessTokenOrAppId, news: news);
  68. var result = AdvancedAPIs.CustomApi.SendMpNews(accessTokenOrAppId, openId, newsResult.media_id);
  69. return new ApiResult((int)result.errcode, result.errmsg, result);
  70. }
  71. catch (ErrorJsonResultException ex)
  72. {
  73. return new ApiResult(ex.JsonResult.ErrorCodeValue, ex.JsonResult.errmsg, ex.JsonResult);
  74. }
  75. }
  76. }
  77. }