Example #1
0
        public async Task <Answer <T> > AskAsync <T>(string question, List <QuestionAnswerAlternative <T> > alternatives, int millisecondsTimeout = 3000)
        {
            if (_settingAgent.GetSetting("Muted", false))
            {
                //TODO: Show dialog where the anser can be entered. The dialog should have the same timeout as the default answer.
                //There should be one answer for each alternative, up to a limit, where there will be a drop down.
                //If there are no set alternatives, there should be a text input box for answers.
                //Also look at the other question functions: AskYesNoAsync, AskFolderAsync and AskStringAsync. And align them as well.
                return(GetDefaultAnswer(alternatives));
            }

            try
            {
                using (var listenerAgent = new ListenerAgent <T>(_eventHub, alternatives))
                {
                    listenerAgent.HeardSomethingEvent += EventHub_HeardSomethingEvent;

                    var listenId = Guid.NewGuid();
                    _eventHub.InvokeStartListeningEvent(listenId);

                    await _talkAgent.SayAsync(question);

                    listenerAgent.StartListening();

                    var r = await Task.Factory.StartNew(() =>
                    {
                        if (!_responseEvent.WaitOne(millisecondsTimeout))
                        {
                            return(GetDefaultAnswer(alternatives));
                        }

                        var selectedAlternative = alternatives.First(x => x.Phrases.Any(y => y == _responsePhrase));
                        return(new Answer <T>(selectedAlternative.Response));
                    });

                    _eventHub.InvokeDoneListeningEvent(listenId);

                    return(r);
                }
            }
            catch (InvalidOperationException exception)
            {
                CompositeRoot.Instance.TalkAgent.SayAsync("Oups, now we have problems! " + exception.Message);
                _settingAgent.SetSetting("Muted", true);
            }

            return(GetDefaultAnswer(alternatives));
        }