BestFriendService.Bot.SendMessageAndGetResponseFromBot C# (CSharp) Method

SendMessageAndGetResponseFromBot() public method

public SendMessageAndGetResponseFromBot ( string message ) : IAsyncOperation
message string
return IAsyncOperation
        public IAsyncOperation<string> SendMessageAndGetResponseFromBot(string message)
        {
            return Task.Run<string>(async () =>
            {
                if (key == "<INSERT YOUR API KEY HERE>" || botId == "<INSERT YOUR BOT NUMBER HERE>")
                {
                    return "Please update the API key and/or botId in Bot.cs in order to talk to me!";
                }

                string msg = "";

                try
                {
                    HttpClient client = new HttpClient();
                    string uri = "http://www.personalityforge.com/api/chat/?apiKey=" + key +
                                 "&chatBotID=" + botId +
                                 "&message=" + message +
                                 "&externalID=demo";
                    string response = await client.GetStringAsync(new Uri(uri));

                    Match match = Regex.Match(response, "{\"success\".*}");
                    string json = match.ToString();

                    var jObject = JsonObject.Parse(json);
                    msg = jObject["message"].GetObject()["message"].GetString();

                }
                catch (Exception e)
                {
                    // no op
                    Debug.WriteLine(e.Message);
                }

                return msg;
            }).AsAsyncOperation();
        }

Usage Example

Example #1
0
        protected override async void OnRun(IBackgroundTaskInstance taskInstance)
        {
            this.serviceDeferral = taskInstance.GetDeferral();
            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            VoiceCommandUserMessage userMessage;
            VoiceCommandResponse response;
            try
            {
                voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                voiceServiceConnection.VoiceCommandCompleted += VoiceCommandCompleted;
                VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                switch (voiceCommand.CommandName)
                {
                    case "where":

                        var city = voiceCommand.Properties["city"][0];

                        var imageFile = await GenerateWideIconWithCity(city);
                        var localFolder = ApplicationData.Current.LocalFolder;
                        StorageFile cityIcon = await localFolder.GetFileAsync(imageFile);

                        var contentTiles = new List<VoiceCommandContentTile>();
                        var tile1 = new VoiceCommandContentTile();
                        tile1.ContentTileType = VoiceCommandContentTileType.TitleWith280x140IconAndText;
                        tile1.AppLaunchArgument = city;
                        tile1.Image = cityIcon;
                        contentTiles.Add(tile1);

                        userMessage = new VoiceCommandUserMessage()
                        {
                            DisplayMessage = "Here you go Best Friend, it's " + city,
                            SpokenMessage = "Here you go Best Friend, it's " + city
                        };

                        response = VoiceCommandResponse.CreateResponse(userMessage, contentTiles);
                        await voiceServiceConnection.ReportSuccessAsync(response);

                        break;

                    
                    case "sendMessageInCanvas":
                        var message = voiceCommand.Properties["message"][0];
                        var bot = new Bot();
                        string firstResponse = await bot.SendMessageAndGetResponseFromBot(message);

                        var responseMessage = new VoiceCommandUserMessage();
                        responseMessage.DisplayMessage = responseMessage.SpokenMessage = "Your Best Friend says \"" + firstResponse + "\"";
                        
                        response = VoiceCommandResponse.CreateResponse(responseMessage);
                        await voiceServiceConnection.ReportSuccessAsync(response);

                        break;

                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            finally
            {
                if (this.serviceDeferral != null)
                {
                    //Complete the service deferral
                    this.serviceDeferral.Complete();
                }
            }
        }