private bool Push(ActionContext context) {
            try {
                // Default to normal priority
                var priority = MessagePriority.Normal;
                if (context.Properties.ContainsKey("Priority"))
                    priority = context.Properties["Priority"] == "High"
                                   ? MessagePriority.High
                                   : MessagePriority.Normal;

                // Default to current DateTime
                DateTime timestamp;
                if (!context.Properties.ContainsKey("Timestamp")
                    || !DateTime.TryParse(_tokenizer.Replace(context.Properties["Timestamp"], context.Tokens), out timestamp))
                    timestamp = DateTime.Now;

                var message = new PushoverMessageBase {
                    User = string.IsNullOrEmpty(context.Properties["PushoverUser"])
                               ? context.Properties["UserSelect"]
                               : _tokenizer.Replace(context.Properties["PushoverUser"], context.Tokens),
                    Title = _tokenizer.Replace(context.Properties["MessageTitle"], context.Tokens),
                    Message = _tokenizer.Replace(context.Properties["MessageBody"], context.Tokens),
                    Url = _tokenizer.Replace(context.Properties["MessageUrl"], context.Tokens),
                    UrlTitle = _tokenizer.Replace(context.Properties["MessageUrlTitle"], context.Tokens),
                    Priority = priority,
                    Timestamp = timestamp
                };

                _pushover.Push(message);
            }
            catch (Exception e) {
                Logger.Error(e, "Failed to push message to Pushover");
            }
            return true;
        }
        private LocalizedString DisplayDelayedAction(ActionContext context) {
            var amount = Convert.ToInt32(context.Properties["Amount"]);
            var type = context.Properties["Unity"];
            var ruleId = Convert.ToInt32(context.Properties["RuleId"]);

            var rule = _repository.Get(ruleId);

            return T.Plural("Triggers \"{1}\" in {0} {2}", "Triggers \"{1}\" in {0} {2}s", amount, rule.Name, type);
        }
        private bool CreateDelayedAction(ActionContext context) {
            var amount = Convert.ToInt32(context.Properties["Amount"]);
            var type = context.Properties["Unity"];
            var ruleId = Convert.ToInt32(context.Properties["RuleId"]);

            var scheduledActionTask = _contentManager.New("ScheduledActionTask").As<ScheduledActionTaskPart>();
            var rule = _repository.Get(ruleId);

            var when = _clock.UtcNow;

            switch (type) {
                case "Minute":
                    when = when.AddMinutes(amount);
                    break;
                case "Hour":
                    when = when.AddHours(amount);
                    break;
                case "Day":
                    when = when.AddDays(amount);
                    break;
                case "Week":
                    when = when.AddDays(7 * amount);
                    break;
                case "Month":
                    when = when.AddMonths(amount);
                    break;
                case "Year":
                    when = when.AddYears(amount);
                    break;
            }

            foreach (var action in rule.Actions) {
                var actionRecord = new ActionRecord {
                    Category = action.Category,
                    Position = action.Position,
                    Type = action.Type,
                    Parameters = _tokenizer.Replace(action.Parameters, context.Tokens)
                };

                _actionRecordRepository.Create(actionRecord);

                var scheduledAction = new ScheduledActionRecord { ActionRecord = actionRecord };
                _scheduledActionRecordRepository.Create(scheduledAction);

                scheduledActionTask.ScheduledActions.Add(scheduledAction);
            }


            _contentManager.Create(scheduledActionTask, VersionOptions.Draft);

            _scheduledTaskManager.CreateTask("TriggerRule", when, scheduledActionTask.ContentItem);

            return true;
        }
        private bool Action(ActionContext context)
        {
            int Id = Convert.ToInt32(0);
            string name = context.Properties["Name"];
            int triggerType = Convert.ToInt32(context.Properties["Trigger"]);
            string triggerData = context.Properties["TriggerData"];
            int actionType = Convert.ToInt32(context.Properties["Action"]);
            string actionData = context.Properties["ActionData"];

            return true;
        }
示例#5
0
        public bool ImportCommentsFromDisqus(ActionContext context)
        {
            try
            {
                _disqusSynchronizationService.ImportComments();

                return true;
            }
            catch
            {
                return false;
            }
        }
        private bool Send(ActionContext context) {
            var channel = context.Properties.GetValue("Channel");
            var recipient = context.Properties.GetValue("Recipient");
            var dataTokens = ParseDataTokens(context.Properties);

            if(channel == null)
                throw new InvalidOperationException("No channel has been specified");

            if(recipient == null)
                throw new InvalidOperationException("No recipient has been specified");

            var properties = dataTokens.ToDictionary(x => x.Key, x => x.Value);

            foreach (var token in context.Tokens) {
                properties[token.Key] = token.Value.ToString();
            }

            properties["Recipient"] = recipient;
            properties["Channel"] = context.Properties.GetValue("Channel");
            properties["TemplateId"] = context.Properties.GetValue("TemplateId");

            _messageManager.Send(new[] {recipient}, MessageType, channel, properties);
            return true;
        }
        public void ExecuteActions(IEnumerable<ActionRecord> actions, Dictionary<string, object> tokens)
        {
            var actionDescriptors = DescribeActions().SelectMany(x => x.Descriptors).ToList();

            // execute each action associated with this rule
            foreach (var actionRecord in actions) {
                var actionCategory = actionRecord.Category;
                var actionType = actionRecord.Type;

                // look for the specified Event target/type
                var descriptor = actionDescriptors.FirstOrDefault(x => actionCategory == x.Category && actionType == x.Type);

                if (descriptor == null) {
                    continue;
                }

                // evaluate the tokens
                var parameters = _tokenizer.Replace(actionRecord.Parameters, tokens);

                var properties = FormParametersHelper.FromString(parameters);
                var context = new ActionContext { Properties = properties, Tokens = tokens };

                // execute the action
                try {
                    var continuation = descriptor.Action(context);

                    // early termination of the actions ?
                    if (!continuation) {
                        break;
                    }
                }
                catch (Exception e) {
                    Logger.Error(e, "An action could not be executed.");

                    // stop executing other actions
                    break;
                }
            }
        }
 private LocalizedString Display(ActionContext context)
 {
     return T.Plural("Action Power! {1}", "Action Power! {1}", 1);
 }
 private LocalizedString DislayAction(ActionContext context) {
     var layoutId = context.Properties.GetValue("TemplateId").AsInt32();
     var channel = context.Properties.GetValue("Channel") ?? "unknown";
     var layout = layoutId != null ? _messageTemplateService.GetTemplate(layoutId.Value) : default(MessageTemplatePart);
     return T("Send a templated message using the [{0}] template and the [{1}] channel", layout != null ? layout.Title : "unkown", channel);
 }