Merge pull request #108 from hchen2020/master

Support rich content in UI.
This commit is contained in:
Haiping 2023-08-20 20:46:58 -05:00 committed by GitHub
commit 7cf649d73e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 260 additions and 53 deletions

View file

@ -2,5 +2,6 @@ namespace BotSharp.Abstraction.Agents;
public interface IAgentRouting public interface IAgentRouting
{ {
Task<Agent> LoadRouter();
Task<Agent> LoadCurrentAgent(); Task<Agent> LoadCurrentAgent();
} }

View file

@ -0,0 +1,9 @@
using System.Text.Json.Serialization;
namespace BotSharp.Abstraction.Agents.Models;
public class AgentRoutingArgs
{
[JsonPropertyName("agent_id")]
public string AgentId { get; set; }
}

View file

@ -20,12 +20,14 @@ public interface IConversationService
/// <param name="lastDalog"></param> /// <param name="lastDalog"></param>
/// <param name="onMessageReceived"></param> /// <param name="onMessageReceived"></param>
/// <param name="onFunctionExecuting">This delegate is useful when you want to report progress on UI</param> /// <param name="onFunctionExecuting">This delegate is useful when you want to report progress on UI</param>
/// <param name="onFunctionExecuted">This delegate is useful when you want to report progress on UI</param>
/// <returns></returns> /// <returns></returns>
Task<bool> SendMessage(string agentId, Task<bool> SendMessage(string agentId,
string conversationId, string conversationId,
RoleDialogModel lastDalog, RoleDialogModel lastDalog,
Func<RoleDialogModel, Task> onMessageReceived, Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting); Func<RoleDialogModel, Task> onFunctionExecuting,
Func<RoleDialogModel, Task> onFunctionExecuted);
List<RoleDialogModel> GetDialogHistory(string conversationId, int lastCount = 20); List<RoleDialogModel> GetDialogHistory(string conversationId, int lastCount = 20);
Task CleanHistory(string agentId); Task CleanHistory(string agentId);

View file

@ -20,10 +20,16 @@ public class RoleDialogModel
public string? FunctionArgs { get; set; } public string? FunctionArgs { get; set; }
/// <summary> /// <summary>
/// Function execution result /// Function execution result, this result will be seen by LLM.
/// </summary> /// </summary>
public string? ExecutionResult { get; set; } public string? ExecutionResult { get; set; }
/// <summary>
/// Function execution structured data, this data won't pass to LLM.
/// It's ideal to render in rich content in UI.
/// </summary>
public object ExecutionData { get; set; }
public bool IsConversationEnd { get; set; } public bool IsConversationEnd { get; set; }
public bool NeedReloadAgent { get; set; } public bool NeedReloadAgent { get; set; }

View file

@ -5,4 +5,5 @@ public class ConversationSetting
public string DataDir { get; set; } public string DataDir { get; set; }
public string ChatCompletion { get; set; } public string ChatCompletion { get; set; }
public bool EnableKnowledgeBase { get; set; } public bool EnableKnowledgeBase { get; set; }
public bool ShowVerboseLog { get; set; }
} }

View file

@ -1,3 +1,4 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Core.Agents.Services; namespace BotSharp.Core.Agents.Services;
@ -17,11 +18,18 @@ public class AgentRouter : IAgentRouting
_settings = settings; _settings = settings;
} }
public async Task<Agent> LoadRouter()
{
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(_settings.RouterId);
return agent;
}
public async Task<Agent> LoadCurrentAgent() public async Task<Agent> LoadCurrentAgent()
{ {
// Load current agent from state // Load current agent from state
var state = _services.GetRequiredService<IConversationStateService>(); var state = _services.GetRequiredService<IConversationStateService>();
var currentAgentId = state.GetState("agentId"); var currentAgentId = state.GetState("agent_id");
if (string.IsNullOrEmpty(currentAgentId)) if (string.IsNullOrEmpty(currentAgentId))
{ {
currentAgentId = _settings.RouterId; currentAgentId = _settings.RouterId;
@ -30,7 +38,7 @@ public class AgentRouter : IAgentRouting
var agent = await agentService.LoadAgent(currentAgentId); var agent = await agentService.LoadAgent(currentAgentId);
// Set agent and trigger state changed // Set agent and trigger state changed
state.SetState("agentId", currentAgentId); state.SetState("agent_id", currentAgentId);
return agent; return agent;
} }

View file

@ -73,7 +73,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Colorful.Console" Version="1.2.15" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="6.2.1" /> <PackageReference Include="EntityFrameworkCore.BootKit" Version="6.2.1" />
<PackageReference Include="Fluid.Core" Version="2.4.0" /> <PackageReference Include="Fluid.Core" Version="2.4.0" />
<PackageReference Include="TensorFlow.Keras" Version="0.11.2" /> <PackageReference Include="TensorFlow.Keras" Version="0.11.2" />

View file

@ -38,6 +38,7 @@ public static class BotSharpServiceCollectionExtensions
services.AddScoped<IAgentRouting, AgentRouter>(); services.AddScoped<IAgentRouting, AgentRouter>();
services.AddScoped<IFunctionCallback, GoToRouterFn>(); services.AddScoped<IFunctionCallback, GoToRouterFn>();
services.AddScoped<IFunctionCallback, RouteToAgentFn>();
return services; return services;
} }

View file

@ -15,7 +15,8 @@ public partial class ConversationService
Agent agent, Agent agent,
List<RoleDialogModel> wholeDialogs, List<RoleDialogModel> wholeDialogs,
Func<RoleDialogModel, Task> onMessageReceived, Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting) Func<RoleDialogModel, Task> onFunctionExecuting,
Func<RoleDialogModel, Task> onFunctionExecuted)
{ {
currentRecursiveDepth++; currentRecursiveDepth++;
if (currentRecursiveDepth > maxRecursiveDepth) if (currentRecursiveDepth > maxRecursiveDepth)
@ -23,7 +24,8 @@ public partial class ConversationService
_logger.LogError($"Exceed max current recursive depth."); _logger.LogError($"Exceed max current recursive depth.");
await HandleAssistantMessage(new RoleDialogModel(AgentRole.Assistant, "System has exception, please try later.") await HandleAssistantMessage(new RoleDialogModel(AgentRole.Assistant, "System has exception, please try later.")
{ {
CurrentAgentId = agent.Id CurrentAgentId = agent.Id,
Channel = wholeDialogs.Last().Channel
}, onMessageReceived); }, onMessageReceived);
return false; return false;
} }
@ -38,14 +40,15 @@ public partial class ConversationService
{ {
var preAgentId = agent.Id; var preAgentId = agent.Id;
await HandleFunctionMessage(fn, onFunctionExecuting); await HandleFunctionMessage(fn, onFunctionExecuting, onFunctionExecuted);
// Function executed has exception // Function executed has exception
if (fn.ExecutionResult == null) if (fn.ExecutionResult == null)
{ {
await HandleAssistantMessage(new RoleDialogModel(AgentRole.Assistant, fn.Content) await HandleAssistantMessage(new RoleDialogModel(AgentRole.Assistant, fn.Content)
{ {
CurrentAgentId = fn.CurrentAgentId CurrentAgentId = fn.CurrentAgentId,
Channel = fn.Channel
}, onMessageReceived); }, onMessageReceived);
return; return;
} }
@ -58,10 +61,6 @@ public partial class ConversationService
var agentSettings = _services.GetRequiredService<AgentSettings>(); var agentSettings = _services.GetRequiredService<AgentSettings>();
var agentService = _services.GetRequiredService<IAgentService>(); var agentService = _services.GetRequiredService<IAgentService>();
agent = await agentService.LoadAgent(fn.CurrentAgentId); agent = await agentService.LoadAgent(fn.CurrentAgentId);
// Set state to make next conversation will go to this agent directly
// var state = _services.GetRequiredService<IConversationStateService>();
// state.SetState("agentId", fn.CurrentAgentId);
} }
// Add to dialog history // Add to dialog history
@ -70,7 +69,13 @@ public partial class ConversationService
// After function is executed, pass the result to LLM to get a natural response // After function is executed, pass the result to LLM to get a natural response
wholeDialogs.Add(fn); wholeDialogs.Add(fn);
await GetChatCompletionsAsyncRecursively(chatCompletion, conversationId, agent, wholeDialogs, onMessageReceived, onFunctionExecuting); await GetChatCompletionsAsyncRecursively(chatCompletion,
conversationId,
agent,
wholeDialogs,
onMessageReceived,
onFunctionExecuting,
onFunctionExecuted);
}); });
return result; return result;
@ -89,7 +94,9 @@ public partial class ConversationService
await onMessageReceived(msg); await onMessageReceived(msg);
} }
private async Task HandleFunctionMessage(RoleDialogModel msg, Func<RoleDialogModel, Task> onFunctionExecuting) private async Task HandleFunctionMessage(RoleDialogModel msg,
Func<RoleDialogModel, Task> onFunctionExecuting,
Func<RoleDialogModel, Task> onFunctionExecuted)
{ {
// Save states // Save states
SaveStateByArgs(msg.FunctionArgs); SaveStateByArgs(msg.FunctionArgs);
@ -97,5 +104,6 @@ public partial class ConversationService
// Call functions // Call functions
await onFunctionExecuting(msg); await onFunctionExecuting(msg);
await CallFunctions(msg); await CallFunctions(msg);
await onFunctionExecuted(msg);
} }
} }

