Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions src/FillInTheTextBot.Messengers.Yandex/YandexService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using FillInTheTextBot.Services;
using FillInTheTextBot.Services.Extensions;
using Microsoft.Extensions.Logging;
using Yandex.Dialogs.Models;
using Yandex.Dialogs.Models.Input;
Expand Down Expand Up @@ -30,26 +32,22 @@ protected override Models.Request Before(InputModel input)
var request = input.ToRequest();

input.TryGetFromUserState(Models.Request.IsOldUserKey, out bool isOldUser);

request.IsOldUser = isOldUser;

if (input.TryGetFromUserState(Models.Response.NextTextIndexStorageKey, out object nextTextIndex) != true)
{
input.TryGetFromApplicationState(Models.Response.NextTextIndexStorageKey, out nextTextIndex);
}

request.NextTextIndex = Convert.ToInt32(nextTextIndex);

input.TryGetFromSessionState(Models.Response.ScopeStorageKey, out string scopeKey);

request.ScopeKey = scopeKey;

if (request.NewSession == true)
{
var contexts = GetContexts(input);
input.TryGetFromSessionState(Models.Response.CurrentTextKey, out string currentText);
request.CurrentText = currentText;

request.RequiredContexts.AddRange(contexts);
}
input.TryGetFromUserState(Models.Response.PassedTextsKey, out object passedTexts);
request.PassedTexts = passedTexts.ToString().Deserialize<string[]>()
.Where(s => !string.IsNullOrEmpty(s)).ToList();

if (request.NewSession != true) return request;

var contexts = GetContexts(input);
request.RequiredContexts.AddRange(contexts);

