/// <summary>
        /// Send message(es) using REST API Client
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static RestSmsResponse SendSms(RestSmsRequest message)
        {
            //Check if we need to read from app.config
            if (string.IsNullOrEmpty(message.Username))
            {
                message.Username = ConfigurationManager.AppSettings["InteleApiCustomerId"];
            }

            if (string.IsNullOrEmpty(message.Password))
            {
                message.Password = ConfigurationManager.AppSettings["InteleApiPassword"];
            }

            string customData = ConfigurationManager.AppSettings["SmsMessageCustomData"];

            foreach (var msg in message.Messages)
            {
                if (msg.CustomData == null || msg.CustomData.Count == 0)
                {
                    //Bug fix 22.10.2019
                    if (msg.CustomData == null)
                    {
                        msg.CustomData = new Dictionary <string, string>();
                    }

                    foreach (var addString in customData.Split(','))
                    {
                        msg.CustomData.Add(addString.Split('=')[0], addString.Split('=')[1]);
                    }
                }
            }


            return(Sms.SmsRestClient.ExecuteRestApiCall(message));
        }
Пример #2
0
        /// <summary>
        /// Send message using Rest Dto
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static RestSmsResponse ExecuteRestApiCall(RestSmsRequest request)
        {
            //Create client for accessing REST Api
            var client = new JsonServiceClient(Dto.RestApiGlobals.RestApiBaseUri)
            {
                Timeout        = TimeSpan.FromSeconds(120),
                UserAgent      = "Rest API test client",
                ResponseFilter = res => res.StatusCode.ToString().Print()
            };

            var sendResp = client.Post(request);

            client.Dispose();
            client = null;

            return(sendResp);
        }
        /// <summary>
        /// Simple Send message test using REST API
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="password"></param>
        /// <param name="destinationAddress"></param>
        /// <param name="message"></param>
        public static void SendSmsMessageRestApi(int customerId, string password, long destinationAddress, string message)
        {
            //Create main request object
            var restRequest = new RestSmsRequest();

            restRequest.Username = customerId.ToString(); //Your Intele Customer Id
            restRequest.Password = password;              //Your Intele Password

            //Create a message and store in temp list
            var msgList = new List <RestSmsMessageReq>();

            var addMsg = new RestSmsMessageReq
            {
                Category           = "TEST-MESSAGES",
                DeliveryReportUrl  = string.Empty,                //Set to your server uri to receive delivery reports to verify that the message is delivered
                DestinationAddress = destinationAddress,
                Gateway            = 99700999,
                MessageId          = Guid.NewGuid().ToString().Replace("-", ""),
                MessageType        = "11",          //MessageType 11 allows more than default 160 characters. Message is automaticly splitted into parts on server using this MessageType value
                OriginatorAddress  = "+4799700999", //Could be +4797513609 or any valid international mobile number
                Price              = 0,
                ServiceCode        = string.Empty,
                ServiceDescription = string.Empty,
                ValidationPeriod   = DateTime.Now.AddHours(1).ToString("ddMMyyyyHHmmss"), //Set delivery to timeout if message is not received by end-user in 2 hours.
                UserDataHeader     = string.Empty,                                        //Only used on concatenated messages, or binary messages with MessageType 10
                UserData           = message,
                CustomData         = addProps("qos=3")
            };

            msgList.Add(addMsg);


            //Inline function for adding properties to CustomData
            //Takes input value formatted as value=data,value2=data2 etc
            Dictionary <string, string> addProps(string addValues)
            {
                var addList = new Dictionary <string, string>();

                if (string.IsNullOrEmpty(addValues) || addValues.IndexOf("=") == -1)
                {
                    return(addList);
                }

                var addParams = addValues.Split(',');

                foreach (var p in addParams)
                {
                    addList.Add(p.Split('=')[0], p.Split('=')[1]);
                }

                return(addList);
            }

            restRequest.Messages = msgList.ToArray();

            var response = SmsFactory.SendSms(restRequest);

            // TODO:
            //Just print output to console. You should handle each message and it's status and returned parameters here.
            Console.WriteLine(JsonSerializer.SerializeToString(response));
        }