View file

@ -8,7 +8,8 @@ public partial class ConversationService
public async Task<bool> SendMessage(string agentId, string conversationId, public async Task<bool> SendMessage(string agentId, string conversationId,
RoleDialogModel lastDialog, RoleDialogModel lastDialog,
Func<RoleDialogModel, Task> onMessageReceived, Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting) Func<RoleDialogModel, Task> onFunctionExecuting,
Func<RoleDialogModel, Task> onFunctionExecuted)
{ {
var converation = await GetConversation(conversationId); var converation = await GetConversation(conversationId);
@ -27,16 +28,19 @@ public partial class ConversationService
var stateService = _services.GetRequiredService<IConversationStateService>(); var stateService = _services.GetRequiredService<IConversationStateService>();
stateService.SetConversation(conversationId); stateService.SetConversation(conversationId);
stateService.Load(); stateService.Load();
stateService.SetState("channel", lastDialog.Channel);
var router = _services.GetRequiredService<IAgentRouting>(); var router = _services.GetRequiredService<IAgentRouting>();
var agent = await router.LoadCurrentAgent(); var agent = await router.LoadRouter();
_logger.LogInformation($"[{agent.Name}] {lastDialog.Role}: {lastDialog.Content}"); _logger.LogInformation($"[{agent.Name}] {lastDialog.Role}: {lastDialog.Content}");
lastDialog.CurrentAgentId = agent.Id; lastDialog.CurrentAgentId = agent.Id;
_storage.Append(conversationId, agent.Id, lastDialog);
var wholeDialogs = GetDialogHistory(conversationId); var wholeDialogs = GetDialogHistory(conversationId);
wholeDialogs.Add(lastDialog);
_storage.Append(conversationId, agent.Id, lastDialog);
// Get relevant domain knowledge // Get relevant domain knowledge
/*if (_settings.EnableKnowledgeBase) /*if (_settings.EnableKnowledgeBase)
@ -67,7 +71,8 @@ public partial class ConversationService
agent, agent,
wholeDialogs, wholeDialogs,
onMessageReceived, onMessageReceived,
onFunctionExecuting); onFunctionExecuting,
onFunctionExecuted);
return result; return result;
} }

View file

@ -0,0 +1,36 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.Functions.Models;
namespace BotSharp.Core.Functions;
public class RouteToAgentFn : IFunctionCallback
{
public string Name => "route_to_agent";
private readonly IServiceProvider _services;
public RouteToAgentFn(IServiceProvider services)
{
_services = services;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<AgentRoutingArgs>(message.FunctionArgs);
if (string.IsNullOrEmpty(args.AgentId))
{
var result = new FunctionExecutionValidationResult("false", "agent_id can't be parsed.");
message.ExecutionResult = JsonSerializer.Serialize(result);
}
else
{
var result = new FunctionExecutionValidationResult("true");
message.ExecutionResult = JsonSerializer.Serialize(result);
message.CurrentAgentId = args.AgentId;
}
return true;
}
}

View file

@ -3,7 +3,6 @@ using Microsoft.Extensions.Configuration;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using Console = Colorful.Console;
namespace BotSharp.Core.Plugins; namespace BotSharp.Core.Plugins;

View file

@ -1,8 +1,6 @@
using BotSharp.Abstraction.ApiAdapters; using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Conversations.Models;
using BotSharp.OpenAPI.ViewModels.Conversations; using BotSharp.OpenAPI.ViewModels.Conversations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace BotSharp.OpenAPI.Controllers; namespace BotSharp.OpenAPI.Controllers;
@ -50,11 +48,23 @@ public class ConversationController : ControllerBase, IApiAdapter
var stackMsg = new List<RoleDialogModel>(); var stackMsg = new List<RoleDialogModel>();
await conv.SendMessage(agentId, conversationId, await conv.SendMessage(agentId, conversationId,
new RoleDialogModel("user", input.Text), new RoleDialogModel("user", input.Text)
{
Channel = "webapi"
},
async msg => async msg =>
stackMsg.Add(msg), {
async fn stackMsg.Add(msg);
=> await Task.CompletedTask); },
async fnExecuting =>
{
},
async fnExecuted =>
{
response.Function = fnExecuted.FunctionName;
response.Data = fnExecuted.ExecutionData;
});
response.Text = string.Join("\r\n", stackMsg.Select(x => x.Content)); response.Text = string.Join("\r\n", stackMsg.Select(x => x.Content));
return response; return response;

View file

@ -3,4 +3,6 @@ namespace BotSharp.OpenAPI.ViewModels.Conversations;
public class MessageResponseModel public class MessageResponseModel
{ {
public string Text { get; set; } public string Text { get; set; }
public string Function { get; set; }
public object Data { get; set; }
} }

View file

@ -3,9 +3,11 @@ using Azure.AI.OpenAI;
using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Conversations.Settings;
using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.MLTasks;
using BotSharp.Plugin.AzureOpenAI.Settings; using BotSharp.Plugin.AzureOpenAI.Settings;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -18,12 +20,16 @@ namespace BotSharp.Plugin.AzureOpenAI.Providers;
public class ChatCompletionProvider : IChatCompletion public class ChatCompletionProvider : IChatCompletion
{ {
private readonly AzureOpenAiSettings _settings; private readonly AzureOpenAiSettings _settings;
private readonly IServiceProvider _services;
private readonly ILogger _logger; private readonly ILogger _logger;
public ChatCompletionProvider(AzureOpenAiSettings settings, ILogger<ChatCompletionProvider> logger) public ChatCompletionProvider(AzureOpenAiSettings settings,
ILogger<ChatCompletionProvider> logger,
IServiceProvider services)
{ {
_settings = settings; _settings = settings;
_logger = logger; _logger = logger;
_services = services;
} }
private OpenAIClient GetClient() private OpenAIClient GetClient()
@ -98,7 +104,8 @@ public class ChatCompletionProvider : IChatCompletion
{ {
CurrentAgentId = agent.Id, CurrentAgentId = agent.Id,
FunctionName = message.FunctionCall.Name, FunctionName = message.FunctionCall.Name,
FunctionArgs = message.FunctionCall.Arguments FunctionArgs = message.FunctionCall.Arguments,
Channel = conversations.Last().Channel
}; };
// Execute functions // Execute functions
@ -110,7 +117,8 @@ public class ChatCompletionProvider : IChatCompletion
var msg = new RoleDialogModel(AgentRole.Assistant, message.Content) var msg = new RoleDialogModel(AgentRole.Assistant, message.Content)
{ {
CurrentAgentId= agent.Id CurrentAgentId= agent.Id,
Channel = conversations.Last().Channel
}; };
// Text response received // Text response received
@ -215,7 +223,18 @@ public class ChatCompletionProvider : IChatCompletion
chatCompletionsOptions.Temperature = 0.5f; chatCompletionsOptions.Temperature = 0.5f;
chatCompletionsOptions.NucleusSamplingFactor = 0.5f; chatCompletionsOptions.NucleusSamplingFactor = 0.5f;
_logger.LogInformation(string.Join("\n", chatCompletionsOptions.Messages.Select(x => $"{x.Role}: {x.Content}"))); var convSetting = _services.GetRequiredService<ConversationSetting>();
if (convSetting.ShowVerboseLog)
{
var verbose = string.Join("\n", chatCompletionsOptions.Messages.Select(x =>
{
return x.Role == ChatRole.Function ?
$"{x.Role}: {x.Name} {x.Content}" :
$"{x.Role}: {x.Content}";
}));
_logger.LogInformation(verbose);
}
return chatCompletionsOptions; return chatCompletionsOptions;
} }
} }

View file

@ -64,7 +64,10 @@ public class ChatbotUiController : ControllerBase, IApiAdapter
var conversation = input.Messages var conversation = input.Messages
.Where(x => x.Role == AgentRole.User) .Where(x => x.Role == AgentRole.User)
.Select(x => new RoleDialogModel(x.Role, x.Content)) .Select(x => new RoleDialogModel(x.Role, x.Content)
{
Channel = "webchat"
})
.Last(); .Last();
var conversationService = _services.GetRequiredService<IConversationService>(); var conversationService = _services.GetRequiredService<IConversationService>();
@ -75,6 +78,8 @@ public class ChatbotUiController : ControllerBase, IApiAdapter
async msg => async msg =>
await OnChunkReceived(outputStream, msg), await OnChunkReceived(outputStream, msg),
async fn async fn
=> await Task.CompletedTask,
async fn
=> await Task.CompletedTask); => await Task.CompletedTask);
await OnEventCompleted(outputStream); await OnEventCompleted(outputStream);

View file

@ -15,7 +15,7 @@ using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Refit; using Refit;
using BotSharp.Abstraction.Agents.Enums; using Microsoft.Extensions.Logging;
namespace BotSharp.Plugin.MetaMessenger.Controllers; namespace BotSharp.Plugin.MetaMessenger.Controllers;
@ -27,10 +27,12 @@ namespace BotSharp.Plugin.MetaMessenger.Controllers;
public class WebhookController : ControllerBase public class WebhookController : ControllerBase
{ {
private readonly IServiceProvider _services; private readonly IServiceProvider _services;
private readonly ILogger _logger;
public WebhookController(IServiceProvider services) public WebhookController(IServiceProvider services, ILogger<WebhookController> logger)
{ {
_services = services; _services = services;
_logger = logger;
} }
[HttpGet("/messenger/webhook/{agentId}")] [HttpGet("/messenger/webhook/{agentId}")]
@ -67,7 +69,7 @@ public class WebhookController : ControllerBase
{ {
var conv = _services.GetRequiredService<IConversationService>(); var conv = _services.GetRequiredService<IConversationService>();
string content = ""; var reply = new QuickReplyMessage();
var senderId = req.Entry[0].Messaging[0].Sender.Id; var senderId = req.Entry[0].Messaging[0].Sender.Id;
var input = req.Entry[0].Messaging[0].Message.Text; var input = req.Entry[0].Messaging[0].Message.Text;
@ -78,11 +80,13 @@ public class WebhookController : ControllerBase
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
}; };
var recipient = JsonSerializer.Serialize(new { Id = senderId }, jsonOpt);
// Marking seen // Marking seen
await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest
{ {
AccessToken = setting.PageAccessToken, AccessToken = setting.PageAccessToken,
Recipient = JsonSerializer.Serialize(new { Id = senderId }, jsonOpt), Recipient = recipient,
SenderAction = SenderActionEnum.MarkSeen SenderAction = SenderActionEnum.MarkSeen
}); });
@ -90,7 +94,7 @@ public class WebhookController : ControllerBase
await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest
{ {
AccessToken = setting.PageAccessToken, AccessToken = setting.PageAccessToken,
Recipient = JsonSerializer.Serialize(new { Id = senderId }, jsonOpt), Recipient = recipient,
SenderAction = SenderActionEnum.TypingOn SenderAction = SenderActionEnum.TypingOn
}); });
@ -100,12 +104,8 @@ public class WebhookController : ControllerBase
Channel = "messenger" Channel = "messenger"
}, async msg => }, async msg =>
{ {
if (msg.Role == AgentRole.Function) reply.Text = msg.Content;
{ }, async functionExecuting =>
}
content = msg.Content;
}, async fn =>
{ {
/*await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest /*await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest
{ {
@ -113,28 +113,39 @@ public class WebhookController : ControllerBase
Recipient = JsonSerializer.Serialize(new { Id = sessionId }, jsonOpt), Recipient = JsonSerializer.Serialize(new { Id = sessionId }, jsonOpt),
Message = JsonSerializer.Serialize(new { Text = "I'm pulling the relevent information, please wait a second ..." }, jsonOpt) Message = JsonSerializer.Serialize(new { Text = "I'm pulling the relevent information, please wait a second ..." }, jsonOpt)
});*/ });*/
}, async functionExecuted =>
await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest {
// Render structured data
if (functionExecuted.ExecutionData != null)
{ {
AccessToken = setting.PageAccessToken, // validate data format
Recipient = JsonSerializer.Serialize(new { Id = senderId }, jsonOpt), var json = JsonSerializer.Serialize(functionExecuted.ExecutionData, jsonOpt);
SenderAction = SenderActionEnum.TypingOn
}); try
{
var parsed = JsonSerializer.Deserialize<QuickReplyMessageItem[]>(json, jsonOpt);
reply.QuickReplies = parsed;
}
catch(Exception ex)
{
_logger.LogError(ex, ex.Message);
}
}
}); });
// Response to user // Response to user
await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest
{ {
AccessToken = setting.PageAccessToken, AccessToken = setting.PageAccessToken,
Recipient = JsonSerializer.Serialize(new { Id = senderId }, jsonOpt), Recipient = recipient,
Message = JsonSerializer.Serialize(new { Text = content }, jsonOpt) Message = JsonSerializer.Serialize(reply, jsonOpt)
}); });
// Typing off // Typing off
await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest
{ {
AccessToken = setting.PageAccessToken, AccessToken = setting.PageAccessToken,
Recipient = JsonSerializer.Serialize(new { Id = senderId }, jsonOpt), Recipient = recipient,
SenderAction = SenderActionEnum.TypingOff SenderAction = SenderActionEnum.TypingOff
}); });
} }

