From 21e7c53904e37694eadda1a2ea07512fd5c24b94 Mon Sep 17 00:00:00 2001 From: xbotter Date: Thu, 29 Jun 2023 19:40:01 +0800 Subject: [PATCH 1/5] 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}#", From aa68256fb472cf4e91344e572c89f0f8fed81e66 Mon Sep 17 00:00:00 2001 From: xbotter Date: Mon, 3 Jul 2023 09:02:33 +0800 Subject: [PATCH 2/5] add wechat identity --- .../Users/Services/UserIdentity.cs | 8 +++---- .../Repository/DbTables/WeChatAccount.cs | 2 ++ .../WeChatBackgroundService.cs | 22 ++++++++++++++++++- .../BotSharp.Plugin.WeChat/WeChatPlugin.cs | 4 +++- .../WeChatUserIdentity.cs | 19 ++++++++++++++++ src/WebStarter/Program.cs | 4 ++-- src/WebStarter/appsettings.json | 2 +- 7 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 src/Plugins/BotSharp.Plugin.WeChat/WeChatUserIdentity.cs 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}#", From 21a1e69447341a280115cd306c4609728af494fb Mon Sep 17 00:00:00 2001 From: xbotter Date: Mon, 3 Jul 2023 10:57:09 +0800 Subject: [PATCH 3/5] update wechat account user bind --- .../Agents/Services/AgentService.GetAgents.cs | 2 +- .../Repository/DbTables/WeChatAccount.cs | 26 ------------ .../Users/IWeChatAccountUserService.cs | 11 +++++ .../Users/WeChatAccountUserService.cs | 31 ++++++++++++++ .../WeChatBackgroundService.cs | 40 +++++++++++++++---- .../BotSharp.Plugin.WeChat/WeChatPlugin.cs | 3 ++ .../WeChatUserIdentity.cs | 19 --------- 7 files changed, 79 insertions(+), 53 deletions(-) delete mode 100644 src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs create mode 100644 src/Plugins/BotSharp.Plugin.WeChat/Users/IWeChatAccountUserService.cs create mode 100644 src/Plugins/BotSharp.Plugin.WeChat/Users/WeChatAccountUserService.cs delete mode 100644 src/Plugins/BotSharp.Plugin.WeChat/WeChatUserIdentity.cs diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs index e50426d7..cdbdf95b 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -18,7 +18,7 @@ public partial class AgentService { var db = _services.GetRequiredService(); var query = from agent in db.Agent - where agent.OwnerId == _user.Id && agent.Id == id + where agent.Id == id select agent.ToAgent(); var profile = query.FirstOrDefault(); diff --git a/src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs b/src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs deleted file mode 100644 index dc22732a..00000000 --- a/src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs +++ /dev/null @@ -1,26 +0,0 @@ -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/Users/IWeChatAccountUserService.cs b/src/Plugins/BotSharp.Plugin.WeChat/Users/IWeChatAccountUserService.cs new file mode 100644 index 00000000..68f21019 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.WeChat/Users/IWeChatAccountUserService.cs @@ -0,0 +1,11 @@ +using BotSharp.Abstraction.Users; +using BotSharp.Abstraction.Users.Models; +using System.Threading.Tasks; + +namespace BotSharp.Plugin.WeChat.Users +{ + public interface IWeChatAccountUserService + { + Task GetOrCreateWeChatAccountUserAsync(string appId, string openId); + } +} diff --git a/src/Plugins/BotSharp.Plugin.WeChat/Users/WeChatAccountUserService.cs b/src/Plugins/BotSharp.Plugin.WeChat/Users/WeChatAccountUserService.cs new file mode 100644 index 00000000..d9fce4e9 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.WeChat/Users/WeChatAccountUserService.cs @@ -0,0 +1,31 @@ +using BotSharp.Abstraction.Users; +using BotSharp.Abstraction.Users.Models; +using BotSharp.Core.Repository; +using BotSharp.Plugin.WeChat.Repository.DbTables; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace BotSharp.Plugin.WeChat.Users +{ + public class WeChatAccountUserService : IWeChatAccountUserService + { + private readonly IUserService userService; + + public WeChatAccountUserService(IUserService userService) + { + this.userService = userService; + } + public async Task GetOrCreateWeChatAccountUserAsync(string appId, string openId) + { + var user = await userService.CreateUser(new User() + { + Email = openId + "@" + appId + ".wechat", + Password = openId + }); + return user; + } + } +} diff --git a/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs b/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs index 65a7f600..d8946603 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs +++ b/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs @@ -2,6 +2,9 @@ using BotSharp.Abstraction.Agents; using BotSharp.Abstraction.Conversations; using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Models; +using BotSharp.Abstraction.Users; +using BotSharp.Abstraction.Users.Models; +using BotSharp.Plugin.WeChat.Users; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; using Microsoft.Extensions.DependencyInjection; @@ -39,20 +42,18 @@ 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 user = await GetWeChatAccountUserAsync(openid, scoped); + BindWeChatAccountUser(user, scoped); var conversationService = scoped.GetRequiredService(); var latestConversationId = (await conversationService.GetConversations()).OrderByDescending(_ => _.CreatedTime).FirstOrDefault()?.Id; latestConversationId ??= (await conversationService.NewConversation(new Conversation() - { - UserId = openid, - AgentId = AgentId + { + UserId = openid, + AgentId = AgentId }))?.Id; var result = await conversationService.SendMessage(AgentId, latestConversationId, new RoleDialogModel @@ -64,6 +65,31 @@ namespace BotSharp.Plugin.WeChat await ReplyTextMessageAsync(openid, result); } + private async Task GetWeChatAccountUserAsync(string openId, IServiceProvider service) + { + var userService = service.GetRequiredService(); + + return await userService.GetOrCreateWeChatAccountUserAsync(WeChatAppId, openId); + } + private void BindWeChatAccountUser(User user,IServiceProvider service) + { + var context = service.GetService(); + var claims = new List + { + new Claim(ClaimTypes.NameIdentifier, user.Id) + }; + + if (!string.IsNullOrWhiteSpace(user.Email)) + { + claims.Add(new Claim(ClaimTypes.Email, user.Email)); + } + + context.HttpContext = new DefaultHttpContext + { + User = new ClaimsPrincipal(new ClaimsIdentity(claims)) + }; + } + private async Task ReplyTextMessageAsync(string openid, string content) { await Senparc.Weixin.MP.AdvancedAPIs.CustomApi.SendTextAsync(WeChatAppId, openid, content); diff --git a/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs b/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs index 0739ebd5..b81d8ea4 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs @@ -18,6 +18,7 @@ using Senparc.CO2NET.RegisterServices; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Http; using BotSharp.Abstraction.Users; +using BotSharp.Plugin.WeChat.Users; namespace BotSharp.Plugin.WeChat { @@ -26,6 +27,8 @@ namespace BotSharp.Plugin.WeChat { public void RegisterDI(IServiceCollection services, IConfiguration config) { + services.AddScoped (); + services.AddMemoryCache(); services.Configure(config.GetSection("WeChat")); diff --git a/src/Plugins/BotSharp.Plugin.WeChat/WeChatUserIdentity.cs b/src/Plugins/BotSharp.Plugin.WeChat/WeChatUserIdentity.cs deleted file mode 100644 index a6edbf81..00000000 --- a/src/Plugins/BotSharp.Plugin.WeChat/WeChatUserIdentity.cs +++ /dev/null @@ -1,19 +0,0 @@ -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(); - } -} From 42d700e80afbd9f55d6eec4028a7281d26b96cba Mon Sep 17 00:00:00 2001 From: xbotter Date: Mon, 3 Jul 2023 11:09:20 +0800 Subject: [PATCH 4/5] docs: update wechat plugin readme.md --- src/Plugins/BotSharp.Plugin.WeChat/README.md | 60 ++++++++++---------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.WeChat/README.md b/src/Plugins/BotSharp.Plugin.WeChat/README.md index dcc51684..35344421 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/README.md +++ b/src/Plugins/BotSharp.Plugin.WeChat/README.md @@ -1,47 +1,45 @@ -# botsharp-channel-weixin -A channel module of BotSharp for Tencent Weixin +# botsharp-plugin-wechat -### How to install through NuGet - -``` -PM> Install-Package BotSharp.Channel.Weixin -``` +A channel plugin of BotSharp for Tencent WeChat ### How to run locally ``` -git clone https://github.com/dotnetcore/BotSharp +git clone https://github.com/SciSharp/BotSharp ``` -Check app.json to use DialogflowAi +Check appsettings.json to import module ``` { - "version": "0.1.0", - "assemblies": "BotSharp.Core", - - "platformModuleName": "DialogflowAi" -} -``` - -Update channels.weixin.json to set the corresponding KEY -``` -{ - "weixinChannel": { - "token": "botsharp", - "encodingAESKey": "", - "appId": "", - "agentId": "60bee6f9-ba58-4fe8-8b95-94af69d6fd41" + "PluginLoader": { + "Assemblies": [ + ... + "BotSharp.Plugin.WeChat" + ], + "Plugins": [ + ... + "WeChatPlugin" + ] } } ``` -F5 run BotSharp.WebHost +Update appsettings.json to set the corresponding KEY +``` +{ + "WeChat": { + "AgentId": "437bed34-1169-4833-95ce-c24b8b56154a", + "Token": "#{Token}#", + "EncodingAESKey": "#{EncodingAESKey}#", + "WeixinAppId": "#{WeixinAppId}#", + "WeixinAppSecret": "#{WeixinAppSecret}#" + } +} +``` -Access http://localhost:3112 +Update WeChat WebHook form https://mp.weixin.qq.com/ ,set as `https://{HOST}/WeChatAsync` -Import demo (Spotify.zip) agent located at App_Data +F5 run WebStarter -Train agent (id: 60bee6f9-ba58-4fe8-8b95-94af69d6fd41) +Access http://localhost:5500 -Or refer [BotSharp docs](https://botsharp.readthedocs.io) to design your new chatbot. - -Setup Wechat webhood from https://mp.weixin.qq.com/. +If you are debugging and developing locally, you can use an intranet penetration tool to receive WeChat message push. It is recommended to use the Dev Tunnels of VS. From 9dc2d535aa59f4f31bd28063f1b6d50ce5cb46af Mon Sep 17 00:00:00 2001 From: xbotter Date: Mon, 3 Jul 2023 11:45:47 +0800 Subject: [PATCH 5/5] clean error namespace --- .../BotSharp.Plugin.WeChat/Users/WeChatAccountUserService.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Plugins/BotSharp.Plugin.WeChat/Users/WeChatAccountUserService.cs b/src/Plugins/BotSharp.Plugin.WeChat/Users/WeChatAccountUserService.cs index d9fce4e9..bf48c433 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/Users/WeChatAccountUserService.cs +++ b/src/Plugins/BotSharp.Plugin.WeChat/Users/WeChatAccountUserService.cs @@ -1,7 +1,6 @@ using BotSharp.Abstraction.Users; using BotSharp.Abstraction.Users.Models; using BotSharp.Core.Repository; -using BotSharp.Plugin.WeChat.Repository.DbTables; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic;