2024-09-24 15:45:57 +00:00
|
|
|
using Azure;
|
2024-02-22 19:37:14 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Enums;
|
|
|
|
|
using BotSharp.Abstraction.MLTasks;
|
|
|
|
|
using BotSharp.Core.Infrastructures;
|
|
|
|
|
using BotSharp.Plugin.SqlDriver.Models;
|
|
|
|
|
using MySqlConnector;
|
|
|
|
|
using static Dapper.SqlMapper;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.SqlDriver.Functions;
|
|
|
|
|
|
|
|
|
|
public class LookupDictionaryFn : IFunctionCallback
|
|
|
|
|
{
|
2024-09-24 15:45:57 +00:00
|
|
|
public string Name => "sql_dictionary_lookup";
|
2024-02-22 19:37:14 +00:00
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
|
|
|
|
|
public LookupDictionaryFn(IServiceProvider services)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> Execute(RoleDialogModel message)
|
|
|
|
|
{
|
|
|
|
|
var args = JsonSerializer.Deserialize<LookupDictionary>(message.FunctionArgs);
|
|
|
|
|
|
2024-09-24 15:45:57 +00:00
|
|
|
// check if need to instantely
|
2024-02-22 19:37:14 +00:00
|
|
|
var settings = _services.GetRequiredService<SqlDriverSetting>();
|
2024-09-24 15:45:57 +00:00
|
|
|
using var connection = new MySqlConnection(settings.MySqlExecutionConnectionString);
|
|
|
|
|
var result = connection.Query(args.SqlStatement);
|
2024-02-22 19:37:14 +00:00
|
|
|
|
2024-09-24 15:45:57 +00:00
|
|
|
if (result == null)
|
2024-02-22 19:37:14 +00:00
|
|
|
{
|
2024-09-24 15:45:57 +00:00
|
|
|
message.Content = "Record not found";
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-02-22 19:37:14 +00:00
|
|
|
{
|
2024-09-24 15:45:57 +00:00
|
|
|
message.Content = JsonSerializer.Serialize(result);
|
|
|
|
|
}
|
|
|
|
|
var states = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
var dictionaryItems = states.GetState("dictionary_items", "");
|
|
|
|
|
dictionaryItems += "\r\n\r\n" + args.Reason + ":\r\n" + message.Content + "\r\n";
|
|
|
|
|
states.SetState("dictionary_items", dictionaryItems);
|
2024-02-22 19:37:14 +00:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|