Skip to content

Commit

Permalink
Merge pull request #96 from EasyAbp/new-template-message-api
Browse files Browse the repository at this point in the history
Support new official account template message API
  • Loading branch information
gdlcf88 authored Aug 14, 2023
2 parents a8ccefc + 605ce40 commit 35b265f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using System.Collections.Generic;
using EasyAbp.Abp.WeChat.Official.Models;
using Newtonsoft.Json;

Expand All @@ -10,23 +11,31 @@ namespace EasyAbp.Abp.WeChat.Official.Services.TemplateMessage.Request
public class CreateTemplateRequest : OfficialCommonRequest
{
/// <summary>
/// 模板库中模板的编号,有 "TM**" 和 "OPENTMTM**" 等形式
/// 模板库中模板的编号,有TM**”和“OPENTMTM**等形式,对于类目模板,为纯数字ID
/// </summary>
[JsonPropertyName("template_id_short")]
[JsonProperty("template_id_short")]
public string TemplateShortId { get; protected set; }

/// <summary>
/// 选用的类目模板的关键词,按顺序传入,如果为空,或者关键词不在模板库中,会返回40246错误码
/// </summary>
[JsonProperty("keyword_name_list")]
public List<string> KeywordNameList { get; protected set; }

protected CreateTemplateRequest()
{
}

/// <summary>
/// 构建一个新的 <see cref="CreateTemplateRequest"/> 对象。
/// </summary>
/// <param name="templateShortId">模板库中模板的编号,有 "TM**" 和 "OPENTMTM**" 等形式。</param>
public CreateTemplateRequest(string templateShortId)
/// <param name="templateShortId">模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式,对于类目模板,为纯数字ID</param>
/// <param name="keywordNameList">选用的类目模板的关键词,按顺序传入,如果为空,或者关键词不在模板库中,会返回40246错误码</param>
public CreateTemplateRequest(string templateShortId, List<string> keywordNameList)
{
TemplateShortId = templateShortId;
KeywordNameList = keywordNameList;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.Official.Models;
Expand Down Expand Up @@ -25,7 +26,8 @@ public class TemplateMessageWeService : OfficialAbpWeChatServiceBase
private const string DeletePrivateTemplateUrl =
"https://api.weixin.qq.com/cgi-bin/template/del_private_template?";

public TemplateMessageWeService(AbpWeChatOfficialOptions options, IAbpLazyServiceProvider lazyServiceProvider) : base(options, lazyServiceProvider)
public TemplateMessageWeService(AbpWeChatOfficialOptions options, IAbpLazyServiceProvider lazyServiceProvider) :
base(options, lazyServiceProvider)
{
}

Expand Down Expand Up @@ -112,13 +114,15 @@ public virtual Task<GetIndustryResponse> GetIndustryAsync()
/// <summary>
/// 根据短模版 Id 创建模版。
/// </summary>
/// <param name="templateShortId">模板库中模板的编号,有 "TM**" 和 "OPENTMTM**" 等形式。</param>
public virtual Task<CreateTemplateResponse> CreateTemplateAsync(string templateShortId)
/// <param name="templateShortId">模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式,对于类目模板,为纯数字ID</param>
/// <param name="keywordNameList">选用的类目模板的关键词,按顺序传入,如果为空,或者关键词不在模板库中,会返回40246错误码</param>
public virtual Task<CreateTemplateResponse> CreateTemplateAsync(string templateShortId,
List<string> keywordNameList)
{
return ApiRequester.RequestAsync<CreateTemplateResponse>(
GetTemplateIdUrl,
HttpMethod.Post,
new CreateTemplateRequest(templateShortId),
new CreateTemplateRequest(templateShortId, keywordNameList),
Options);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using Shouldly;
Expand Down Expand Up @@ -57,7 +58,8 @@ public async Task Should_Create_A_Template_And_Return_TemplateId()
{
var templateMessageService = await WeChatServiceFactory.CreateAsync<TemplateMessageWeService>();

var response = await templateMessageService.CreateTemplateAsync("OPENTM206482867");
var response = await templateMessageService.CreateTemplateAsync("47123",
new List<string> { "时间", "地点", "金额" });

response.ErrorCode.ShouldBe(0);
response.TemplateId.ShouldNotBeNullOrEmpty();
Expand All @@ -78,7 +80,9 @@ public async Task Should_Delete_Template_By_Id()
{
var templateMessageService = await WeChatServiceFactory.CreateAsync<TemplateMessageWeService>();

var createdTemplateResponse = await templateMessageService.CreateTemplateAsync("OPENTM206482867");
var createdTemplateResponse = await templateMessageService.CreateTemplateAsync("47123",
new List<string> { "时间", "地点", "金额" });

var deletedTemplateResponse =
await templateMessageService.DeleteTemplateAsync(createdTemplateResponse.TemplateId);

Expand Down

0 comments on commit 35b265f

Please sign in to comment.