Beispiel #1
0
        public string SendLostPasswordSms(string internationalPrefix, string phoneNumber, Func <string, string> createUrl)
        {
            _orchardServices.WorkContext.TryResolve <ISmsServices>(out _smsServices);
            if (_smsServices == null)
            {
                return("FALSE");
            }

            var user = _orchardServices.ContentManager.Query <UserPart, UserPartRecord>()
                       .Join <UserPwdRecoveryPartRecord>()
                       .Where(u => u.InternationalPrefix == internationalPrefix.ToString() && u.PhoneNumber == phoneNumber.ToString())
                       .List().FirstOrDefault();

            if (user != null)
            {
                string nonce = _userService.CreateNonce(user, DelayToResetPassword);
                string url   = createUrl(nonce);

                //var template = _shapeFactory.Create("Template_User_LostPassword", Arguments.From(new {
                //    User = user,
                //    LostPasswordUrl = url
                //}));
                //template.Metadata.Wrappers.Add("Template_User_Wrapper");

                //var parameters = new Dictionary<string, object> {
                //            {"Subject", T("Lost password").Text},
                //            {"Body", _shapeDisplay.Display(template)},
                //            {"Recipients", user.Email }
                //        };
                var smsSettings = _orchardServices.WorkContext.CurrentSite.As <SmsSettingsPart>();

                long phoneNumberComplete = 0;
                if (long.TryParse(String.Concat(internationalPrefix.Trim(), phoneNumber.Trim()), out phoneNumberComplete))
                {
                    //return _smsServices.SendSms(new long[] { phoneNumberComplete }, user.UserName + "\r\n" + url);

                    Hashtable hs = new Hashtable();
                    hs.Add("SmsContactNumber", phoneNumberComplete);

                    List <Hashtable> listaDestinatari = new List <Hashtable>();
                    listaDestinatari.Add(hs);

                    return(_smsServices.SendSms(listaDestinatari, user.UserName + "\r\n" + url));
                }
            }

            return("FALSE");
        }
        public void Process(ScheduledTaskContext context)
        {
            if (context.Task.TaskType != TaskType)
            {
                return;
            }
            try {
                //dynamic content = _orchardServices.ContentManager.Get(part.ContentItem.Id);
                dynamic        content        = context.Task.ContentItem;
                SmsGatewayPart part           = context.Task.ContentItem.As <SmsGatewayPart>();
                Int32[]        ids            = null;
                Int32?         idLocalization = null;

                if (content.QueryPickerPart != null && content.QueryPickerPart.Ids.Length > 0)
                {
                    ids = content.QueryPickerPart.Ids;
                }

                var localizedPart = content.LocalizationPart;
                if (localizedPart != null && localizedPart.Culture != null)
                {
                    idLocalization = localizedPart.Culture.Id;
                }

                IList listaDestinatari = new List <Hashtable>();

                if (part.RecipientList != null && part.RecipientList != "")
                {
                    string[] lstDest = part.RecipientList.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                    foreach (string tel in lstDest)
                    {
                        Hashtable hs = new Hashtable();
                        hs.Add("SmsContactNumber", tel);

                        listaDestinatari.Add(hs);
                    }
                }
                else
                {
                    //var listaNumeri = _smsCommunicationService.GetSmsNumbersQueryResult(ids, idLocalization);
                    listaDestinatari = _smsCommunicationService.GetSmsQueryResult(ids, idLocalization, false, context.Task.ContentItem);
                }

                if (listaDestinatari.Count > 0)
                {
                    string linktosend = "";
                    ICommunicationService _communicationService;

                    bool tryed = _orchardServices.WorkContext.TryResolve <ICommunicationService>(out _communicationService);
                    if (tryed)
                    {
                        if (_communicationService.CampaignLinkExist(part))
                        {
                            linktosend = _communicationService.GetCampaignLink("Sms", part);
                        }
                    }
                    string messageToSms = part.Message + " " + linktosend;

                    // Invio SMS
                    //_smsServices.SendSms(listaDestinatari.Select(x => Convert.ToInt64(x.SmsPrefix + x.SmsNumber)).ToArray(),
                    //                     messageToSms, part.Alias, "Orchard_" + part.Id.ToString(), part.HaveAlias);
                    _smsServices.SendSms(listaDestinatari, messageToSms, part.Alias, "Orchard_" + part.Id.ToString(), part.HaveAlias);

                    part.SmsRecipientsNumber = listaDestinatari.Count;
                    part.SmsMessageSent      = true;
                }
            } catch (Exception ex) {
                string idcontenuto = "nessun id ";
                try {
                    idcontenuto = context.Task.ContentItem.Id.ToString();
                } catch (Exception ex2) { Logger.Error(ex2, ex2.Message); }
                Logger.Error(ex, "Error on " + TaskType + " for ContentItem id = " + idcontenuto + " : " + ex.Message);
            }
        }
 async Task INotificationHandler <LoanEffetivatedEvent> .Handle(LoanEffetivatedEvent notification, CancellationToken cancellationToken)
 {
     await _smsServices.SendSms(notification.Phone, $"Book {notification.BookName} lent, expected return date {notification.ReturnDate.ToShortDateString()}");
 }