View file

@ -0,0 +1,5 @@
namespace BotSharp.Plugin.MetaMessenger.Interfaces;
public interface IResponseMessage
{
}

View file

@ -0,0 +1,11 @@
using System.Text.Json.Serialization;
namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
public class AttachementPayload
{
[JsonPropertyName("template_type")]
public string TemplateType { get; set; }
public string Text { get; set; }
public ButtonItem[] Buttons { get; set; }
}

View file

@ -0,0 +1,7 @@
namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
public class AttachmentBody
{
public string Type { get; set; } = "template";
public AttachementPayload Payload { get; set; }
}

View file

@ -0,0 +1,8 @@
namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
public class ButtonItem
{
public string Type { get; set; }
public string Title { get; set; }
public string Url { get; set; }
}

View file

@ -0,0 +1,17 @@
using BotSharp.Plugin.MetaMessenger.Interfaces;
using System.Text.Json.Serialization;
namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
/// <summary>
/// Quick Replies
/// https://developers.facebook.com/docs/messenger-platform/send-messages/quick-replies
/// </summary>
public class QuickReplyMessage : IResponseMessage
{
[JsonPropertyName("text")]
public string Text { get; set; }
[JsonPropertyName("quick_replies")]
public QuickReplyMessageItem[] QuickReplies { get; set; }
}

