Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

Accessing SMS provider

$
0
0

Hello,

I am getting a login failure '("code":3,"message":"Invalid login details","status":"failure")' when I use the following code in a CRM Plugin (CRM is on premise).  I am using a company called Txtlocal (Txtlocal.co.uk) as the SMS provider.  The code within in the try / catch block works fine in a console application by replacing the Uri.EscapeUriString() with Uri.EscapeDataString(), I cannot use Uri.EscapeDataString() in the plugin as this always generates a Generis SQL Server Error:

        public void Execute(IServiceProvider serviceProvider)
        {
            // Get the execution context from the service provider
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                // Get the target entity from the input parameters
                Entity entity = (Entity)context.InputParameters["Target"];

                if (context.MessageName == "Create" && entity.LogicalName == "new_smsmessaging")
                {
                    try
                    {
                        string apikey = Uri.EscapeUriString("fV8Rwej8k+Y-1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
                        string sendTo = entity.GetAttributeValue<string>("subject");
                        string message = entity.GetAttributeValue<string>("description");

                        using (var wb = new WebClient())
                        {
                            byte[] response = wb.UploadValues("https://api.txtlocal.com/send/", new NameValueCollection()
                                {
                                    {"apiKey", apikey},
                                    {"numbers", sendTo},
                                    {"message", message},
                                    {"sender", "TEST"},
                                    {"test", "true"}
                                });
                            string result = System.Text.Encoding.UTF8.GetString(response);
                            entity["new_result"] = result;
                            entity["new_senton"] = DateTime.Now.ToLocalTime();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidPluginExecutionException(string.Format("SMS Messaging : {0}{1}Inner Exception : {2}", 
                            ex.Message, Environment.NewLine, ex.InnerException));
                    }
                }
            }
        }

Thanks in advance

Duane


Viewing all articles
Browse latest Browse all 8223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>