コード例 #1
0
        /// <summary>
        /// Send a new outbound message.
        /// </summary>
        /// <param name="text">Message text</param>
        /// <param name="phone">Destination phone number in international E.164 format</param>
        /// <returns></returns>
        public SendingResult SendMessage(string text, string phone)
        {
            string[] phones = { phone };
            var options = new SendingOptions
            {
                Text = text,
                Phones = phones
            };

            return SendMessage(options);
        }
コード例 #2
0
        /// <summary>
        /// Convert SendingOptions to RestRequest parameters
        /// </summary>
        /// <param name="request"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        protected RestRequest _makeSendingRequest(RestRequest request, SendingOptions options)
        {
            request.Resource = "messages";
            if (!string.IsNullOrEmpty(options.Text)) request.AddParameter("text", options.Text);
            if (options.Phones != null && options.Phones.Length > 0) request.AddParameter("phones", string.Join(",", options.Phones));
            if (options.ContactIds != null && options.ContactIds.Length > 0) request.AddParameter("contacts", string.Join(",", options.ContactIds));
            if (options.ListIds != null && options.ListIds.Length > 0) request.AddParameter("lists", string.Join(",", options.ListIds));
            if (options.CutExtra.HasValue) request.AddParameter("cutExtra", (bool)options.CutExtra ? "1" : "0");
            if (options.PartsCount.HasValue) request.AddParameter("partsCount", options.PartsCount.ToString());
            if (!string.IsNullOrEmpty(options.ReferenceId)) request.AddParameter("referenceId", options.ReferenceId);
            if (!string.IsNullOrEmpty(options.From)) request.AddParameter("from", options.From);
            if (!string.IsNullOrEmpty(options.Rrule)) request.AddParameter("rrule", options.Rrule);
            if (options.SendingTime.HasValue) request.AddParameter("sendingTime", DateTimeToTimestamp((DateTime)options.SendingTime).ToString());
            if (options.TemplateId.HasValue) request.AddParameter("templateId", options.TemplateId.ToString());

            return request;
        }
コード例 #3
0
        /// <summary>
        /// Check pricing for a new outbound message.
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public Pricing GetPrice(SendingOptions options)
        {
            var request = new RestRequest(Method.POST);
            request = _makeSendingRequest(request, options);
            request.Method = Method.GET;
            request.Resource = "messages/price";
            request.AddParameter("dummy", "1");

            return Execute<Pricing>(request);
        }
コード例 #4
0
        /// <summary>
        /// Send a new outbound message.
        /// </summary>
        /// <param name="options">Message sending options</param>
        /// <returns></returns>
        public SendingResult SendMessage(SendingOptions options)
        {
            var request = new RestRequest(Method.POST);
            request = _makeSendingRequest(request, options);

            return Execute<SendingResult>(request);
        }
コード例 #5
0
        public void ShouldSendScheduledMessage()
        {
            IRestRequest savedRequest = null;
            mockClient.Setup(trc => trc.Execute<SendingResult>(It.IsAny<IRestRequest>()))
                .Callback<IRestRequest>((request) => savedRequest = request)
                .Returns(new SendingResult());
            var client = mockClient.Object;

            var options = new SendingOptions
            {
                Phones = messageReceivers,
                Text = messageText,
                SendingTime = messageTime
            };
            var link = client.SendMessage(options);

            mockClient.Verify(trc => trc.Execute<SendingResult>(It.IsAny<IRestRequest>()), Times.Once);
            Assert.IsNotNull(savedRequest);
            Assert.AreEqual("messages", savedRequest.Resource);
            Assert.AreEqual(Method.POST, savedRequest.Method);
            Assert.AreEqual(3, savedRequest.Parameters.Count);
            Assert.AreEqual(messageText, savedRequest.Parameters.Find(x => x.Name == "text").Value);
            Assert.AreEqual(string.Join(",", messageReceivers), savedRequest.Parameters.Find(x => x.Name == "phones").Value);
            Assert.AreEqual(Client.DateTimeToTimestamp(messageTime).ToString(), savedRequest.Parameters.Find(x => x.Name == "sendingTime").Value);
        }
