Skip to content

Commit

Permalink
feat(openai): 新增 v2 版机器人配置相关接口
Browse files Browse the repository at this point in the history
  • Loading branch information
fudiwei committed Jun 5, 2024
1 parent a92a602 commit ea25c0b
Show file tree
Hide file tree
Showing 25 changed files with 546 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Flurl.Http;

namespace SKIT.FlurlHttpClient.Wechat.OpenAI
{
public static class WechatOpenAIClientExecuteAsyncExtensions
{
/// <summary>
/// <para>异步调用 [POST] /v2/async/fetch 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developers.weixin.qq.com/doc/aispeech/confapi/dialog/bot/fetch.html ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.AsyncFetchV2Response> ExecuteAsyncFetchV2Async(this WechatOpenAIClient client, Models.AsyncFetchV2Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "v2", "async", "fetch");

return await client.SendFlurlRequestAsJsonAsync<Models.AsyncFetchV2Response>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Flurl.Http;

namespace SKIT.FlurlHttpClient.Wechat.OpenAI
{
public static class WechatOpenAIClientExecuteBotExtensions
{
/// <summary>
/// <para>异步调用 [POST] /v2/bot/import/json 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developers.weixin.qq.com/doc/aispeech/confapi/dialog/bot/import.html ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.BotImportJsonV2Response> ExecuteBotImportJsonV2Async(this WechatOpenAIClient client, Models.BotImportJsonV2Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "v2", "bot", "import", "json");

return await client.SendFlurlRequestAsJsonAsync<Models.BotImportJsonV2Response>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// <para>异步调用 [POST] /v2/bot/publish 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developers.weixin.qq.com/doc/aispeech/confapi/dialog/bot/publish.html ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.BotPublishV2Response> ExecuteBotPublishV2Async(this WechatOpenAIClient client, Models.BotPublishV2Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "v2", "bot", "publish");

return await client.SendFlurlRequestAsJsonAsync<Models.BotPublishV2Response>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// <para>异步调用 [POST] /v2/bot/effective_progress 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developers.weixin.qq.com/doc/aispeech/confapi/dialog/bot/progress.html ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.BotEffectiveProgressV2Response> ExecuteBotEffectiveProgressV2Async(this WechatOpenAIClient client, Models.BotEffectiveProgressV2Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "v2", "bot", "effective_progress");

return await client.SendFlurlRequestAsJsonAsync<Models.BotEffectiveProgressV2Response>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ public WechatOpenAIRequestEncryptionInterceptor(string baseUrl, string encodingA
_baseUrl = baseUrl;
_encodingAESKey = encodingAESKey;
_customEncryptedRequestPathMatcher = customEncryptedRequestPathMatcher;

// AES 密钥的长度不是 4 的倍数需要补齐,确保其始终为有效的 Base64 字符串
const int MULTI = 4;
int tLen = _encodingAESKey.Length;
int tRem = tLen % MULTI;
if (tRem > 0)
{
_encodingAESKey = _encodingAESKey.PadRight(tLen - tRem + MULTI, '=');
}
}

public override async Task BeforeCallAsync(HttpInterceptorContext context, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -83,7 +74,7 @@ public override async Task BeforeCallAsync(HttpInterceptorContext context, Cance
}

context.FlurlCall.HttpRequestMessage!.Content?.Dispose();
context.FlurlCall.HttpRequestMessage!.Content = new ByteArrayContent(reqBytesEncrypted);
context.FlurlCall.HttpRequestMessage!.Content = new StringContent(Convert.ToBase64String(reqBytesEncrypted));
context.FlurlCall.Request.WithHeader(HttpHeaders.ContentType, MimeTypes.Text);
}

Expand Down Expand Up @@ -150,9 +141,10 @@ private bool IsRequestUrlPathMatched(Uri uri)
{
if (_customEncryptedRequestPathMatcher is not null)
return _customEncryptedRequestPathMatcher(relativeUrl);
return false;
}

return true;
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
{
/// <summary>
/// <para>表示 [POST] /v2/async/fetch 接口的请求。</para>
/// </summary>
public class AsyncFetchV2Request : WechatOpenAIRequest
{
/// <summary>
/// 获取或设置任务 ID。
/// </summary>
[Newtonsoft.Json.JsonProperty("task_id")]
[System.Text.Json.Serialization.JsonPropertyName("task_id")]
public string TaskId { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
{
/// <summary>
/// <para>表示 [POST] /v2/async/fetch 接口的响应。</para>
/// </summary>
public class AsyncFetchV2Response : WechatOpenAIResponse<AsyncFetchV2Response.Types.Data>
{
public static class Types
{
public class Data : WechatOpenAIResponseData
{
public static class Types
{
public class Intent
{
/// <summary>
/// 获取或设置意图 ID。
/// </summary>
[Newtonsoft.Json.JsonProperty("id")]
[System.Text.Json.Serialization.JsonPropertyName("id")]
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.Common.TextualNumberReadOnlyConverter))]
public long IntentId { get; set; }

/// <summary>
/// 获取或设置意图名称。
/// </summary>
[Newtonsoft.Json.JsonProperty("name")]
[System.Text.Json.Serialization.JsonPropertyName("name")]
public string Name { get; set; } = default!;
}

public class FailedSkill
{
/// <summary>
/// 获取或设置技能名称。
/// </summary>
[Newtonsoft.Json.JsonProperty("name")]
[System.Text.Json.Serialization.JsonPropertyName("name")]
public string Name { get; set; } = default!;
}

public class SucceededSkill : FailedSkill
{
/// <summary>
/// 获取或设置技能 ID。
/// </summary>
[Newtonsoft.Json.JsonProperty("id")]
[System.Text.Json.Serialization.JsonPropertyName("id")]
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.Common.TextualNumberReadOnlyConverter))]
public long SkillId { get; set; }

/// <summary>
/// 获取或设置意图列表。
/// </summary>
[Newtonsoft.Json.JsonProperty("intents")]
[System.Text.Json.Serialization.JsonPropertyName("intents")]
public Types.Intent[] IntentList { get; set; } = default!;
}
}

/// <summary>
/// 获取或设置任务状态。
/// </summary>
[Newtonsoft.Json.JsonProperty("state")]
[System.Text.Json.Serialization.JsonPropertyName("state")]
public int State { get; set; }

/// <summary>
/// 获取或设置任务进度(单位:百分数)。
/// </summary>
[Newtonsoft.Json.JsonProperty("progress")]
[System.Text.Json.Serialization.JsonPropertyName("progress")]
public int Progress { get; set; }

/// <summary>
/// 获取或设置任务开始时间戳。
/// </summary>
[Newtonsoft.Json.JsonProperty("start")]
[System.Text.Json.Serialization.JsonPropertyName("start")]
public long StartTimestamp { get; set; }

/// <summary>
/// 获取或设置任务结束时间戳。
/// </summary>
[Newtonsoft.Json.JsonProperty("end")]
[System.Text.Json.Serialization.JsonPropertyName("end")]
public long EndTimestamp { get; set; }

/// <summary>
/// 获取或设置导出任务的 URL。
/// </summary>
[Newtonsoft.Json.JsonProperty("url")]
[System.Text.Json.Serialization.JsonPropertyName("url")]
public string? Url { get; set; }

/// <summary>
/// 获取或设置导入的总数量。
/// </summary>
[Newtonsoft.Json.JsonProperty("total_count")]
[System.Text.Json.Serialization.JsonPropertyName("total_count")]
public int TotalCount { get; set; }

/// <summary>
/// 获取或设置导入失败的数量。
/// </summary>
[Newtonsoft.Json.JsonProperty("fail_count")]
[System.Text.Json.Serialization.JsonPropertyName("fail_count")]
public int FailedCount { get; set; }

/// <summary>
/// 获取或设置导入失败的技能列表。
/// </summary>
[Newtonsoft.Json.JsonProperty("fail_skill_info")]
[System.Text.Json.Serialization.JsonPropertyName("fail_skill_info")]
public Types.FailedSkill[]? FailedSkillList { get; set; }

/// <summary>
/// 获取或设置导入成功的数量。
/// </summary>
[Newtonsoft.Json.JsonProperty("success_count")]
[System.Text.Json.Serialization.JsonPropertyName("success_count")]
public int SucceededCount { get; set; }

/// <summary>
/// 获取或设置导入成功的技能列表。
/// </summary>
[Newtonsoft.Json.JsonProperty("success_skill_info")]
[System.Text.Json.Serialization.JsonPropertyName("success_skill_info")]
public Types.SucceededSkill[]? SucceededSkillList { get; set; }
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
{
/// <summary>
/// <para>表示 [POST] /v2/bot/effective_progress 接口的请求。</para>
/// </summary>
public class BotEffectiveProgressV2Request : WechatOpenAIRequest
{
/// <summary>
/// 获取或设置查询环境。
/// <para>默认值:"online"</para>
/// </summary>
[Newtonsoft.Json.JsonProperty("env")]
[System.Text.Json.Serialization.JsonPropertyName("env")]
public string Environment { get; set; } = "online";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
{
/// <summary>
/// <para>表示 [POST] /v2/bot/effective_progress 接口的响应。</para>
/// </summary>
public class BotEffectiveProgressV2Response : WechatOpenAIResponse<BotEffectiveProgressV2Response.Types.Data>
{
public static class Types
{
public class Data : WechatOpenAIResponseData
{
/// <summary>
/// 获取或设置任务状态。
/// </summary>
[Newtonsoft.Json.JsonProperty("status")]
[System.Text.Json.Serialization.JsonPropertyName("status")]
public int Status { get; set; }

/// <summary>
/// 获取或设置任务进度(单位:百分数)。
/// </summary>
[Newtonsoft.Json.JsonProperty("progress")]
[System.Text.Json.Serialization.JsonPropertyName("progress")]
public int Progress { get; set; }

/// <summary>
/// 获取或设置任务结束时间字符串。
/// </summary>
[Newtonsoft.Json.JsonProperty("end_time")]
[System.Text.Json.Serialization.JsonPropertyName("end_time")]
public string? EndTimeString { get; set; }
}
}
}
}
Loading

0 comments on commit ea25c0b

Please sign in to comment.