return request;
}
Expand All @@ -74,8 +72,9 @@ protected override Task<OutputModel> AfterAsync(InputModel input, Models.Respons

output.AddToUserState(Models.Request.IsOldUserKey, true);

output.AddToUserState(Models.Response.NextTextIndexStorageKey, response.NextTextIndex);
output.AddToApplicationState(Models.Response.NextTextIndexStorageKey, response.NextTextIndex);
output.AddToSessionState(Models.Response.CurrentTextKey, response.CurrentText);

output.AddToUserState(Models.Response.PassedTextsKey, response.PassedTexts);

output.AddToSessionState(Models.Response.ScopeStorageKey, response.ScopeKey);

Expand Down
4 changes: 4 additions & 0 deletions src/FillInTheTextBot.Models/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public Request()

public int NextTextIndex { get; set; }

public string CurrentText { get; set; }

public ICollection<string> PassedTexts { get; set; }

public string ScopeKey { get; set; }

public Appeal Appeal { get; set; }
Expand Down
8 changes: 7 additions & 1 deletion src/FillInTheTextBot.Models/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class Response
{
public static string NextTextIndexStorageKey => nameof(NextTextIndex).ToUpper();
public static string ScopeStorageKey => nameof(ScopeKey).ToUpper();
public static string PassedTextsKey => nameof(PassedTexts).ToUpper();
public static string CurrentTextKey => nameof(CurrentText).ToUpper();

public Response()
{
Expand All @@ -26,8 +28,12 @@ public Response()

public int NextTextIndex { get; set; }

public string CurrentText { get; set; }

public IList<string> PassedTexts { get; set; }

public string ScopeKey { get; set; }

public IDictionary<string, string> Emotions { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/FillInTheTextBot.Models/TextsOverException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace FillInTheTextBot.Models
{
public class TextsOverException : Exception
{

}
}
59 changes: 41 additions & 18 deletions src/FillInTheTextBot.Services/ConversationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ public class ConversationService : IConversationService

private readonly IDialogflowService _dialogflowService;
private readonly IRedisCacheService _cache;
private readonly Random _random;

public ConversationService(IDialogflowService dialogflowService, IRedisCacheService cache)
{
_dialogflowService = dialogflowService;
_cache = cache;
_random = Random.Shared;
}

public async Task<Response> GetResponseAsync(Request request)
Expand All @@ -31,8 +33,9 @@ public async Task<Response> GetResponseAsync(Request request)
Text = dialog?.Response,
Finished = dialog?.EndConversation ?? false,
Buttons = dialog?.Buttons,
ScopeKey = dialog?.ScopeKey

ScopeKey = dialog?.ScopeKey,
CurrentText = request.CurrentText,
PassedTexts = request.PassedTexts.ToList()
};

var resetTextIndex = string.Empty;
Expand All @@ -41,7 +44,7 @@ public async Task<Response> GetResponseAsync(Request request)

if (isGotResetParameter && string.Equals(resetTextIndex, bool.TrueString, StringComparison.InvariantCultureIgnoreCase))
{
request.NextTextIndex = 0;
request.PassedTexts.Clear();
}

if (string.Equals(dialog?.Action, "GetText"))
Expand All @@ -58,16 +61,14 @@ public async Task<Response> GetResponseAsync(Request request)

response.Emotions = GetEmotions(dialog);

response.NextTextIndex = request.NextTextIndex;

response.Text = GetResponseText(request.Appeal, response.Text);
response.Buttons = GetButtonsFromPayload(response.Buttons, dialog?.Payload, request.Source);

var texts = TryAddReplacementsFromPayload(dialog?.Payload, request.Source, response.Text);
response.Text = texts.Text;
response.AlternativeText = texts.AlternativeText;

TrySetSavedText(request.SessionId, dialog, texts);
TrySetSavedText(request.SessionId, dialog, texts, response);

return response;
}
Expand Down Expand Up @@ -109,7 +110,10 @@ private async Task<Response> GetText(Request request, string startText, string t
{
using (Tracing.Trace())
{
var response = new Response();
var response = new Response
{
PassedTexts = request.PassedTexts.ToList()
};

if (string.IsNullOrEmpty(textKey))
{
Expand All @@ -124,7 +128,21 @@ private async Task<Response> GetText(Request request, string startText, string t
return response;
}

textKey = GetTextKey(request, texts);
try
{
textKey = GetTextKey(request.PassedTexts, texts);

response.CurrentText = textKey;
}
catch (TextsOverException e)
{
if (!string.IsNullOrEmpty(request.CurrentText))
{
textKey = "texts-over";
}

response.PassedTexts.Clear();
}
}
}

Expand All @@ -146,21 +164,24 @@ private async Task<Response> GetText(Request request, string startText, string t
}
}

private string GetTextKey(Request request, string[] texts)
private string GetTextKey(ICollection<string> passedTexts, string[] texts)
{
string textKey;

var index = request.NextTextIndex++;

if (index >= texts.Length)
if (passedTexts.Count >= texts.Length)
{
textKey = "texts-over";
throw new TextsOverException();
}
else

if (passedTexts.Count == 0)
{
textKey = texts[index];
return texts.FirstOrDefault();
}

var candidateTexts = texts.Except(passedTexts).ToList();

var candidateIndex = _random.Next(candidateTexts.Count);

var textKey = candidateTexts[candidateIndex];

return textKey;
}

Expand Down Expand Up @@ -272,7 +293,7 @@ private ICollection<Button> GetButtonsFromPayload(ICollection<Button> responseBu
}
}

private void TrySetSavedText(string sessionId, Dialog dialog, Texts texts)
private void TrySetSavedText(string sessionId, Dialog dialog, Texts texts, Response response)
{
if (dialog?.ParametersIncomplete != true && string.Equals(dialog?.Action ?? string.Empty, "saveToRepeat", StringComparison.InvariantCultureIgnoreCase))
{
Expand All @@ -283,6 +304,8 @@ private void TrySetSavedText(string sessionId, Dialog dialog, Texts texts)
};

_dialogflowService.SetContextAsync(sessionId, "savedText", 5, parameters).Forget();

response.PassedTexts.Add(response.CurrentText);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/FillInTheTextBot.Services/DialogflowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public DialogflowService(
{InternalModels.Source.Marusia, DefaultWelcomeEventResolve}
};
}

//TODO: remove requiredContext
public async Task<InternalModels.Dialog> GetResponseAsync(string text, string sessionId, string requiredContext = null)
{
var request = new InternalModels.Request
Expand Down