コード例 #6
0
        public void ShouldSendMessage()
        {
            IRestRequest savedRequest = null;
            mockClient.Setup(trc => trc.Execute<SendingResult>(It.IsAny<IRestRequest>()))
                .Callback<IRestRequest>((request) => savedRequest = request)
                .Returns(new SendingResult());
            var client = mockClient.Object;

            int[] contactIds = { 321 };
            int[] listIds = { 5436 };
            string[] phones = { "55443322" };

            SendingOptions sendingOptions = new SendingOptions();
            sendingOptions.ContactIds = contactIds;
            sendingOptions.ListIds = listIds;
            sendingOptions.Phones = phones;
            sendingOptions.TemplateId = null;
            sendingOptions.Text = "Hello My Message";

            client.SendMessage(sendingOptions);

            mockClient.Verify(trc => trc.Execute<SendingResult>(It.IsAny<IRestRequest>()), Times.Once);
            Assert.IsNotNull(savedRequest);
            Assert.AreEqual("messages", savedRequest.Resource);
            Assert.AreEqual(Method.POST, savedRequest.Method);
            Assert.AreEqual(4, savedRequest.Parameters.Count);

            var content = "{ \"type\": \"session\", \"sessionId\": 1, \"bulkId\": 2,"
                + "\"messageId\": 3, \"scheduleId\": 4 }";

            var testClient = Common.CreateClient<SendingResult>(content, null, null);
            client = new Client(testClient);

            var messageSent = client.SendMessage(sendingOptions);

            Assert.IsTrue(messageSent.Success);
            Assert.AreEqual("session", messageSent.Type);
            Assert.AreEqual(1, messageSent.SessionId);
            Assert.AreEqual(2, messageSent.BulkId);
            Assert.AreEqual(3, messageSent.MessageId);
            Assert.AreEqual(4, messageSent.ScheduleId);
        }
コード例 #7
0
        public void ShouldSendAllSetParameters()
        {
            int[] contactIds = { 385, 15 };
            int[] listIds = { 424, 454, 223 };

            IRestRequest savedRequest = null;
            mockClient.Setup(trc => trc.Execute<SendingResult>(It.IsAny<IRestRequest>()))
                .Callback<IRestRequest>((request) => savedRequest = request)
                .Returns(new SendingResult());
            var client = mockClient.Object;

            var options = new SendingOptions
            {
                Phones = messageReceivers,
                Text = messageText,
                SendingTime = messageTime,
                ContactIds = contactIds,
                ListIds = listIds,
                From = messageSender,
                CutExtra = messageCutExtra,
                PartsCount = messagePartsCount,
                ReferenceId = sessionReferenceId,
                Rrule = messageRrule
            };
            var link = client.SendMessage(options);

            mockClient.Verify(trc => trc.Execute<SendingResult>(It.IsAny<IRestRequest>()), Times.Once);
            Assert.IsNotNull(savedRequest);
            Assert.AreEqual("messages", savedRequest.Resource);
            Assert.AreEqual(Method.POST, savedRequest.Method);
            Assert.AreEqual(10, savedRequest.Parameters.Count);
            Assert.AreEqual(messageText, savedRequest.Parameters.Find(x => x.Name == "text").Value);
            Assert.AreEqual(string.Join(",", messageReceivers), savedRequest.Parameters.Find(x => x.Name == "phones").Value);
            Assert.AreEqual(string.Join(",", contactIds), savedRequest.Parameters.Find(x => x.Name == "contacts").Value);
            Assert.AreEqual(string.Join(",", listIds), savedRequest.Parameters.Find(x => x.Name == "lists").Value);
            Assert.AreEqual(Client.DateTimeToTimestamp(messageTime).ToString(), savedRequest.Parameters.Find(x => x.Name == "sendingTime").Value);
            Assert.AreEqual(messageSender, savedRequest.Parameters.Find(x => x.Name == "from").Value);
            Assert.AreEqual(messageCutExtra ? "1" : "0", savedRequest.Parameters.Find(x => x.Name == "cutExtra").Value);
            Assert.AreEqual(messagePartsCount.ToString(), savedRequest.Parameters.Find(x => x.Name == "partsCount").Value);
            Assert.AreEqual(sessionReferenceId, savedRequest.Parameters.Find(x => x.Name == "referenceId").Value);
            Assert.AreEqual(messageRrule, savedRequest.Parameters.Find(x => x.Name == "rrule").Value);
        }
