public void HandleRequest(long notifierId, TwilioSms sms)
        {
            if (sms == null)
            {
                throw new ArgumentException(nameof(sms));
            }

            var notifier = Entity.Get <TwilioNotifier>(notifierId);


            if (notifier == null)
            {
                throw new UnmatchedNotifierException();
            }

            ValidateRequest(notifier);


            // check the account Sid matches. This ensures that someone else with a Twilio account can't create a phony incoming message.
            if (notifier.TpAccountSid != sms.AccountSid)
            {
                throw new UnauthorisedApiException();
            }

            string matchExpression = $"[From]='{sms.To}' and [To]='{sms.From}' and not [Closed] and GetDateTime() < [Notification].[Accept replies until] ";       // Looking for a matching reply

            // do Something
            // Match sent to received.

            var matches      = Entity.GetCalculationMatchesAsIds(matchExpression, SmsSendRecord.SmsSendRecord_Type, false);
            var countMatches = matches.Count();

            if (countMatches == 1)
            {
                var send = Entity.Get <SmsSendRecord>(matches.First());
                AddReply(send, sms.Body);
            }
            else if (countMatches > 1)
            {
                // Find the newest record and close the rest
                var matchedSends = Entity.Get <SmsSendRecord>(matches);
                var dateOrdered  = matchedSends.OrderByDescending(s => s.SrSendDate ?? DateTime.MinValue);
                var newest       = dateOrdered.First();

                AddReply(newest, sms.Body);

                var rest = dateOrdered.Skip(1);

                Close(rest);
            }
            else
            {
                EventLog.Application.WriteInformation($"Unmatched SMS received and ingored.\nNotifier: {notifier.Name}({notifier.Id})\n AccountSid: {sms.AccountSid}\nFrom: {sms.From}\nTo: {sms.To}\nBody: '{sms.Body}'");
            }
        }
示例#2
0
        /// <summary>
        /// Send a message syncronously
        /// </summary>
        /// <returns>The messages</returns>
        public IEnumerable <SendResponse> SendMessageSync(long notifierId, string sendingNumber, string message, IEnumerable <string> numbers)
        {
            var cleanFailNumber = PhoneNumberHelper.CleanNumber(FailNumber);
            var statusUrl       = _receiver.GetStatusUpdateUrl(notifierId);
            var incommingUrl    = _receiver.GetIncommingUrl(_notifier.Id);

            var reply = $"Reply:{message}";

            var result = new List <SendResponse>();

            foreach (var number in numbers)
            {
                //TODO: Emulate status updating

                bool isFailNumber = number == cleanFailNumber;

                var messageSid = Guid.NewGuid().ToString();

                if (!isFailNumber)
                {
                    var twilioSms = new TwilioSms
                    {
                        MessageSid = messageSid,
                        AccountSid = _notifier.TpAccountSid,
                        From       = PhoneNumberHelper.CleanNumber(number),
                        To         = PhoneNumberHelper.CleanNumber(sendingNumber),
                        Body       = $"TestMode:Reply:{ResponseFromNumber(number)}"
                    };

                    _replies.Add(twilioSms);
                }

                var sendResponse = new SendResponse
                {
                    Number     = number,
                    MessageSid = messageSid,
                    Success    = !isFailNumber,
                    Message    = isFailNumber ? FailMessage : null
                };

                result.Add(sendResponse);
            }

            return(result);
        }