Ejemplo n.º 1
0
        public static byte[] SerializeTriggerConfig(ReceiveMessageTriggerConfig cfg)
        {
            if (cfg == null)
            {
                throw new HspiException("configuration parameter is required");
            }

            string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(cfg);

            return(Encoding.Unicode.GetBytes(jsonString));
        }
Ejemplo n.º 2
0
        private void DoRefresh()
        {
            LogDebug("Refreshing Triggers");

            var triggers = Callback.TriggerMatches(Name, TriggerReceiveMessageTANumber, -1);

            if (triggers == null || triggers.Length == 0)
            {
                LogDebug("No triggers exist; aborting refresh");
                return;
            }

            var messages = twilioService.GetMessagesFromTwilio(pluginConfig, triggerRefreshFrequencyMillis / 1000);

            LogDebug(string.Format("Checking triggers against {0} messages", messages.Count));

            foreach (var strTrigActInfo in triggers)
            {
                if (ShutdownCancellationToken.IsCancellationRequested)
                {
                    break;
                }

                var config = ReceiveMessageTriggerConfig.DeserializeTriggerConfig(strTrigActInfo.DataIn);
                if (config.IsValid())
                {
                    string normalizedConfigFrom = NormalizePhoneNumber(config.FromNumber);
                    string messageToLower       = config.Message.ToLower();
                    bool   shouldFire           = messages.Exists((MessageResource obj) =>
                    {
                        bool bodyMatches = obj.Body.ToLower().Contains(messageToLower);
                        bool fromMatches = normalizedConfigFrom.IsNullOrWhiteSpace() || normalizedConfigFrom == NormalizePhoneNumber(obj.From.ToString());
                        return(fromMatches && bodyMatches);
                    });

                    if (shouldFire)
                    {
                        LogDebug("Firing trigger");
                        Callback.TriggerFire(Name, strTrigActInfo);
                    }
                }
                else
                {
                    LogDebug("Skipping trigger with invalid config");
                }
            }
        }
Ejemplo n.º 3
0
        public static ReceiveMessageTriggerConfig DeserializeTriggerConfig(byte[] configuration)
        {
            var configInstance = new ReceiveMessageTriggerConfig();

            if (configuration == null || configuration.Length == 0)
            {
                configInstance.FromNumber = "";
                configInstance.Message    = "";
            }
            else
            {
                string jsonString = Encoding.Unicode.GetString(configuration);
                configInstance = Newtonsoft.Json.JsonConvert.DeserializeObject <ReceiveMessageTriggerConfig>(jsonString);
            }

            return(configInstance);
        }