コード例 #8
0
        public void ShouldGetPrice()
        {
            IRestRequest savedRequest = null;
            mockClient.Setup(trc => trc.Execute<Pricing>(It.IsAny<IRestRequest>()))
                .Callback<IRestRequest>((request) => savedRequest = request)
                .Returns(new Pricing());
            var client = mockClient.Object;

            int[] contactIds = { 321 };
            int[] listIds = { 5436 };
            string[] phones = { "55443322" };

            SendingOptions sendingOptions = new SendingOptions();
            sendingOptions.ContactIds = contactIds;
            sendingOptions.ListIds = listIds;
            sendingOptions.Phones = phones;
            sendingOptions.TemplateId = null;
            sendingOptions.Text = "Hello My Message";

            client.GetPrice(sendingOptions);

            mockClient.Verify(trc => trc.Execute<Pricing>(It.IsAny<IRestRequest>()), Times.Once);
            Assert.IsNotNull(savedRequest);
            Assert.AreEqual("messages/price", savedRequest.Resource);
            Assert.AreEqual(Method.GET, savedRequest.Method);
            Assert.AreEqual(5, savedRequest.Parameters.Count);

            var content = "{ \"total\": 0.056, \"parts\": 1, \"countries\": { \"CH\": {"
                + "\"country\": \"CH\", \"count\": \"1\", \"max\": 0.056 } } }";

            var testClient = Common.CreateClient<Pricing>(content, null, null);
            client = new Client(testClient);

            var messagePricing = client.GetPrice(sendingOptions);

            Assert.IsTrue(messagePricing.Success);
            Assert.AreEqual("CH", messagePricing.Countries["CH"].Country);
            Assert.AreEqual(1, messagePricing.Countries["CH"].Count);
            Assert.AreEqual(0.056f, messagePricing.Countries["CH"].Price);
            Assert.AreEqual(1, messagePricing.Parts);
            Assert.AreEqual(0.056f, messagePricing.Total);
        }
コード例 #9
0
        public void ShouldSendTemplate()
        {
            var templateId = 318;

            IRestRequest savedRequest = null;
            mockClient.Setup(trc => trc.Execute<SendingResult>(It.IsAny<IRestRequest>()))
                .Callback<IRestRequest>((request) => savedRequest = request)
                .Returns(new SendingResult());
            var client = mockClient.Object;

            var options = new SendingOptions
            {
                Phones = messageReceivers,
                TemplateId = templateId
            };
            var link = client.SendMessage(options);

            mockClient.Verify(trc => trc.Execute<SendingResult>(It.IsAny<IRestRequest>()), Times.Once);
            Assert.IsNotNull(savedRequest);
            Assert.AreEqual("messages", savedRequest.Resource);
            Assert.AreEqual(Method.POST, savedRequest.Method);
            Assert.AreEqual(2, savedRequest.Parameters.Count);
            Assert.AreEqual(string.Join(",", messageReceivers), savedRequest.Parameters.Find(x => x.Name == "phones").Value);
            Assert.AreEqual(templateId.ToString(), savedRequest.Parameters.Find(x => x.Name == "templateId").Value);
        }