예제 #1
0
        public ActionResult Index(AlexaRoot root)
        {
            //var input = new JavaScriptSerializer().DeserializeObject(json);

            bool   success  = false;
            string speech   = null;
            bool   inListen = false;
            object card     = null;

            if (root == null || root.Request == null || !IsTimestampValid(root.Request.Timestamp))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            User user = null;

            var session = root.Session;

            if (session != null)
            {
                if (session.Attributes != null)
                {
                    string v;
                    if (session.Attributes.TryGetValue("inListen", out v) && v == "true")
                    {
                        inListen = true;
                    }
                }

                if (session.User != null && session.User.AccessToken != null)
                {
                    using (var manager = new Manager())
                    {
                        user = manager.Users
                               .FirstOrDefault(o => o.AlexaToken == session.User.AccessToken);

                        if (user != null)
                        {
                            RouteData.Values["userID"] = (int?)user.ID;

                            if (session.User.UserID != null && user.AlexaUserID != session.User.UserID)
                            {
                                user.AlexaUserID = session.User.UserID;

                                manager.SaveChanges();
                            }
                        }
                    }
                }
            }

            if (user == null)
            {
                var linkResponse = new
                {
                    version           = "1.0",
                    sessionAttributes = new
                    {
                        inListen = (inListen ? "true" : "false")
                    },
                    response = new
                    {
                        outputSpeech = new
                        {
                            type = "PlainText",
                            text = "You need to link your Harmony account using the Alexa app"
                        },
                        card = new
                        {
                            type = "LinkAccount"
                        },
                        shouldEndSession = true
                    }
                };

                return(Json(linkResponse, JsonRequestBehavior.AllowGet));
            }

            var requestType = root.Request.Type;

            if (requestType != null)
            {
                switch (requestType)
                {
                case "LaunchRequest":
                {
                    inListen = true;
                    success  = true;
                    speech   = "OK, I'm listening. A command list is available in the Alexa app. Say stop listening when you're done.";
                    card     = new
                    {
                        type  = "Standard",
                        title = "Command List",
                        text  = "Start the [name of] activity\nPress the [name of] button\nPress the [name of] button on the [device name]\nStart listening\nStop listening\nRun the [name of] sequence\nTurn everything off\n"
                    };
                }
                break;

                case "IntentRequest":
                {
                    var intent = root.Request.Intent;
                    if (intent != null)
                    {
                        if (intent.Name != null)
                        {
                            RouteData.Values["intentName"] = intent.Name;

                            switch (intent.Name)
                            {
                            case "AMAZON.HelpIntent":
                                success = true;
                                speech  = "Here are some sample phrases. Tell the remote to start the TV activity. Or. Tell the remote to pause. Or. Tell the remote to press the mute button.";
                                break;

                            case "ListenStartIntent":
                                inListen = true;
                                success  = true;
                                speech   = "OK, I'm listening";
                                break;

                            case "ListenEndIntent":
                            case "AMAZON.StopIntent":
                            case "AMAZON.CancelIntent":
                            case "AMAZON.NoIntent":
                                inListen = false;
                                success  = true;
                                speech   = "OK, done listening";
                                break;

                            default:
                            {
                                var values = (intent.Slots ?? new Dictionary <string, AlexaSlot>())
                                             .ToDictionary(o => o.Key, o => o.Value.Value);

                                success = Command(user, intent.Name, values, out speech);
                                if (inListen)
                                {
                                    speech = success ? "OK" : "Hmm";
                                }
                            }
                            break;
                            }
                        }
                        else
                        {
                            speech = "Error. Missing intent name.";
                        }
                    }
                    else
                    {
                        speech = "Error. Missing intent.";
                    }
                }
                break;
                }
            }
            else
            {
                speech = "Error. Missing request type.";
            }

            var response = new
            {
                version           = "1.0",
                sessionAttributes = new
                {
                    inListen = (inListen ? "true" : "false")
                },
                response = new
                {
                    outputSpeech = new
                    {
                        type = "PlainText",
                        text = speech ?? "OK"
                    },
                    card             = card,
                    shouldEndSession = !inListen
                }
            };

            return(Json(response, JsonRequestBehavior.AllowGet));
        }
