Beispiel #1
0
        private void SwitchCulture(string cultureName)
        {
            var languagesManager = new LanguagesManager();

            cultureName         = languagesManager.CheckAndReturnAppropriateCulture(cultureName);
            CulturedBot.Culture = new CultureInfo(cultureName);
            OpportunityConfidenceValue.Culture = new CultureInfo(cultureName);
            OpportunityStatusValue.Culture     = new CultureInfo(cultureName);

            if (!_services.LuisServices.ContainsKey(CulturedBot.LuisAppName))
            {
                throw new System.ArgumentException($"The bot configuration does not contain a service type of `luis` with the id `{CulturedBot.LuisAppName}`.");
            }
        }
Beispiel #2
0
        private async System.Threading.Tasks.Task HandleIncomingBotMessagesAsync(IList <Activity> botReplies, string callSid)
        {
            var voiceResponse    = new VoiceResponse();
            var says             = new StringBuilder();
            var forwardingNumber = string.Empty;
            var forward          = false;
            var error            = false;
            var errorMessage     = string.Empty;

            foreach (var activity in botReplies)
            {
                var languagesManager = new LanguagesManager();
                var localeCulture    = languagesManager.CheckAndReturnAppropriateCulture(activity.Locale);
                CultureInfo.CurrentCulture = new CultureInfo(localeCulture);

                //Using TTS to repond to the caller
                var ttsResponse = await System.Threading.Tasks.Task.Run(() =>
                                                                        _textToSpeech.TransformTextToSpeechAsync(activity.Text, CultureInfo.CurrentCulture.Name));

                var wavGuid = Guid.NewGuid();
                var pathToAudioDirectory = _hostingEnvironment.WebRootPath + "/audio";
                var pathCombined         = Path.Combine(pathToAudioDirectory, $"{ wavGuid }.wav");
                var formatConverter      = new FormatConvertor();
                await formatConverter.TurnAudioStreamToFile(ttsResponse, pathCombined);

                voiceResponse.Play(new Uri($"{_directlineConfig.Host}audio/{wavGuid}.wav"));

                if (activity.Entities != null)
                {
                    foreach (var entity in activity.Entities)
                    {
                        forward          = entity.Properties.TryGetValue("forward", out var numberJToken);
                        forwardingNumber = forward ? numberJToken.ToString() : string.Empty;

                        error = entity.Properties.TryGetValue("error", out var errorMessageJToken);
                        if (error)
                        {
                            break;
                        }
                    }
                }
            }

            if (error)
            {
                voiceResponse.Hangup();
            }
            else if (forward)
            {
                voiceResponse.Dial(number: forwardingNumber);
            }
            else
            {
                voiceResponse.Gather(
                    input: new List <Gather.InputEnum> {
                    Gather.InputEnum.Speech
                },
                    language: CultureInfo.CurrentCulture.Name,
                    action: new Uri($"{_directlineConfig.Host}api/voice/send"),
                    method: HttpMethod.Get,
                    speechTimeout: "auto",
                    hints: _hints
                    );
            }

            var xmlFileName        = Guid.NewGuid();
            var pathToXMLDirectory = _hostingEnvironment.WebRootPath + "/xml";

            System.IO.File.WriteAllText($"{pathToXMLDirectory}/{xmlFileName}.xml", voiceResponse.ToString());

            CallResource.Update(
                method: HttpMethod.Get,
                url: new Uri($"{_directlineConfig.Host}xml/{xmlFileName}.xml"),
                pathSid: callSid
                );
        }