add wechat identity

This commit is contained in:
xbotter 2023-07-03 09:02:33 +08:00
parent 21e7c53904
commit aa68256fb4
No known key found for this signature in database
GPG key ID: A3F32F44E9F160E1
7 changed files with 52 additions and 9 deletions

View file

@ -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;
}

View file

@ -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; }

View file

@ -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<WeChatBackgroundService> _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<IHttpContextAccessor>();
context.HttpContext = new DefaultHttpContext();
context.HttpContext.User = new ClaimsPrincipal(
new ClaimsIdentity(new List<Claim>() { new Claim(ClaimTypes.NameIdentifier, openid) }));
var conversationService = scoped.GetRequiredService<IConversationService>();
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,

View file

@ -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,6 +34,7 @@ namespace BotSharp.Plugin.WeChat
{
services = services.AddSenparcGlobalServices(config);
}
WeChatBackgroundService.AgentId = config["WeChat:AgentId"];
services.AddSingleton<WeChatBackgroundService>();

View file

@ -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();
}
}

View file

@ -57,8 +57,8 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI();
}
//app.UseAuthentication();
//app.UseAuthorization();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();

View file

@ -62,7 +62,7 @@
},
"WeChat": {
"AgentId": "",
"AgentId": "437bed34-1169-4833-95ce-c24b8b56154a",
"Token": "#{Token}#",
"EncodingAESKey": "#{EncodingAESKey}#",
"WeixinAppId": "#{WeixinAppId}#",