示例#1
0
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task <HttpResponseMessage> Post([FromBody] Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                ConnectorClient connector   = new ConnectorClient(new Uri(activity.ServiceUrl));
                StateClient     stateClient = activity.GetStateClient();
                BotData         userData    = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);

                string text     = (activity.Text ?? string.Empty).ToLower();
                int    length   = text.Length;
                string replyStr = string.Empty;

                string name     = String.Empty;
                string userId   = activity.From.Id.ToString();
                bool   loggedIn = false;

                try
                {
                    loggedIn = await authoriseUser(activity, connector, text, userId);
                }
                catch (MobileServiceInvalidOperationException e)
                {
                    System.Diagnostics.Debug.WriteLine($"ERROR! : {e.Message}");
                }

                if (!loggedIn)
                {
                    var responseOut = Request.CreateResponse(HttpStatusCode.OK);
                    return(responseOut);
                }

                Accounts userAccount;
                try
                {
                    userAccount = await getUserAccount(name);
                }
                catch (MobileServiceInvalidOperationException e)
                {
                    System.Diagnostics.Debug.WriteLine($"ERROR! : {e.Message}");
                    userAccount = new Accounts();
                }


                string responderName = userData.GetProperty <string>("Responder");
                System.Diagnostics.Debug.WriteLine(userData.GetProperty <string>("Responder"));
                if (responderName == null)
                {
                    responderName = "boring";
                    userData.SetProperty <string>("Responder", responderName);
                }
                Responder responder = Responder.GetResponder(responderName);

                //If block for user input
                if (text.StartsWith("clear"))
                {
                    await stateClient.BotState.DeleteStateForUserAsync(activity.ChannelId, activity.From.Id);

                    Auth auth = await getUserAuth(userId);

                    await AzureManager.AzureManagerInstance.DeleteAuths(auth);

                    replyStr = responder.affirmative();
                }
                if (text.StartsWith("set"))
                {
                    Auth auth = await getUserAuth(userId);

                    string[] input = text.Substring(4).Split(':');
                    if (input.Length != 2)
                    {
                        replyStr = responder.unknown();
                    }
                    else
                    {
                        auth.Name     = input[0];
                        auth.Password = input[1];
                        replyStr      = responder.affirmative();
                    }
                }
                else if (text.StartsWith("use"))
                {
                    //Sets the users preferred responder
                    //Does not check if a responder exists with that name
                    userData.SetProperty <string>("Responder", text.Substring(4));
                    responderName = userData.GetProperty <string>("Responder");
                    responder     = Responder.GetResponder(responderName);
                    await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);

                    Activity greetingCard = await buildCardResponse(activity, $"Now using {responder.getName()}", "", responder.greeting(), responder.neutralImageUrl());

                    await connector.Conversations.SendToConversationAsync(greetingCard);
                }
                else if (text.StartsWith("quote"))
                {
                    string   quote     = getRandomQuote(responder.getName());
                    Activity quoteCard = await buildCardResponse(activity, "", "", quote, responder.neutralImageUrl());

                    await connector.Conversations.SendToConversationAsync(quoteCard);
                }
                else if (text.StartsWith("balance"))
                {
                    Activity balanceCardReply = await getFullBalance(userAccount, responder, activity);

                    await connector.Conversations.SendToConversationAsync(balanceCardReply);
                }
                else if (text.StartsWith("cheque"))
                {
                    try
                    {
                        string change      = text.Substring(7);
                        double changeValue = Double.Parse(change);
                        userAccount.Cheque = userAccount.Cheque + changeValue;

                        await AzureManager.AzureManagerInstance.UpdateAccounts(userAccount);

                        string imageUrl;
                        if (changeValue >= 0.0)
                        {
                            imageUrl = responder.positiveImageUrl();
                        }
                        else
                        {
                            imageUrl = responder.negativeImageUrl();
                        }

                        Activity confirmCard = await buildCardResponse(activity, responder.affirmative(), "Balance updated", $"{userAccount.Cheque}", imageUrl);

                        await connector.Conversations.SendToConversationAsync(confirmCard);
                    }
                    catch (Exception e)
                    {
                        Activity unkownCard = await buildCardResponse(activity, "", "", responder.unknown(), responder.negativeImageUrl());

                        await connector.Conversations.SendToConversationAsync(unkownCard);
                    }
                }
                else if (text.StartsWith("savings"))
                {
                    string change = text.Substring(8);
                    try
                    {
                        double changeValue = Double.Parse(change);
                        userAccount.Savings = userAccount.Savings + changeValue;

                        await AzureManager.AzureManagerInstance.UpdateAccounts(userAccount);

                        string imageUrl;
                        if (changeValue >= 0.0)
                        {
                            imageUrl = responder.positiveImageUrl();
                        }
                        else
                        {
                            imageUrl = responder.negativeImageUrl();
                        }

                        Activity confirmCard = await buildCardResponse(activity, responder.affirmative(), "Balance updated", $"{userAccount.Savings}", imageUrl);

                        await connector.Conversations.SendToConversationAsync(confirmCard);
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine($"ERROR! : {e.Message}");
                        System.Diagnostics.Debug.WriteLine($"ERROR! : {change}");
                        Activity unkownCard = await buildCardResponse(activity, "", "", responder.unknown(), responder.negativeImageUrl());

                        await connector.Conversations.SendToConversationAsync(unkownCard);
                    }
                }
                else
                {
                    Activity unkownCard = await buildCardResponse(activity, "", "", responder.unknown(), responder.negativeImageUrl());

                    await connector.Conversations.SendToConversationAsync(unkownCard);
                }


                if (replyStr.Length > 0)
                {
                    // return our reply to the user
                    Activity reply = activity.CreateReply(replyStr);
                    await connector.Conversations.ReplyToActivityAsync(reply);
                }
            }
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);

            return(response);
        }