add webstarter setting

This commit is contained in:
xbotter 2023-06-27 07:12:38 +08:00
parent 8b680203dd
commit 62e833d6b8
No known key found for this signature in database
GPG key ID: D299220A7FE5CF1E
4 changed files with 40 additions and 13 deletions

View file

@ -1,6 +1,7 @@
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Infrastructures.ContentTransmitters;
using BotSharp.Abstraction.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
@ -13,23 +14,26 @@ namespace BotSharp.Plugin.WeChat
public class WeChatBackgroundService : IHostedService, IMessageQueue
{
private readonly Channel<WeChatMessage> _queue;
private readonly IConversationService _conversationService;
private readonly IContentTransfer _contentTransfer;
private readonly IServiceProvider _service;
private readonly ILogger<WeChatBackgroundService> _logger;
public WeChatBackgroundService(IConversationService conversationService,
IContentTransfer contentTransfer,
public WeChatBackgroundService(
IServiceProvider service,
ILogger<WeChatBackgroundService> logger)
{
this._conversationService = conversationService;
this._contentTransfer = contentTransfer;
this._service = service;
this._logger = logger;
this._queue = Channel.CreateUnbounded<WeChatMessage>();
}
private async Task HandleTextMessageAsync(string openid, string message)
{
var conversations = _conversationService.GetDialogHistory(openid);
var scoped = _service.CreateScope().ServiceProvider;
var conversationService = scoped.GetRequiredService<IConversationService>();
var contentTransfer = scoped.GetRequiredService<IContentTransfer>();
var conversations = conversationService.GetDialogHistory(openid);
conversations.Add(new RoleDialogModel
{
Role = "User",
@ -41,13 +45,13 @@ namespace BotSharp.Plugin.WeChat
Conversations = conversations
};
var result = await _contentTransfer.Transport(container);
var result = await contentTransfer.Transport(container);
if (result.IsSuccess)
{
var output = container.Output.Text.Trim();
await ReplyTextMessageAsync(openid, output);
_conversationService.AddDialog(new RoleDialogModel()
conversationService.AddDialog(new RoleDialogModel()
{
Role = "Assistant",
Text = output,

View file

@ -13,6 +13,10 @@ using System.Text;
using Senparc.Weixin;
using Senparc.Weixin.MP;
using Senparc.Weixin.MP.MessageHandlers.Middleware;
using Senparc.Weixin.Entities;
using Senparc.CO2NET.RegisterServices;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
namespace BotSharp.Plugin.WeChat
{
@ -23,7 +27,12 @@ namespace BotSharp.Plugin.WeChat
{
services.AddMemoryCache();
services.AddSenparcWeixinServices(config);
services.Configure<SenparcWeixinSetting>(config.GetSection("WeChat"));
if (!Senparc.CO2NET.RegisterServices.RegisterServiceExtension.SenparcGlobalServicesRegistered)
{
services = services.AddSenparcGlobalServices(config);
}
services.AddHostedService<WeChatBackgroundService>();
@ -33,17 +42,21 @@ namespace BotSharp.Plugin.WeChat
public void Configure(IApplicationBuilder app)
{
var env = app.ApplicationServices.GetRequiredService<IHostEnvironment>();
var logger = app.ApplicationServices.GetRequiredService<ILogger<WeChatPlugin>>();
var register = app.UseSenparcGlobal(env);
register.UseSenparcWeixin(null, (svc, settings) =>
{
svc.RegisterMpAccount(settings, "WeChat");
}, app.ApplicationServices);
app.UseMessageHandlerForMp("/WeixinAsync", BotSharpMessageHandler.GenerateMessageHandler, options =>
app.UseMessageHandlerForMp("/WeChatAsync", BotSharpMessageHandler.GenerateMessageHandler, options =>
{
options.AccountSettingFunc = context => Senparc.Weixin.Config.SenparcWeixinSetting;
});
logger.LogInformation("WeChat Message Handler is running on /WeChatAsync.");
}
}
}

View file

@ -42,6 +42,7 @@
<ProjectReference Include="..\Plugins\BotSharp.Plugin.MetaAI\BotSharp.Plugin.MetaAI.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.PaddleSharp\BotSharp.Plugin.PaddleSharp.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.Qdrant\BotSharp.Plugin.Qdrant.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.WeChat\BotSharp.Plugin.WeChat.csproj" />
</ItemGroup>
</Project>

View file

@ -14,7 +14,7 @@
},
"Conversation": {
},
"LlamaSharp": {
@ -60,6 +60,13 @@
"ApiKey": ""
},
"WeChat": {
"Token": "#{Token}#",
"EncodingAESKey": "#{EncodingAESKey}#",
"WeixinAppId": "#{WeixinAppId}#",
"WeixinAppSecret": "#{WeixinAppSecret}#"
},
"PluginLoader": {
"Assemblies": [
"BotSharp.Core",
@ -67,13 +74,15 @@
"BotSharp.Plugin.MetaAI",
"BotSharp.Plugin.Qdrant",
"BotSharp.Plugin.PaddleSharp"
"BotSharp.Plugin.WeChat"
],
"Plugins": [
// "LLamaSharpPlugin",
"AzureOpenAiPlugin",
"MetaAiPlugin",
"QdrantPlugin",
"PaddleSharpPlugin"
"PaddleSharpPlugin",
"WeChatPlugin"
]
}
}