diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs index 436a60c8..7482d5d9 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs @@ -14,12 +14,12 @@ public class UserIdentity : IUserIdentity _contextAccessor = contextAccessor; } - public string Id => _claims.First(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value; + public string Id => _claims.First(x => x.Type == ClaimTypes.NameIdentifier).Value; - public string Email => _claims.First(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress").Value; + public string Email => _claims.First(x => x.Type == ClaimTypes.Email).Value; - public string FirstName => _claims.First(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname").Value; + public string FirstName => _claims.First(x => x.Type == ClaimTypes.GivenName).Value; - public string LastName => _claims.First(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname").Value; + public string LastName => _claims.First(x => x.Type == ClaimTypes.Surname).Value; } diff --git a/src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs b/src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs index 6a72f601..dc22732a 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs +++ b/src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs @@ -14,9 +14,11 @@ namespace BotSharp.Plugin.WeChat.Repository.DbTables [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 0c88fd56..65a7f600 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs +++ b/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs @@ -2,10 +2,16 @@ using BotSharp.Abstraction.Agents; using BotSharp.Abstraction.Conversations; using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Models; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Senparc.Weixin.Entities; using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; @@ -18,6 +24,7 @@ namespace BotSharp.Plugin.WeChat private readonly IServiceProvider _service; private readonly ILogger _logger; private string WeChatAppId => Senparc.Weixin.Config.SenparcWeixinSetting.WeixinAppId; + public static string AgentId { get; set; } public WeChatBackgroundService( IServiceProvider service, @@ -32,10 +39,23 @@ namespace BotSharp.Plugin.WeChat private async Task HandleTextMessageAsync(string openid, string message) { var scoped = _service.CreateScope().ServiceProvider; + var context = scoped.GetService(); + context.HttpContext = new DefaultHttpContext(); + context.HttpContext.User = new ClaimsPrincipal( + new ClaimsIdentity(new List() { new Claim(ClaimTypes.NameIdentifier, openid) })); + var conversationService = scoped.GetRequiredService(); - var result = await conversationService.SendMessage(WeChatAppId, openid, new RoleDialogModel + var latestConversationId = (await conversationService.GetConversations()).OrderByDescending(_ => _.CreatedTime).FirstOrDefault()?.Id; + + latestConversationId ??= (await conversationService.NewConversation(new Conversation() + { + UserId = openid, + AgentId = AgentId + }))?.Id; + + var result = await conversationService.SendMessage(AgentId, latestConversationId, new RoleDialogModel { Role = "user", Text = message, diff --git a/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs b/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs index 38c6592f..0739ebd5 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs @@ -17,6 +17,7 @@ using Senparc.Weixin.Entities; using Senparc.CO2NET.RegisterServices; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Http; +using BotSharp.Abstraction.Users; namespace BotSharp.Plugin.WeChat { @@ -33,9 +34,10 @@ namespace BotSharp.Plugin.WeChat { services = services.AddSenparcGlobalServices(config); } + WeChatBackgroundService.AgentId = config["WeChat:AgentId"]; services.AddSingleton(); - + services.AddHostedService(s => s.GetRequiredService()); services.TryAddSingleton(s => s.GetRequiredService()); diff --git a/src/Plugins/BotSharp.Plugin.WeChat/WeChatUserIdentity.cs b/src/Plugins/BotSharp.Plugin.WeChat/WeChatUserIdentity.cs new file mode 100644 index 00000000..a6edbf81 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.WeChat/WeChatUserIdentity.cs @@ -0,0 +1,19 @@ +using BotSharp.Abstraction.Users; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.Plugin.WeChat +{ + public class WeChatUserIdentity : IUserIdentity + { + + public string Id => throw new NotImplementedException(); + + public string Email => throw new NotImplementedException(); + + public string FirstName => throw new NotImplementedException(); + + public string LastName => throw new NotImplementedException(); + } +} diff --git a/src/WebStarter/Program.cs b/src/WebStarter/Program.cs index 8e5fc5ef..695a6121 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 75e0948a..70e8f700 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -62,7 +62,7 @@ }, "WeChat": { - "AgentId": "", + "AgentId": "437bed34-1169-4833-95ce-c24b8b56154a", "Token": "#{Token}#", "EncodingAESKey": "#{EncodingAESKey}#", "WeixinAppId": "#{WeixinAppId}#",