From 21e7c53904e37694eadda1a2ea07512fd5c24b94 Mon Sep 17 00:00:00 2001 From: xbotter Date: Thu, 29 Jun 2023 19:40:01 +0800 Subject: [PATCH] draft wechat plugin users --- .../BotSharp.Plugin.WeChat.csproj | 1 + .../Repository/DbTables/WeChatAccount.cs | 24 +++++++++++++++++++ .../WeChatBackgroundService.cs | 8 ++++--- src/WebStarter/Program.cs | 4 ++-- src/WebStarter/appsettings.json | 1 + 5 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs diff --git a/src/Plugins/BotSharp.Plugin.WeChat/BotSharp.Plugin.WeChat.csproj b/src/Plugins/BotSharp.Plugin.WeChat/BotSharp.Plugin.WeChat.csproj index 611146dd..f8ab250e 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/BotSharp.Plugin.WeChat.csproj +++ b/src/Plugins/BotSharp.Plugin.WeChat/BotSharp.Plugin.WeChat.csproj @@ -21,6 +21,7 @@ + diff --git a/src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs b/src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs new file mode 100644 index 00000000..6a72f601 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs @@ -0,0 +1,24 @@ +using BotSharp.Core.Repository.Abstraction; +using EntityFrameworkCore.BootKit; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Text; + +namespace BotSharp.Plugin.WeChat.Repository.DbTables +{ + [Table("WeChatAccount")] + public class WeChatAccount : DbRecord, IAgentTable + { + [Required] + [MaxLength(18)] + public string WeChatAppId { get; set; } + [Required] + [MaxLength(36)] + public string WeChatOpenId { get; set; } + [Required] + [MaxLength(36)] + public string UserId { get; set; } + } +} diff --git a/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs b/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs index d1ed3109..0c88fd56 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs +++ b/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Agents; using BotSharp.Abstraction.Conversations; using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Models; @@ -16,6 +17,7 @@ namespace BotSharp.Plugin.WeChat private readonly Channel _queue; private readonly IServiceProvider _service; private readonly ILogger _logger; + private string WeChatAppId => Senparc.Weixin.Config.SenparcWeixinSetting.WeixinAppId; public WeChatBackgroundService( IServiceProvider service, @@ -30,9 +32,10 @@ namespace BotSharp.Plugin.WeChat private async Task HandleTextMessageAsync(string openid, string message) { var scoped = _service.CreateScope().ServiceProvider; + var conversationService = scoped.GetRequiredService(); - var result = await conversationService.SendMessage(openid, Guid.Empty.ToString(), new RoleDialogModel + var result = await conversationService.SendMessage(WeChatAppId, openid, new RoleDialogModel { Role = "user", Text = message, @@ -43,8 +46,7 @@ namespace BotSharp.Plugin.WeChat private async Task ReplyTextMessageAsync(string openid, string content) { - var appId = Senparc.Weixin.Config.SenparcWeixinSetting.WeixinAppId; - await Senparc.Weixin.MP.AdvancedAPIs.CustomApi.SendTextAsync(appId, openid, content); + await Senparc.Weixin.MP.AdvancedAPIs.CustomApi.SendTextAsync(WeChatAppId, openid, content); } public async Task EnqueueAsync(WeChatMessage message) diff --git a/src/WebStarter/Program.cs b/src/WebStarter/Program.cs index 695a6121..8e5fc5ef 100644 --- a/src/WebStarter/Program.cs +++ b/src/WebStarter/Program.cs @@ -57,8 +57,8 @@ if (app.Environment.IsDevelopment()) app.UseSwaggerUI(); } -app.UseAuthentication(); -app.UseAuthorization(); +//app.UseAuthentication(); +//app.UseAuthorization(); app.MapControllers(); diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index 3a23b97b..75e0948a 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -62,6 +62,7 @@ }, "WeChat": { + "AgentId": "", "Token": "#{Token}#", "EncodingAESKey": "#{EncodingAESKey}#", "WeixinAppId": "#{WeixinAppId}#",