Exemplo n.º 1
0
        public async Task <AlexaDemoResponse> MessageReceivedRequest(AlexaDemoRequest alexaRequest, AlexaDemoResponse response)
        {
            try
            {
                if (ValidatePermission(alexaRequest))
                {
                    AlexaNotificationManager manager = new AlexaNotificationManager();

                    var notif = new UserNotifcation();
                    notif.DisplayInfo = new Displayinfo();
                    notif.SpokenInfo  = new SpokenInfo();

                    List <DisplayContent> displayList = new List <DisplayContent>();
                    List <Content>        contentList = new List <Content>();

                    var data    = alexaRequest.Request.Message;
                    var content = (JArray)data["content"];

                    foreach (JObject locale in content.Children())
                    {
                        DisplayContent display = new DisplayContent();
                        Content        spoke   = new Content();

                        display.Title  = (String)locale["notificationTitle"].ToString();
                        display.Body   = (String)locale["notificationBody"].ToString();
                        display.Locale = (String)locale["locale"].ToString();

                        spoke.Locale = (String)locale["locale"].ToString();
                        spoke.Text   = (String)locale["spokenOutput"].ToString();
                        // spoke.type = (String)locale["spokenOutputType"].ToString().ToUpper();

                        contentList.Add(spoke);
                        displayList.Add(display);
                    }

                    notif.DisplayInfo.Content = displayList.ToArray();
                    notif.SpokenInfo.Content  = contentList.ToArray();

                    var result = manager.SendToUser(alexaRequest.Context.System.User.Permissions.ConsentToken, notif);
                }
                else
                {
                    response.Response.Card             = new Amazon.Alexa.Speechlet.Card();
                    response.Response.Card.Type        = "AskForPermissionsConsent";
                    response.Response.Card.Permissions = Alexa.Notification.Manager.Models.Permissions.Permission;
                }
            }
            catch (Exception ex)
            {
                return(await ThrowSafeException(ex, response));
            }
            return(response);
        }
        public async Task <NotificationUserResponse> SendToUser(string constentToken, UserNotifcation body)
        {
            try
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(NOTIFICATION_BASE_URL);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", constentToken);
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Content-length", JsonConvert.SerializeObject(body).Length.ToString());
                    var response = await client.PostAsync(NOTIFICATION_PATH, new StringContent(JsonConvert.SerializeObject(body,
                                                                                                                           Newtonsoft.Json.Formatting.None,
                                                                                                                           new JsonSerializerSettings
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    }), System.Text.Encoding.UTF8, "application/json"));

                    var result = new Models.NotificationUserResponse()
                    {
                        ReasonPhrase = SendToUserReasonCodeDecoder(response.StatusCode), StatusCode = response.StatusCode
                    };
                    return(result);
                }
            }
            catch (Exception ex)
            {
                return(new Models.NotificationUserResponse()
                {
                    ReasonPhrase = SendToUserReasonCodeDecoder(HttpStatusCode.InternalServerError), StatusCode = HttpStatusCode.InternalServerError
                });
            }
        }