private static void SendFacebookMessage(FacebookSendRequest FacebookRequest)
        {
            // Setup a REST client object using the message send URL with our API Space incorporated
            var client  = new RestClient(string.Format("https://api.comapi.com/apispaces/{0}/messages", APISPACE));
            var request = new RestRequest(Method.POST);

            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("content-type", "application/json");
            request.AddHeader("authorization", "Bearer " + TOKEN); // Add the security token

            // Serialise our Facebook request object to JSON for submission
            string requestJson = JsonConvert.SerializeObject(FacebookRequest, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            request.AddParameter("application/json", requestJson, ParameterType.RequestBody);

            // Make the web service call
            IRestResponse response = client.Execute(request);

            if (response.StatusCode != System.Net.HttpStatusCode.Created)
            {
                // Something went wrong.
                throw new InvalidOperationException(string.Format("Call to Comapi failed with status code ({0}), and body: {1}", response.StatusCode, response.Content));
            }
        }
Пример #2
0
        private static void SendFacebookMessage(FacebookSendRequest FacebookRequest)
        {
            // Setup a REST client object using the web service URI and our API credentials
            var client = new RestClient(@"https://api-cpaas.dotdigital.com/cpaas/messages");

            client.Authenticator = new HttpBasicAuthenticator(API_USERNAME, API_PASSWORD);

            var request = new RestRequest(Method.POST);

            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("content-type", "application/json");

            // Serialise our Facebook request object to JSON for submission
            string requestJson = JsonConvert.SerializeObject(FacebookRequest, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            request.AddParameter("application/json", requestJson, ParameterType.RequestBody);

            // Make the web service call
            IRestResponse response = client.Execute(request);

            if (response.StatusCode != System.Net.HttpStatusCode.Created)
            {
                // Something went wrong.
                throw new InvalidOperationException(string.Format("Call to Engagement Cloud CPaaS failed with status code ({0}), and body: {1}", response.StatusCode, response.Content));
            }
        }
Пример #3
0
        public ActionResult TestMessage()
        {
            var viewData = new Models.HomeIndexViewModel();

            try
            {
                // Create the Facebook metadata for the logged in user using the Engagement Cloud CPaaS web service.
                viewData.FacebookMetaData = GetFacebookMetaData(User.Identity.Name);

                // Send a test message via the Engagement Cloud CPaaS "One" API

                // Setup the channel options to indicate this is a A2P post purchase message
                dynamic fbMessengerOptions = new ExpandoObject();
                fbMessengerOptions.messagingType = "MESSAGE_TAG";
                fbMessengerOptions.messageTag    = "POST_PURCHASE_UPDATE";

                // Create the request
                var myRequest = new FacebookSendRequest()
                {
                    to = new FacebookSendRequest.toStruct()
                    {
                        profileId = User.Identity.Name
                    },                                                                          // Current logged in user
                    body           = "A test message sent via Engagement Cloud CPaaS!",
                    channelOptions = new FacebookSendRequest.channelOptionsStruct()
                    {
                        fbMessenger = fbMessengerOptions
                    }
                };

                // Send it
                SendFacebookMessage(myRequest);

                // Set the result
                viewData.TestMessageResult = new Models.ResultFeedback()
                {
                    Success         = true,
                    FeedbackMessage = "Test message sent successfully, check Facebook Messenger"
                };
            }
            catch (Exception ex)
            {
                // An error occurred.
                viewData.TestMessageResult = new Models.ResultFeedback()
                {
                    Success         = false,
                    FeedbackMessage = "The web service call failed, check the console logs",
                    ErrorMessage    = HttpUtility.JavaScriptStringEncode(ex.Message)
                };
            }

            // Render the view with the model
            return(View("Index", viewData));
        }
        public ActionResult TestRichMessage()
        {
            var viewData = new Models.HomeIndexViewModel();

            try
            {
                // Create the Facebook metadata for the logged in user using the Comapi web service.
                viewData.FacebookMetaData = GetFacebookMetaData(User.Identity.Name);

                // Send a test message via the Comapi "One" API

                // Create the request
                var myRequest = new FacebookSendRequest()
                {
                    to = new FacebookSendRequest.toStruct {
                        profileId = User.Identity.Name
                    },                                                                        // Current logged in user
                    customBody = new FacebookSendRequest.customBodyStruct {
                        fbMessenger = @"
                        {
                          ""attachment"": {
                            ""type"": ""image"",
                            ""payload"": {
                                        ""url"": ""https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/17156020_1871286216424427_1662368582524349363_n.jpg?oh=22685c22a19fc2e28e69634e6a920972&oe=592FD3D1""
                            }
                                }
                        }"
                    }
                };

                // Send it
                SendFacebookMessage(myRequest);

                // Set the result
                viewData.TestMessageResult = new Models.ResultFeedback()
                {
                    Success         = true,
                    FeedbackMessage = "Test message sent successfully, check Facebook"
                };
            }
            catch (Exception ex)
            {
                // An error occurred.
                viewData.TestMessageResult = new Models.ResultFeedback()
                {
                    Success         = false,
                    FeedbackMessage = string.Format(@"The web service call failed: {0}", ex.Message)
                };
            }

            // Render the view with the model
            return(View("Index", viewData));
        }
        public ActionResult TestMessage()
        {
            var viewData = new Models.HomeIndexViewModel();

            try
            {
                // Create the Facebook metadata for the logged in user using the Comapi web service.
                viewData.FacebookMetaData = GetFacebookMetaData(User.Identity.Name);

                // Send a test message via the Comapi "One" API

                // Create the request
                var myRequest = new FacebookSendRequest()
                {
                    to = new FacebookSendRequest.toStruct()
                    {
                        profileId = User.Identity.Name
                    },                                                                          // Current logged in user
                    body = "A test message sent via Comapi!"
                };

                // Send it
                SendFacebookMessage(myRequest);

                // Set the result
                viewData.TestMessageResult = new Models.ResultFeedback()
                {
                    Success         = true,
                    FeedbackMessage = "Test message sent successfully, check Facebook"
                };
            }
            catch (Exception ex)
            {
                // An error occurred.
                viewData.TestMessageResult = new Models.ResultFeedback()
                {
                    Success         = false,
                    FeedbackMessage = string.Format(@"The web service call failed: {0}", ex.Message)
                };
            }

            // Render the view with the model
            return(View("Index", viewData));
        }