From 5eaa8f55c3b926b7856a5dc52973801638f46189 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Thu, 29 Sep 2022 09:37:26 +0300 Subject: [PATCH 1/8] passed texts, randomly choose next text --- src/FillInTheTextBot.Models/Request.cs | 2 + src/FillInTheTextBot.Models/Response.cs | 2 + .../TextsOverException.cs | 9 +++++ .../ConversationService.cs | 37 ++++++++++++------- .../DialogflowService.cs | 2 +- 5 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 src/FillInTheTextBot.Models/TextsOverException.cs diff --git a/src/FillInTheTextBot.Models/Request.cs b/src/FillInTheTextBot.Models/Request.cs index 8cdfcbb5..bc4926b8 100644 --- a/src/FillInTheTextBot.Models/Request.cs +++ b/src/FillInTheTextBot.Models/Request.cs @@ -35,6 +35,8 @@ public Request() public int NextTextIndex { get; set; } + public ICollection PassedTexts { get; set; } + public string ScopeKey { get; set; } public Appeal Appeal { get; set; } diff --git a/src/FillInTheTextBot.Models/Response.cs b/src/FillInTheTextBot.Models/Response.cs index 83a05195..097b4615 100644 --- a/src/FillInTheTextBot.Models/Response.cs +++ b/src/FillInTheTextBot.Models/Response.cs @@ -26,6 +26,8 @@ public Response() public int NextTextIndex { get; set; } + public ICollection PassedTexts { get; set; } + public string ScopeKey { get; set; } public IDictionary Emotions { get; set; } diff --git a/src/FillInTheTextBot.Models/TextsOverException.cs b/src/FillInTheTextBot.Models/TextsOverException.cs new file mode 100644 index 00000000..8732a36d --- /dev/null +++ b/src/FillInTheTextBot.Models/TextsOverException.cs @@ -0,0 +1,9 @@ +using System; + +namespace FillInTheTextBot.Models +{ + public class TextsOverException : Exception + { + + } +} \ No newline at end of file diff --git a/src/FillInTheTextBot.Services/ConversationService.cs b/src/FillInTheTextBot.Services/ConversationService.cs index 838a408f..02165ad6 100644 --- a/src/FillInTheTextBot.Services/ConversationService.cs +++ b/src/FillInTheTextBot.Services/ConversationService.cs @@ -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 GetResponseAsync(Request request) @@ -109,7 +111,10 @@ private async Task GetText(Request request, string startText, string t { using (Tracing.Trace()) { - var response = new Response(); + var response = new Response + { + PassedTexts = request.PassedTexts + }; if (string.IsNullOrEmpty(textKey)) { @@ -124,7 +129,15 @@ private async Task GetText(Request request, string startText, string t return response; } - textKey = GetTextKey(request, texts); + try + { + textKey = GetTextKey(request.PassedTexts, texts); + response.PassedTexts.Add(textKey); + } + catch (TextsOverException e) + { + textKey = "texts-over"; + } } } @@ -146,21 +159,19 @@ private async Task GetText(Request request, string startText, string t } } - private string GetTextKey(Request request, string[] texts) + private string GetTextKey(ICollection passedTexts, string[] texts) { - string textKey; - - var index = request.NextTextIndex++; - - if (index >= texts.Length) - { - textKey = "texts-over"; - } - else + if (passedTexts.Count == texts.Length) { - textKey = texts[index]; + throw new TextsOverException(); } + var candidateTexts = texts.Except(passedTexts).ToList(); + + var candidateIndex = _random.Next(candidateTexts.Count); + + var textKey = candidateTexts[candidateIndex]; + return textKey; } diff --git a/src/FillInTheTextBot.Services/DialogflowService.cs b/src/FillInTheTextBot.Services/DialogflowService.cs index c7b3315d..e1349084 100644 --- a/src/FillInTheTextBot.Services/DialogflowService.cs +++ b/src/FillInTheTextBot.Services/DialogflowService.cs @@ -52,7 +52,7 @@ public DialogflowService( {InternalModels.Source.Marusia, DefaultWelcomeEventResolve} }; } - + //TODO: remove requiredContext public async Task GetResponseAsync(string text, string sessionId, string requiredContext = null) { var request = new InternalModels.Request From ba01d32de64eaec421be6083bf14c94508f30158 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sat, 1 Oct 2022 21:13:47 +0300 Subject: [PATCH 2/8] fill passed texts array from user state --- .../YandexService.cs | 23 ++++++++----------- src/FillInTheTextBot.Models/Response.cs | 3 ++- .../ConversationService.cs | 6 ++--- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/FillInTheTextBot.Messengers.Yandex/YandexService.cs b/src/FillInTheTextBot.Messengers.Yandex/YandexService.cs index 62b4b5bf..13116897 100644 --- a/src/FillInTheTextBot.Messengers.Yandex/YandexService.cs +++ b/src/FillInTheTextBot.Messengers.Yandex/YandexService.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Threading.Tasks; using FillInTheTextBot.Services; +using FillInTheTextBot.Services.Extensions; using Microsoft.Extensions.Logging; using Yandex.Dialogs.Models; using Yandex.Dialogs.Models.Input; @@ -30,26 +31,21 @@ 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); - } - + input.TryGetFromUserState(Models.Response.NextTextIndexStorageKey, out long 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.TryGetFromUserState(Models.Response.PassedTextsKey, out object passedTexts); + request.PassedTexts = passedTexts.ToString().Deserialize(); - request.RequiredContexts.AddRange(contexts); - } + if (request.NewSession != true) return request; + + var contexts = GetContexts(input); + request.RequiredContexts.AddRange(contexts); return request; } @@ -75,7 +71,8 @@ protected override Task 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.AddToUserState(Models.Response.PassedTextsKey, response.PassedTexts); output.AddToSessionState(Models.Response.ScopeStorageKey, response.ScopeKey); diff --git a/src/FillInTheTextBot.Models/Response.cs b/src/FillInTheTextBot.Models/Response.cs index 097b4615..54cfe6ec 100644 --- a/src/FillInTheTextBot.Models/Response.cs +++ b/src/FillInTheTextBot.Models/Response.cs @@ -6,6 +6,7 @@ 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 Response() { @@ -26,7 +27,7 @@ public Response() public int NextTextIndex { get; set; } - public ICollection PassedTexts { get; set; } + public IList PassedTexts { get; set; } public string ScopeKey { get; set; } diff --git a/src/FillInTheTextBot.Services/ConversationService.cs b/src/FillInTheTextBot.Services/ConversationService.cs index 02165ad6..ba4d1cb1 100644 --- a/src/FillInTheTextBot.Services/ConversationService.cs +++ b/src/FillInTheTextBot.Services/ConversationService.cs @@ -33,8 +33,8 @@ public async Task GetResponseAsync(Request request) Text = dialog?.Response, Finished = dialog?.EndConversation ?? false, Buttons = dialog?.Buttons, - ScopeKey = dialog?.ScopeKey - + ScopeKey = dialog?.ScopeKey, + PassedTexts = request.PassedTexts.ToList() }; var resetTextIndex = string.Empty; @@ -113,7 +113,7 @@ private async Task GetText(Request request, string startText, string t { var response = new Response { - PassedTexts = request.PassedTexts + PassedTexts = request.PassedTexts.ToList() }; if (string.IsNullOrEmpty(textKey)) From 5d4ff186912833a2894babdcf28cf4a93367901e Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sat, 1 Oct 2022 21:33:32 +0300 Subject: [PATCH 3/8] choose first text, when there is no any passed texts --- src/FillInTheTextBot.Services/ConversationService.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/FillInTheTextBot.Services/ConversationService.cs b/src/FillInTheTextBot.Services/ConversationService.cs index ba4d1cb1..194d6e47 100644 --- a/src/FillInTheTextBot.Services/ConversationService.cs +++ b/src/FillInTheTextBot.Services/ConversationService.cs @@ -166,6 +166,11 @@ private string GetTextKey(ICollection passedTexts, string[] texts) throw new TextsOverException(); } + if (passedTexts.Count == 0) + { + return texts.FirstOrDefault(); + } + var candidateTexts = texts.Except(passedTexts).ToList(); var candidateIndex = _random.Next(candidateTexts.Count); From 2cd1894677487a5e734bf44828ed134640a0e8ab Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sat, 1 Oct 2022 21:33:46 +0300 Subject: [PATCH 4/8] clear passed texts when required --- src/FillInTheTextBot.Services/ConversationService.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/FillInTheTextBot.Services/ConversationService.cs b/src/FillInTheTextBot.Services/ConversationService.cs index 194d6e47..0e57aca1 100644 --- a/src/FillInTheTextBot.Services/ConversationService.cs +++ b/src/FillInTheTextBot.Services/ConversationService.cs @@ -44,6 +44,7 @@ public async Task GetResponseAsync(Request request) if (isGotResetParameter && string.Equals(resetTextIndex, bool.TrueString, StringComparison.InvariantCultureIgnoreCase)) { request.NextTextIndex = 0; + request.PassedTexts.Clear(); } if (string.Equals(dialog?.Action, "GetText")) @@ -137,6 +138,7 @@ private async Task GetText(Request request, string startText, string t catch (TextsOverException e) { textKey = "texts-over"; + response.PassedTexts.Clear(); } } } From dabc38b3477e4b61d2356f31ab814af871f93b0c Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sat, 1 Oct 2022 21:33:58 +0300 Subject: [PATCH 5/8] greater or equals --- src/FillInTheTextBot.Services/ConversationService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FillInTheTextBot.Services/ConversationService.cs b/src/FillInTheTextBot.Services/ConversationService.cs index 0e57aca1..3b456f21 100644 --- a/src/FillInTheTextBot.Services/ConversationService.cs +++ b/src/FillInTheTextBot.Services/ConversationService.cs @@ -163,7 +163,7 @@ private async Task GetText(Request request, string startText, string t private string GetTextKey(ICollection passedTexts, string[] texts) { - if (passedTexts.Count == texts.Length) + if (passedTexts.Count >= texts.Length) { throw new TextsOverException(); } From 5bcc2bc9b008bcbb427cc4965ea873260e9bdcff Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sat, 1 Oct 2022 23:38:24 +0300 Subject: [PATCH 6/8] remove NextTextIndex usages --- src/FillInTheTextBot.Messengers.Yandex/YandexService.cs | 5 ----- src/FillInTheTextBot.Services/ConversationService.cs | 3 --- 2 files changed, 8 deletions(-) diff --git a/src/FillInTheTextBot.Messengers.Yandex/YandexService.cs b/src/FillInTheTextBot.Messengers.Yandex/YandexService.cs index 13116897..6c7854d9 100644 --- a/src/FillInTheTextBot.Messengers.Yandex/YandexService.cs +++ b/src/FillInTheTextBot.Messengers.Yandex/YandexService.cs @@ -33,9 +33,6 @@ protected override Models.Request Before(InputModel input) input.TryGetFromUserState(Models.Request.IsOldUserKey, out bool isOldUser); request.IsOldUser = isOldUser; - input.TryGetFromUserState(Models.Response.NextTextIndexStorageKey, out long nextTextIndex); - request.NextTextIndex = Convert.ToInt32(nextTextIndex); - input.TryGetFromSessionState(Models.Response.ScopeStorageKey, out string scopeKey); request.ScopeKey = scopeKey; @@ -70,8 +67,6 @@ protected override Task AfterAsync(InputModel input, Models.Respons output.AddToUserState(Models.Request.IsOldUserKey, true); - output.AddToUserState(Models.Response.NextTextIndexStorageKey, response.NextTextIndex); - output.AddToUserState(Models.Response.PassedTextsKey, response.PassedTexts); output.AddToSessionState(Models.Response.ScopeStorageKey, response.ScopeKey); diff --git a/src/FillInTheTextBot.Services/ConversationService.cs b/src/FillInTheTextBot.Services/ConversationService.cs index 3b456f21..b59aa466 100644 --- a/src/FillInTheTextBot.Services/ConversationService.cs +++ b/src/FillInTheTextBot.Services/ConversationService.cs @@ -43,7 +43,6 @@ public async Task GetResponseAsync(Request request) if (isGotResetParameter && string.Equals(resetTextIndex, bool.TrueString, StringComparison.InvariantCultureIgnoreCase)) { - request.NextTextIndex = 0; request.PassedTexts.Clear(); } @@ -61,8 +60,6 @@ public async Task 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); From ff5cd6115c14edddc055f83e657101e742d8eedd Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Wed, 5 Oct 2022 10:55:13 +0300 Subject: [PATCH 7/8] CurrentText --- .../YandexService.cs | 5 +++++ src/FillInTheTextBot.Models/Request.cs | 2 ++ src/FillInTheTextBot.Models/Response.cs | 5 ++++- .../ConversationService.cs | 15 +++++++++++---- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/FillInTheTextBot.Messengers.Yandex/YandexService.cs b/src/FillInTheTextBot.Messengers.Yandex/YandexService.cs index 6c7854d9..bc3c5247 100644 --- a/src/FillInTheTextBot.Messengers.Yandex/YandexService.cs +++ b/src/FillInTheTextBot.Messengers.Yandex/YandexService.cs @@ -36,6 +36,9 @@ protected override Models.Request Before(InputModel input) input.TryGetFromSessionState(Models.Response.ScopeStorageKey, out string scopeKey); request.ScopeKey = scopeKey; + input.TryGetFromSessionState(Models.Response.CurrentTextKey, out string currentText); + request.CurrentText = currentText; + input.TryGetFromUserState(Models.Response.PassedTextsKey, out object passedTexts); request.PassedTexts = passedTexts.ToString().Deserialize(); @@ -67,6 +70,8 @@ protected override Task AfterAsync(InputModel input, Models.Respons output.AddToUserState(Models.Request.IsOldUserKey, true); + output.AddToSessionState(Models.Response.CurrentTextKey, response.CurrentText); + output.AddToUserState(Models.Response.PassedTextsKey, response.PassedTexts); output.AddToSessionState(Models.Response.ScopeStorageKey, response.ScopeKey); diff --git a/src/FillInTheTextBot.Models/Request.cs b/src/FillInTheTextBot.Models/Request.cs index bc4926b8..0c46cb56 100644 --- a/src/FillInTheTextBot.Models/Request.cs +++ b/src/FillInTheTextBot.Models/Request.cs @@ -35,6 +35,8 @@ public Request() public int NextTextIndex { get; set; } + public string CurrentText { get; set; } + public ICollection PassedTexts { get; set; } public string ScopeKey { get; set; } diff --git a/src/FillInTheTextBot.Models/Response.cs b/src/FillInTheTextBot.Models/Response.cs index 54cfe6ec..984df333 100644 --- a/src/FillInTheTextBot.Models/Response.cs +++ b/src/FillInTheTextBot.Models/Response.cs @@ -7,6 +7,7 @@ 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() { @@ -27,10 +28,12 @@ public Response() public int NextTextIndex { get; set; } + public string CurrentText { get; set; } + public IList PassedTexts { get; set; } public string ScopeKey { get; set; } - + public IDictionary Emotions { get; set; } } } \ No newline at end of file diff --git a/src/FillInTheTextBot.Services/ConversationService.cs b/src/FillInTheTextBot.Services/ConversationService.cs index b59aa466..4683f6f6 100644 --- a/src/FillInTheTextBot.Services/ConversationService.cs +++ b/src/FillInTheTextBot.Services/ConversationService.cs @@ -67,7 +67,7 @@ public async Task GetResponseAsync(Request request) response.Text = texts.Text; response.AlternativeText = texts.AlternativeText; - TrySetSavedText(request.SessionId, dialog, texts); + TrySetSavedText(request.SessionId, dialog, texts, response); return response; } @@ -130,11 +130,16 @@ private async Task GetText(Request request, string startText, string t try { textKey = GetTextKey(request.PassedTexts, texts); - response.PassedTexts.Add(textKey); + + response.CurrentText = textKey; } catch (TextsOverException e) { - textKey = "texts-over"; + if (!string.IsNullOrEmpty(request.CurrentText)) + { + textKey = "texts-over"; + } + response.PassedTexts.Clear(); } } @@ -287,7 +292,7 @@ private ICollection