BotSharp/src/Infrastructure/BotSharp.Core/Demo/Functions/GetWeatherFn.cs

59 lines
1.6 KiB
C#
Raw Normal View History

2025-05-13 16:45:19 +00:00
using BotSharp.Abstraction.Functions;
2025-07-28 22:41:28 +00:00
using BotSharp.Core.MessageHub;
2025-07-31 22:48:51 +00:00
using System.Text.Json.Serialization;
2025-05-13 16:45:19 +00:00
2025-05-16 03:59:57 +00:00
namespace BotSharp.Core.Demo.Functions;
2025-05-13 16:45:19 +00:00
public class GetWeatherFn : IFunctionCallback
{
private readonly IServiceProvider _services;
public GetWeatherFn(IServiceProvider services)
{
_services = services;
}
public string Name => "get_weather";
public string Indication => "Querying weather";
public async Task<bool> Execute(RoleDialogModel message)
{
2025-07-31 22:48:51 +00:00
var args = JsonSerializer.Deserialize<WeatherLocation>(message.FunctionArgs);
2025-07-28 22:41:28 +00:00
var conv = _services.GetRequiredService<IConversationService>();
2025-07-29 18:55:53 +00:00
var messageHub = _services.GetRequiredService<MessageHub<HubObserveData<RoleDialogModel>>>();
2025-07-28 22:41:28 +00:00
await Task.Delay(1000);
2025-07-31 22:48:51 +00:00
message.Indication = $"Start querying weather data in {args?.City}";
2025-07-28 22:41:28 +00:00
messageHub.Push(new()
{
EventName = ChatEvent.OnIndicationReceived,
Data = message,
2025-07-31 22:48:51 +00:00
RefId = conv.ConversationId,
2025-07-28 22:41:28 +00:00
ServiceProvider = _services
});
await Task.Delay(1500);
2025-07-31 22:48:51 +00:00
message.Indication = $"Still working on it, {args?.City}";
2025-07-28 22:41:28 +00:00
messageHub.Push(new()
{
EventName = ChatEvent.OnIndicationReceived,
Data = message,
2025-07-31 22:48:51 +00:00
RefId = conv.ConversationId,
2025-07-28 22:41:28 +00:00
ServiceProvider = _services
});
await Task.Delay(1500);
2025-05-16 04:02:38 +00:00
message.Content = $"It is a sunny day!";
2025-06-24 02:43:25 +00:00
message.StopCompletion = false;
2025-05-13 16:45:19 +00:00
return true;
}
2025-07-31 22:48:51 +00:00
}
class WeatherLocation
{
[JsonPropertyName("city")]
public string City { get; set; }
2025-05-13 16:45:19 +00:00
}