예제 #2
0
        public ActionResult Index(AlexaRoot root)
        {
            Stream req = Request.InputStream;

            req.Seek(0, System.IO.SeekOrigin.Begin);
            string requestBody = new StreamReader(req).ReadToEnd();

            //var input = new JavaScriptSerializer().DeserializeObject(json);

            bool   success    = false;
            string speech     = null;
            bool   inSequence = false;
            string intentName = null;
            int?   userID     = null;

            if (root != null)
            {
                User user = null;

                var session = root.Session;
                if (session != null)
                {
                    if (session.Attributes != null)
                    {
                        string v;
                        if (session.Attributes.TryGetValue("inSequence", out v) && v == "true")
                        {
                            inSequence = true;
                        }
                    }

                    if (session.User != null && session.User.AccessToken != null)
                    {
                        using (var manager = new Manager())
                        {
                            user = manager.Users
                                   .FirstOrDefault(o => o.AlexaToken == session.User.AccessToken);

                            if (user != null && session.User.UserID != null && user.AlexaUserID != session.User.UserID)
                            {
                                user.AlexaUserID = session.User.UserID;

                                manager.SaveChanges();
                            }
                        }
                    }
                }

                var request = root.Request;
                if (request != null)
                {
                    var intent = request.Intent;
                    if (intent != null)
                    {
                        if (intent.Name != null)
                        {
                            intentName = intent.Name;

                            if (user != null)
                            {
                                userID = user.ID;

                                switch (intentName)
                                {
                                case "AMAZON.HelpIntent":
                                    success = true;
                                    speech  = "Here are some sample phrases. Tell the remote to start the TV activity. Or. Tell the remote to pause. Or. Tell the remote to press the mute button.";
                                    break;

                                case "SequenceStartIntent":
                                    inSequence = true;
                                    success    = true;
                                    speech     = "OK, I'm listening";
                                    break;

                                case "SequenceEndIntent":
                                    inSequence = false;
                                    success    = true;
                                    speech     = "OK, done listening";
                                    break;

                                default:
                                {
                                    var values = (intent.Slots ?? new Dictionary <string, AlexaSlot>())
                                                 .ToDictionary(o => o.Key, o => o.Value.Value);

                                    success = Command(user, intent.Name, values, out speech);
                                }
                                break;
                                }
                            }
                            else
                            {
                                speech = "You need to link your Harmony account using the Alexa app";
                            }
                        }
                        else
                        {
                            speech = "Error. Missing intent name.";
                        }
                    }
                    else
                    {
                        speech = "Error. Missing intent.";
                    }
                }
                else
                {
                    speech = "Error. Missing request.";
                }
            }
            else
            {
                speech = "Error. Missing root.";
            }

            using (Manager m = new Manager())
            {
                var log = new RequestLog()
                {
                    UserID      = userID,
                    IntentName  = intentName,
                    RequestBody = requestBody,
                    RequestDate = DateTime.Now
                };
                m.RequestLogs.Add(log);

                m.SaveChanges();
            }

            var response = new
            {
                version           = "1.0",
                sessionAttributes = new
                {
                    inSequence = (inSequence ? "true" : "false")
                },
                response = new
                {
                    outputSpeech = new
                    {
                        type = "PlainText",
                        text = inSequence
                            ? (success ? "Yep" : "Hmm")
                            : (speech ?? "OK")
                    },
                    shouldEndSession = !inSequence
                }
            };

            return(Json(response, JsonRequestBehavior.AllowGet));
        }
