using BotSharp.Abstraction.Functions; using BotSharp.Abstraction.Models; using BotSharp.Abstraction.SideCar; using BotSharp.Core.MessageHub; using System.Text.Json.Serialization; namespace BotSharp.Core.Demo.Functions; 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 Execute(RoleDialogModel message) { var args = JsonSerializer.Deserialize(message.FunctionArgs); var conv = _services.GetRequiredService(); var messageHub = _services.GetRequiredService>>(); await Task.Delay(1000); message.Indication = $"Start querying weather data in {args?.City}"; messageHub.Push(new() { EventName = ChatEvent.OnIndicationReceived, Data = message, RefId = conv.ConversationId }); await Task.Delay(1500); message.Indication = $"Still working on it... Hold on, {args?.City}"; messageHub.Push(new() { EventName = ChatEvent.OnIndicationReceived, Data = message, RefId = conv.ConversationId }); await Task.Delay(1500); message.Content = $"It is a sunny day!"; message.StopCompletion = false; #if DEBUG var sidecar = _services.GetService(); if (sidecar != null) { var text = $"I want to know fun events in {args?.City}"; var states = new List { new() { Key = "channel", Value = "email" } }; var msg = await sidecar.SendMessage(message.CurrentAgentId, text, states: states); message.Content = $"{message.Content} {msg.Content}"; } #endif return true; } } class WeatherLocation { [JsonPropertyName("city")] public string City { get; set; } }