Beispiel #4
0
        public SmsGatewayPartHandler(IRepository <SmsGatewayPartRecord> repository, INotifier notifier, IOrchardServices orchardServices, ISmsServices smsServices,
                                     ISmsCommunicationService smsCommunicationService, IScheduledTaskManager taskManager)
        {
            _orchardServices         = orchardServices;
            _smsServices             = smsServices;
            _smsCommunicationService = smsCommunicationService;
            _notifier    = notifier;
            _taskManager = taskManager;
            T            = NullLocalizer.Instance;
            Filters.Add(StorageFilter.For(repository));

            OnUpdated <SmsGatewayPart>((context, part) => {
                if (_orchardServices.WorkContext.HttpContext.Request.Form["submit.Test"] == "submit.SmsTest")
                {
                    if (part.SendToTestNumber && part.ContentItem.ContentType == "CommunicationAdvertising")
                    {
                        if (!string.IsNullOrEmpty(part.PrefixForTest) && !string.IsNullOrEmpty(part.NumberForTest))
                        {
                            string linktosend = "";
                            ICommunicationService _communicationService;

                            bool tryed = _orchardServices.WorkContext.TryResolve <ICommunicationService>(out _communicationService);
                            if (tryed)
                            {
                                if (_communicationService.CampaignLinkExist(part))
                                {
                                    linktosend = _communicationService.GetCampaignLink("Sms", part);
                                }
                            }
                            string messageToSms = part.Message + " " + linktosend;

                            // Id deve essere univoco - Utilizzo part.Id per il Publish e lo modifico per SendToTestNumber
                            string IdSendToTest = "OrchardTest_" + part.Id.ToString() + "_" + DateTime.Now.ToString("yyyyMMddHHmmss");

                            //_smsServices.SendSms(
                            //    part.NumberForTest.Split(';').Select(x => new SmsHQL { SmsPrefix = "", SmsNumber = x, Id = 0, Title = "Test" }).ToArray(),
                            //    messageToSms, part.Alias, IdSendToTest, part.HaveAlias);

                            // Destinatario
                            string numberTestSms = part.PrefixForTest.Trim() + part.NumberForTest.Trim();

                            Hashtable hs = new Hashtable();
                            hs.Add("SmsContactNumber", numberTestSms);

                            List <Hashtable> listaDestinatari = new List <Hashtable>();
                            listaDestinatari.Add(hs);

                            // Invio SMS a NumberForTest
                            var smsOutcome = _smsServices.SendSms(listaDestinatari.ToArray(), messageToSms, part.Alias, IdSendToTest, part.HaveAlias);
                            if (smsOutcome.ToUpper() == "TRUE")
                            {
                                _notifier.Information(T("SMS sent successfully to test phone number."));
                            }
                            else
                            {
                                _notifier.Error(T("Send SMS to test phone number failed."));
                                Logger.Error("Error sending SMS to test phone number. SendSMS return value: " + smsOutcome);
                            }
                        }
                    }
                }
            });

            OnPublished <SmsGatewayPart>((context, part) => {
                if (part.SendOnNextPublish && !part.SmsMessageSent)
                {
                    ContentItem ci = _orchardServices.ContentManager.Get(part.ContentItem.Id);
                    _taskManager.CreateTask("Laser.Orchard.SmsGateway.Task", DateTime.UtcNow.AddMinutes(1), ci);
                }
            });
        }