public async Task <ActionResult> SendTextMessage(TextMessageViewModel viewModel)
        {
            if (viewModel == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var smsClient = new TwillioWrapperClient();

            smsClient.Init();

            // Get 'from number'
            string fromNumber = GetFromNumber(viewModel);

            if (!viewModel.Bulk)
            {
                await SendSingleTextMessage(viewModel, smsClient, fromNumber);
            }
            else
            {
                await SendBulkTextMessages(viewModel, smsClient, fromNumber);
            }

            viewModel.TextMessageSent = true;

            return(View(viewModel));
        }
示例#2
0
        public ActionResult BulkTextMessage(BulkContactViewModel viewModel)
        {
            if (viewModel.Members.Count == 0)
            {
                return(RedirectToAction("ContactSelected"));
            }

            var user = unitOfWork.AppUserRepository.FindUserByEmail(User.Identity.Name);

            var textMessageContact = new TextMessageContact
            {
                ToPhoneNumber   = string.Empty,
                FromPhoneNumber = user.PhoneNumber,
                DateSent        = DateTime.Now
            };

            var textMessageViewModel = new TextMessageViewModel
            {
                TextMessageContact = textMessageContact,
                Bulk    = true,
                Members = viewModel.Members
            };

            TempData["TextMessageViewModel"] = textMessageViewModel;

            RouteValueDictionary routeDictionary = new RouteValueDictionary();

            routeDictionary.Add("TextMessageViewModel", textMessageViewModel);

            return(RedirectToAction("SendBulkTextMessage", "Sms", routeDictionary));
        }
        private async Task <ActionResult> SendSingleTextMessage(TextMessageViewModel viewModel, TwillioWrapperClient smsClient, string fromNumber)
        {
            var smsMessage = new SmsMessage
            {
                Message    = viewModel.TextMessageContact.Message,
                FromNumber = fromNumber
            };

            // set 'to' number
            if (!string.IsNullOrEmpty(viewModel.TestToNumber))
            {
                smsMessage.ToNumber = viewModel.TestToNumber;
            }
            else if (!string.IsNullOrEmpty(viewModel.TextMessageContact.ToPhoneNumber))
            {
                smsMessage.ToNumber = viewModel.TextMessageContact.ToPhoneNumber;
            }

            try
            {
                // Send SMS message
                IResponse response = await smsClient.SendSmsAsync(smsMessage);

                if (IsResponseOk(response))
                {
                    viewModel.StatusNotification = GetStatusNotification(TextMessageStatus.SingleTextDelivered, viewModel.TextMessageContact.MemberFullName);

                    viewModel.TextMessageContact.DateSent = DateTime.Now;

                    // Store contact record
                    StoreTextMessage(viewModel.TextMessageContact);
                }
                else
                {
                    viewModel.StatusNotification = GetStatusNotification(TextMessageStatus.SingleTextNotDelivered, viewModel.TextMessageContact.MemberFullName);
                }
            }
            catch (Exception ex)
            {
                // log caught exception with Elmah
                ErrorSignal.FromCurrentContext().Raise(ex);

                viewModel.StatusNotification = GetStatusNotification(TextMessageStatus.SingleTextNotDelivered, viewModel.TextMessageContact.MemberFullName);
            }

            if (viewModel.StatusNotification != null)
            {
                viewModel.HasStatusNotification = true;
            }

            return(View(viewModel));
        }
        private string GetFromNumber(TextMessageViewModel viewModel)
        {
            bool   testTwillio = bool.Parse(ConfigurationManager.AppSettings["TestTwillio"]);
            string fromNumber  = ConfigurationManager.AppSettings["TwillioFromNumber"];

            if (testTwillio)
            {
                // gives user the option to enter and test another from number
                if (!string.IsNullOrEmpty(viewModel.TestFromNumber) &&
                    !viewModel.TestFromNumber.Equals(fromNumber))
                {
                    return(viewModel.TestFromNumber);
                }
            }

            // production
            return(fromNumber);
        }
示例#5
0
        public override IMessageData GetMessageData(MessagesCollectionView collectionView, NSIndexPath indexPath)
        {
            MessageViewModel      msg        = viewModel.Messages [indexPath.Row];
            TextMessageViewModel  msgAsText  = msg as TextMessageViewModel;
            ImageMessageViewModel msgAsImage = msg as ImageMessageViewModel;

            string senderId   = msg.IsIncoming ? viewModel.Friend.FriendId.ToString() : SenderId;
            string senderName = msg.IsIncoming ? viewModel.Friend.Name : SenderDisplayName;

            if (string.IsNullOrWhiteSpace(senderName))
            {
                senderName = string.Empty;
            }

            NSDate msgDate = (NSDate)(new DateTime(msg.Timestamp.Ticks, DateTimeKind.Utc));
            var    result  = new JSQMessagesViewController.Message(senderId, senderName, msgDate, msgAsImage != null ? "Image message" : msgAsText.Text);

            return(result);
        }
        // GET
        public ActionResult SendTextMessage(int?id, string number)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }


            var    member         = unitOfWork.MemberRepository.FindMemberById(id);
            string memberFullName = $"{member.FirstName} {member.LastName}";

            var contact = new TextMessageContact
            {
                MemberId       = (int)id,
                MemberFullName = memberFullName,
                ToPhoneNumber  = number,
            };

            var viewModel = new TextMessageViewModel
            {
                TextMessageContact = contact,
                Bulk = false
            };


            // Check if testing Twillio
            bool testTwillio = bool.Parse(ConfigurationManager.AppSettings["TestTwillio"]);

            if (testTwillio)
            {
                viewModel.IsTest         = testTwillio;
                viewModel.TestFromNumber = ConfigurationManager.AppSettings["TwillioFromNumber"];
            }

            viewModel.HasStatusNotification = false;
            viewModel.StatusNotification    = new NotificationViewModel();

            return(View(viewModel));
        }
        private async Task <ActionResult> SendBulkTextMessages(TextMessageViewModel viewModel, TwillioWrapperClient smsClient, string fromNumber)
        {
            bool isGoodResponse = true;

            foreach (var member in viewModel.Members)
            {
                var ccgMember = unitOfWork.MemberRepository.FindMemberById(member.MemberId);

                string toNumber = string.Empty;

                // check if cell number exists first
                if (!string.IsNullOrEmpty(ccgMember.CellPhoneNumber))
                {
                    toNumber = ccgMember.CellPhoneNumber;
                }
                else
                {
                    toNumber = ccgMember.PhoneNumber;
                }

                // use test number is exists
                if (!string.IsNullOrEmpty(viewModel.TestToNumber))
                {
                    toNumber = viewModel.TestToNumber;
                }

                var smsMessage = new SmsMessage
                {
                    ToNumber   = toNumber,
                    FromNumber = fromNumber,
                    Message    = viewModel.TextMessageContact.Message,
                };

                try
                {
                    // Send SMS message
                    IResponse response = await smsClient.SendSmsAsync(smsMessage);

                    if (IsResponseOk(response))
                    {
                        viewModel.TextMessageContact.DateSent = DateTime.Now;

                        // Store contact record
                        viewModel.TextMessageContact.MemberId = member.MemberId;
                        StoreTextMessage(viewModel.TextMessageContact);
                    }
                    else
                    {
                        isGoodResponse = false;
                    }
                }
                catch (Exception ex)
                {
                    // log caught exception with Elmah
                    ErrorSignal.FromCurrentContext().Raise(ex);

                    isGoodResponse = false;
                }
            }

            // Send notification to user with bulk text message status.
            // If any text fails user gets a not delivered message.
            if (isGoodResponse)
            {
                viewModel.StatusNotification = GetStatusNotification(TextMessageStatus.BulkTextsDelivered, viewModel.TextMessageContact.MemberFullName);
            }
            else
            {
                viewModel.StatusNotification = GetStatusNotification(TextMessageStatus.BulkTextsNotDelivered, viewModel.TextMessageContact.MemberFullName);
            }

            if (viewModel.StatusNotification != null)
            {
                viewModel.HasStatusNotification = true;
            }

            return(View(viewModel));
        }