예제 #3
0
        public ActionResult Index(AlexaRoot root)
        {
            //var input = new JavaScriptSerializer().DeserializeObject(json);

            bool success = false;
            string speech = null;
            bool inListen = false;
            object card = null;

            if (root == null || root.Request == null || !IsTimestampValid(root.Request.Timestamp))
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            User user = null;

            var session = root.Session;
            if (session != null)
            {
                if (session.Attributes != null)
                {
                    string v;
                    if (session.Attributes.TryGetValue("inListen", out v) && v == "true")
                        inListen = true;
                }

                if (session.User != null && session.User.AccessToken != null)
                {
                    using (var manager = new Manager())
                    {
                        user = manager.Users
                            .FirstOrDefault(o => o.AlexaToken == session.User.AccessToken);

                        if (user != null)
                        {
                            RouteData.Values["userID"] = (int?)user.ID;

                            if (session.User.UserID != null && user.AlexaUserID != session.User.UserID)
                            {
                                user.AlexaUserID = session.User.UserID;

                                manager.SaveChanges();
                            }
                        }
                    }
                }
            }

            if (user == null)
            {
                var linkResponse = new
                {
                    version = "1.0",
                    sessionAttributes = new
                    {
                        inListen = (inListen ? "true" : "false")
                    },
                    response = new
                    {
                        outputSpeech = new
                        {
                            type = "PlainText",
                            text = "You need to link your Harmony account using the Alexa app"
                        },
                        card = new
                        {
                            type = "LinkAccount"
                        },
                        shouldEndSession = true
                    }
                };

                return Json(linkResponse, JsonRequestBehavior.AllowGet);
            }

            var requestType = root.Request.Type;
            if (requestType != null)
            {
                switch (requestType)
                {
                    case "LaunchRequest":
                        {
                            inListen = true;
                            success = true;
                            speech = "OK, I'm listening. A command list is available in the Alexa app. Say stop listening when you're done.";
                            card = new
                            {
                                type = "Standard",
                                title = "Command List",
                                text = "Start the [name of] activity\nPress the [name of] button\nPress the [name of] button on the [device name]\nStart listening\nStop listening\nRun the [name of] sequence\nTurn everything off\n"
                            };
                        }
                        break;

                    case "IntentRequest":
                        {
                            var intent = root.Request.Intent;
                            if (intent != null)
                            {
                                if (intent.Name != null)
                                {
                                    RouteData.Values["intentName"] = intent.Name;

                                    switch (intent.Name)
                                    {
                                        case "AMAZON.HelpIntent":
                                            success = true;
                                            speech = "Here are some sample phrases. Tell the remote to start the TV activity. Or. Tell the remote to pause. Or. Tell the remote to press the mute button.";
                                            break;

                                        case "ListenStartIntent":
                                            inListen = true;
                                            success = true;
                                            speech = "OK, I'm listening";
                                            break;

                                        case "ListenEndIntent":
                                        case "AMAZON.StopIntent":
                                        case "AMAZON.CancelIntent":
                                        case "AMAZON.NoIntent":
                                            inListen = false;
                                            success = true;
                                            speech = "OK, done listening";
                                            break;

                                        default:
                                            {
                                                var values = (intent.Slots ?? new Dictionary<string, AlexaSlot>())
                                                    .ToDictionary(o => o.Key, o => o.Value.Value);

                                                success = Command(user, intent.Name, values, out speech);
                                                if (inListen)
                                                    speech = success ? "OK" : "Hmm";
                                            }
                                            break;
                                    }
                                }
                                else
                                {
                                    speech = "Error. Missing intent name.";
                                }
                            }
                            else
                            {
                                speech = "Error. Missing intent.";
                            }
                        }
                        break;
                }
            }
            else
            {
                speech = "Error. Missing request type.";
            }

            var response = new
            {
                version = "1.0",
                sessionAttributes = new
                {
                    inListen = (inListen ? "true" : "false")
                },
                response = new
                {
                    outputSpeech = new
                    {
                        type = "PlainText",
                        text = speech ?? "OK"
                    },
                    card = card,
                    shouldEndSession = !inListen
                }
            };

            return Json(response, JsonRequestBehavior.AllowGet);
        }