コード例 #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 Login(AuthModels.Login model)
        {
            if (ModelState.IsValid)
            {
                string token = null;
                try
                {
                    token = HarmonyLogin.GetUserAuthToken(model.Username, model.Password);
                }
                catch { }

                if (string.IsNullOrEmpty(token))
                {
                    ModelState.AddModelError("Username", "Invalid username or password");
                }

                if (ModelState.IsValid)
                {
                    using (var manager = new Manager())
                    {
                        var user = manager.Users
                            .FirstOrDefault(o => o.HarmonyUsername == model.Username);

                        if (user == null)
                        {
                            user = new User()
                            {
                                HarmonyUsername = model.Username
                            };
                        }

                        user.HarmonyPassword = "";
                        user.HarmonyToken = token;
                        user.AlexaToken = RandomString(50);
                        user.AlexaUserID = "";
                        user.Hostname = model.Hostname;

                        manager.SaveChanges();

                        string url = ConfigurationManager.AppSettings["AuthUrl"]
                            + "#state=" + Url.Encode(model.State)
                            + "&access_token=" + Url.Encode(user.AlexaToken)
                            + "&token_type=Bearer";

                        return Redirect(url);
                    }
                }
            }

            return View(model);
        }
コード例 #3
0
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            Stream req = Request.InputStream;
            req.Seek(0, System.IO.SeekOrigin.Begin);
            string requestBody = new StreamReader(req).ReadToEnd();

            using (Manager m = new Manager())
            {
                var log = new RequestLog()
                {
                    UserID = RouteData.Values["userID"] as int?,
                    IntentName = RouteData.Values["intentName"] as string,
                    RequestBody = requestBody,
                    RequestDate = DateTime.Now
                };
                m.RequestLogs.Add(log);

                m.SaveChanges();
            }

            base.OnActionExecuted(filterContext);
        }