BotSharp/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs

120 lines
3 KiB
C#
Raw Normal View History

2023-08-09 21:49:55 +00:00
using BotSharp.Abstraction.Conversations.Models;
2023-08-26 04:41:01 +00:00
using BotSharp.Abstraction.Repositories;
2023-08-09 21:49:55 +00:00
using System.IO;
namespace BotSharp.Core.Conversations.Services;
/// <summary>
/// Maintain the conversation state
/// </summary>
public class ConversationStateService : IConversationStateService, IDisposable
{
2023-08-14 17:00:01 +00:00
private readonly ILogger _logger;
private readonly IServiceProvider _services;
2023-08-09 21:49:55 +00:00
private ConversationState _state;
2023-08-10 04:53:22 +00:00
private MyDatabaseSettings _dbSettings;
2023-08-09 21:49:55 +00:00
private string _conversationId;
private string _file;
2023-08-14 17:00:01 +00:00
public ConversationStateService(ILogger<ConversationStateService> logger,
IServiceProvider services,
MyDatabaseSettings dbSettings)
2023-08-09 21:49:55 +00:00
{
2023-08-14 17:00:01 +00:00
_logger = logger;
_services = services;
2023-08-10 04:53:22 +00:00
_dbSettings = dbSettings;
2023-08-09 21:49:55 +00:00
}
public void SetState(string name, string value)
{
2023-08-14 17:00:01 +00:00
var hooks = _services.GetServices<IConversationHook>();
string preValue = _state.ContainsKey(name) ? _state[name] : "";
if (!_state.ContainsKey(name) || _state[name] != value)
{
var currentValue = value;
_state[name] = currentValue;
2023-08-14 22:14:05 +00:00
_logger.LogInformation($"Set state: {name} = {value}");
2023-08-14 17:00:01 +00:00
foreach (var hook in hooks)
{
hook.OnStateChanged(name, preValue, currentValue).Wait();
}
}
2023-08-09 21:49:55 +00:00
}
2023-08-14 17:00:01 +00:00
public void SetConversation(string conversationId)
2023-08-09 21:49:55 +00:00
{
2023-08-14 17:00:01 +00:00
_conversationId = conversationId;
2023-08-09 21:49:55 +00:00
}
2023-08-14 17:00:01 +00:00
public ConversationState Load()
2023-08-09 21:49:55 +00:00
{
if (_state != null)
{
return _state;
}
_state = new ConversationState();
_file = GetStorageFile(_conversationId);
if (File.Exists(_file))
{
var dict = File.ReadAllLines(_file);
foreach (var line in dict)
{
2023-08-14 17:00:01 +00:00
_state[line.Split('=')[0]] = line.Split('=')[1];
2023-08-09 21:49:55 +00:00
}
}
2023-08-14 17:00:01 +00:00
_logger.LogInformation($"Loaded state {_conversationId}");
var hooks = _services.GetServices<IConversationHook>();
foreach (var hook in hooks)
{
hook.OnStateLoaded(_state).Wait();
}
2023-08-09 21:49:55 +00:00
return _state;
}
public void Save()
{
var states = new List<string>();
foreach (var dic in _state)
{
2023-08-14 17:00:01 +00:00
states.Add($"{dic.Key}={dic.Value}");
2023-08-09 21:49:55 +00:00
}
File.WriteAllLines(_file, states);
2023-08-14 17:00:01 +00:00
_logger.LogInformation($"Saved state {_conversationId}");
2023-08-09 21:49:55 +00:00
}
2023-08-15 15:41:34 +00:00
public void CleanState()
{
File.Delete(_file);
}
2023-08-09 21:49:55 +00:00
private string GetStorageFile(string conversationId)
{
2023-08-10 04:53:22 +00:00
var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
return Path.Combine(dir, "state.dict");
2023-08-09 21:49:55 +00:00
}
public string GetState(string name)
{
if (!_state.ContainsKey(name))
{
_state[name] = "";
}
return _state[name];
}
2023-08-14 17:00:01 +00:00
public void Dispose()
{
Save();
}
2023-08-09 21:49:55 +00:00
}