View file

@ -0,0 +1,18 @@
using System.Text.Json.Serialization;
namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
public class QuickReplyMessageItem
{
[JsonPropertyName("content_type")]
public string ContentType { get; set; } = "text";
[JsonPropertyName("title")]
public string Title { get; set; }
[JsonPropertyName("payload")]
public string Payload { get; set; }
[JsonPropertyName("image_url")]
public string ImageUrl { get; set; }
}

View file

@ -0,0 +1,13 @@
using BotSharp.Plugin.MetaMessenger.Interfaces;
using System.Text.Json.Serialization;
namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
/// <summary>
/// https://developers.facebook.com/docs/messenger-platform/send-messages/templates
/// </summary>
public class TemplateMessage : IResponseMessage
{
[JsonPropertyName("attachment")]
public AttachmentBody Attachment { get; set; }
}

View file

@ -1,3 +1,4 @@
using BotSharp.Plugin.MetaMessenger.MessagingModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@ -10,4 +11,6 @@ public class WebhookMessageBody
[JsonPropertyName("mid")] [JsonPropertyName("mid")]
public string Id { get;set; } public string Id { get;set; }
public string Text { get;set; } public string Text { get;set; }
[JsonPropertyName("quick_reply")]
public QuickReplyMessageItem QuickReply { get;set; }
} }

View file

@ -59,7 +59,10 @@ namespace BotSharp.Plugin.WeChat
var result = await conversationService.SendMessage(AgentId, latestConversationId, new RoleDialogModel("user", message), async msg => var result = await conversationService.SendMessage(AgentId, latestConversationId, new RoleDialogModel("user", message), async msg =>
{ {
await ReplyTextMessageAsync(openid, msg.Content); await ReplyTextMessageAsync(openid, msg.Content);
}, async fn => }, async functionExecuting =>
{
}, async functionExecuted =>
{ {
}); });