Exemplo n.º 1
0
        public static string SendSmsReplyIncoming(CMSDataContext db, int receivedId,
                                                  Person p, string toNumber, SMSNumber fromNumber, string title, string message)
        {
            string sSid   = GetSid(db);
            string sToken = GetToken(db);

            var list = new SMSList
            {
                Created     = DateTime.Now,
                SendAt      = DateTime.Now,
                SenderID    = db.UserPeopleId ?? 1,
                SendGroupID = fromNumber.GroupID,
                Title       = title,
                Message     = message,
                ReplyToId   = receivedId
            };
            var item = new SMSItem
            {
                ListID   = list.Id,
                PeopleID = p?.PeopleId,
                Number   = toNumber,
            };

            list.SMSItems.Add(item);

            var r = new TextReplacements(db, message);

            if (item.PeopleID != null)
            {
                message = r.DoReplacements(item);
            }
            var response = SendSmsInternal(sSid, sToken, fromNumber.Number, toNumber, message);

            var succeeded = !IsSmsFailed(response);

            if (succeeded)
            {
                list.SentSMS = 1;
                item.Sent    = true;
            }
            db.SMSLists.InsertOnSubmit(list);
            db.SubmitChanges();
            if (succeeded)
            {
                db.Connection.Execute(
                    "update dbo.SmsReceived set RepliedTo = @replied where id = @id",
                    new { replied = true, id = receivedId });
            }
            return(ResultMessage(response, toNumber));
        }
Exemplo n.º 2
0
        public static void ProcessQueue(CMSDataContext db, int iListID)
        {
            var sSID   = GetSid(db);
            var sToken = GetToken(db);

            if (sSID.Length == 0 || sToken.Length == 0)
            {
                return;
            }

            var cb = new SqlConnectionStringBuilder(db.ConnectionString)
            {
                InitialCatalog = "ELMAH"
            };
            var ErrorLog = new SqlErrorLog(cb.ConnectionString)
            {
                ApplicationName = "BVCMS"
            };

            var smsList = (from e in db.SMSLists
                           where e.Id == iListID
                           select e).Single();

            var smsItems = from e in db.SMSItems
                           where e.ListID == iListID
                           select e;

            var smsGroup = (from e in db.SMSNumbers
                            where e.GroupID == smsList.SendGroupID
                            select e).ToList();

            var iCount = 0;

            var hostUrl = db.Setting("DefaultHost", "");
            var r       = new TextReplacements(db, smsList.Message);

            foreach (var item in smsItems)
            {
                try
                {
                    if (item.NoNumber || item.NoOptIn)
                    {
                        continue;
                    }
                    var callbackUrl = hostUrl.HasValue() ? $"{hostUrl}/WebHook/Twilio/{item.Id}" : null;

                    var message = smsList.Message;
                    if (item.PeopleID != null)
                    {
                        message = r.DoReplacements(item);
                    }
                    var response = SendSmsInternal(sSID, sToken, smsGroup[iCount].Number, item.Number, message, callbackUrl);

                    if (!callbackUrl.HasValue())
                    {
                        UpdateSMSItemStatus(db, item, response);
                    }

                    iCount++;
                    if (iCount >= smsGroup.Count())
                    {
                        iCount = 0;
                    }
                }
                catch (ApiException ae)
                {
                    if (ae.Code == 21610) // https://www.twilio.com/docs/api/errors/21610
                    {
                        var person = db.People.FirstOrDefault(p => p.PeopleId == item.PeopleID);
                        person.ReceiveSMS = false;
                        item.ErrorMessage = "User opt-out";
                    }
                    else
                    {
                        Console.WriteLine(ae);
                        ErrorLog.Log(new Error(ae));
                        item.ErrorMessage = $"({ae.Code}) {ae.Message}".MaxString(150);
                    }

                    item.ResultStatus = $"error";
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    ErrorLog.Log(new Error(ex));

                    item.ResultStatus = $"error";
                    item.ErrorMessage = $"{ex.Message}".MaxString(150);
                    db.SubmitChanges();
                }
            }
            smsList.Sent = true;
            db.SubmitChanges();
        }