/// <summary> /// This method implements retry logic to get a word (specific method implemented in base class) /// This method builds the response dictionary for the word and alexa response /// This method ensures caching for between session logic /// The WordOfTheDay and AnotherWord strategies implement this abstract class with different responses and word retrival logic /// </summary> /// <param name="alexaRequest"></param> /// <returns></returns> public AlexaResponse HandleAlexaRequest(AlexaRequestPayload alexaRequest) { // initialize variables used in case of retry Word word = null; // what word will we get - should never be null int retryCounter = 0; Dictionary <WordEnum, string> wordResponseDictionary = null; // this method must return populated dictionary string wordResponse = null; // get the word // get the response dictionary from the word // handle retry errors - get another word from the word service if invalid response dictionary try { // repeat api call and parse while (wordResponse == null) { // Get the word - implemented in base class if (retryCounter == 0) { word = GetWord(); } // Get random word - there was an issue with the first word else { word = _wordService.GetRandomWord(); } try { wordResponseDictionary = _dictionaryService.GetWordDictionaryFromString(word.WordName); } catch (Exception exception) { return(new AlexaResponse("Unable to build response dictionary for the word: " + word + " " + exception.Message)); } wordResponse = wordResponseDictionary[WordEnum.Word]; retryCounter += 1; } AlexaResponse alexaResponse = BuildAlexaResponse(alexaRequest, wordResponseDictionary); // assign word to request for caching the request between intents alexaRequest.Session.Attributes = new AlexaRequestPayload.SessionCustomAttributes(); alexaRequest.Session.Attributes.LastWord = word.WordName; // use the word from the db as it is saved in intent slots alexaRequest.Session.Attributes.LastWordDefinition = wordResponseDictionary[WordEnum.WordDefinition]; // cache the request and its above added session attributes _cacheService.CacheAlexaRequest(alexaRequest); return(alexaResponse); } catch (Exception exception) { // todo - log the exception return(new AlexaWordErrorResponse().GenerateCustomError()); } }