Esempio n. 1
0
        private string BuildActionBody(string idSuffix, SendMessageActionConfig config)
        {
            LogDebug("Building Action Body");
            string toNumber = config.ToNumber;

            if (toNumber == null)
            {
                toNumber = "";
            }

            string message = config.Message;

            if (message == null)
            {
                message = "";
            }

            var toField = new Scheduler.clsJQuery.jqTextBox("ToNumber" + idSuffix, "", toNumber, "Events", 20, false);

            toField.label = "<strong>To</strong>";

            var messageField = new Scheduler.clsJQuery.jqTextBox("Message" + idSuffix, "", message, "Events", 100, false);

            messageField.label = "<strong>Message</strong>";

            var saveBtn = new Scheduler.clsJQuery.jqButton("submit" + idSuffix, "Save", "Events", true);

            return(toField.Build() + "<br>" + messageField.Build() + "<br>" + saveBtn.Build());
        }
Esempio n. 2
0
        public void SendMessageToTwilio(PluginConfig pluginConfig, SendMessageActionConfig messageConfig)
        {
            this.Log.LogDebug("Starting SendMessageToTwilio");

            PhoneNumber to;
            PhoneNumber from = new PhoneNumber(pluginConfig.FromNumber);

            string message = messageConfig.Message;

            if (string.IsNullOrEmpty(message))
            {
                this.Log.LogWarning("No message configured! Message won't send");
                return;
            }
            if (string.IsNullOrEmpty(messageConfig.ToNumber))
            {
                this.Log.LogWarning("No 'To' number configured! Message won't send");
                return;
            }

            to = new PhoneNumber(HS.ReplaceVariables(messageConfig.ToNumber));

            message = HS.ReplaceVariables(message);

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            TwilioClient.Init(pluginConfig.AccountSID, pluginConfig.AuthToken);

            var publishedMessage = MessageResource.Create(
                to,
                from: from,
                body: message
                );

            this.Log.LogInfo("Published message with Sid: " + publishedMessage.Sid);
        }
Esempio n. 3
0
        public static SendMessageActionConfig DeserializeActionConfig(byte[] configuration)
        {
            var srx2 = new SendMessageActionConfig();

            if (configuration == null || configuration.Length == 0)
            {
                srx2.ToNumber = "";
                srx2.Message  = "";
            }
            else
            {
                try
                {
                    using (var ms = new MemoryStream(configuration))
                    {
                        using (var br = new BinaryReader(ms, Encoding.UTF8))
                        {
                            srx2.ToNumber = br.ReadString();
                            srx2.Message  = br.ReadString();
                        }
                    }
                }
                catch
                {
                    srx2.ToNumber = "";
                    srx2.Message  = "Error reading config";
                }
            }

            return(srx2);
        }
Esempio n. 4
0
        public override bool ActionConfigured(IPlugInAPI.strTrigActInfo actionInfo)
        {
            LogDebug("Checking if action is configured...");
            var config = SendMessageActionConfig.DeserializeActionConfig(actionInfo.DataIn);

            return(config.ToNumber != null &&
                   config.ToNumber.Length > 0 &&
                   config.Message != null &&
                   config.Message.Length > 0);
        }
Esempio n. 5
0
        public static byte[] SerializeActionConfig(SendMessageActionConfig cfg)
        {
            if (cfg == null)
            {
                throw new HspiException("configuration parameter is required");
            }

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

            return(Encoding.Unicode.GetBytes(jsonString));
        }
Esempio n. 6
0
        public override string ActionFormatUI(IPlugInAPI.strTrigActInfo actionInfo)
        {
            LogDebug("Getting formatted action");
            switch (actionInfo.TANumber)
            {
            case ActionSendMessageTANumber:
                var config = SendMessageActionConfig.DeserializeActionConfig(actionInfo.DataIn);
                return($"Twilio sends a message to {config.ToNumber}");

            default:
                return(base.ActionFormatUI(actionInfo));
            }
        }
Esempio n. 7
0
        public override string ActionBuildUI([AllowNull] string uniqueControlId, IPlugInAPI.strTrigActInfo actionInfo)
        {
            switch (actionInfo.TANumber)
            {
            case ActionSendMessageTANumber:
                System.Text.StringBuilder stb = new System.Text.StringBuilder();
                stb.Append(PageBuilderAndMenu.clsPageBuilder.DivStart(uniqueControlId + "div", ""));
                stb.Append(BuildActionBody(
                               uniqueControlId,
                               SendMessageActionConfig.DeserializeActionConfig(actionInfo.DataIn)
                               ));
                stb.Append(PageBuilderAndMenu.clsPageBuilder.DivEnd());
                return(stb.ToString());

            default:
                return(base.ActionBuildUI(uniqueControlId, actionInfo));
            }
        }
Esempio n. 8
0
        public override object PluginFunction([AllowNull] string functionName, [AllowNull] object[] parameters)
        {
            try
            {
                switch (functionName)
                {
                case null:
                    return(null);

                case "SendMessage":
                    LogInfo("Sending message via 'SendMessage' plugin function");

                    if (parameters == null || parameters.Length != 2)
                    {
                        string count = parameters == null ? "none" : parameters.Length.ToString();
                        LogError("SendMessage: expected 2 parameters, but found " + count);
                        return(null);
                    }

                    string toNumber = parameters[0] as string;
                    string message  = parameters[1] as string;

                    SendMessageActionConfig config = new SendMessageActionConfig
                    {
                        ToNumber = toNumber,
                        Message  = message
                    };

                    SendMessageToTwilio(config);
                    return(null);

                default:
                    return(null);
                }
            }
            catch (Exception ex)
            {
                LogWarning($"Failed to execute function with {ex.GetFullMessage()}");
                return(null);
            }
        }
Esempio n. 9
0
        public override bool HandleAction(IPlugInAPI.strTrigActInfo actionInfo)
        {
            try
            {
                switch (actionInfo.TANumber)
                {
                case ActionSendMessageTANumber:
                    var config = SendMessageActionConfig.DeserializeActionConfig(actionInfo.DataIn);
                    SendMessageToTwilio(config);
                    return(true);

                default:
                    return(base.HandleAction(actionInfo));
                }
            }
            catch (Exception ex)
            {
                LogWarning($"Failed to execute action with {ex.GetFullMessage()}");
                return(false);
            }
        }
Esempio n. 10
0
        public static byte[] SerializeActionConfig(SendMessageActionConfig cfg)
        {
            if (cfg == null)
            {
                throw new HspiException("configuration parameter is required");
            }

            string toNumber = cfg.ToNumber;
            string message  = cfg.Message;

            byte[] buffer;

            MemoryStream ms = null;
            BinaryWriter bw = null;

            try
            {
                ms = new MemoryStream();
                bw = new BinaryWriter(ms, Encoding.UTF8);
                bw.Write(toNumber);
                bw.Write(message);

                buffer = ms.ToArray();
            }
            finally
            {
                if (bw != null)
                {
                    bw.Dispose();
                }
                if (ms != null)
                {
                    ms.Dispose();
                }
            }

            return(buffer);
        }
Esempio n. 11
0
        private static SendMessageActionConfig DeserializeLegacyActionConfig(byte[] configuration)
        {
            var configInstance = new SendMessageActionConfig();

            try
            {
                using (var ms = new MemoryStream(configuration))
                {
                    using (var br = new BinaryReader(ms, Encoding.UTF8))
                    {
                        configInstance.ToNumber = br.ReadString();
                        configInstance.Message  = br.ReadString();
                    }
                }
            }
            catch
            {
                configInstance.ToNumber = "";
                configInstance.Message  = "Error reading config";
            }

            return(configInstance);
        }
        public void SendMessageToTwilio(PluginConfig pluginConfig, SendMessageActionConfig messageConfig)
        {
            this.Log.LogDebug("Starting SendMessageToTwilio");

            PhoneNumber to;
            PhoneNumber from    = new PhoneNumber(pluginConfig.FromNumber);
            string      message = messageConfig.Message;

            if (message == null || message.Length == 0)
            {
                this.Log.LogWarning("No message configured! Message won't send");
                return;
            }
            if (messageConfig.ToNumber == null || messageConfig.ToNumber.Length == 0)
            {
                this.Log.LogWarning("No 'To' number configured! Message won't send");
                return;
            }
            else
            {
                to = new PhoneNumber(HS.ReplaceVariables(messageConfig.ToNumber));
            }

            message = HS.ReplaceVariables(message);


            TwilioClient.Init(pluginConfig.AccountSID, pluginConfig.AuthToken);

            var publishedMessage = MessageResource.Create(
                to: to,
                from: from,
                body: message
                );

            this.Log.LogInfo("Published message with Sid: " + publishedMessage.Sid);
        }
Esempio n. 13
0
        public static SendMessageActionConfig DeserializeActionConfig(byte[] configuration)
        {
            var configInstance = new SendMessageActionConfig();

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

            return(configInstance);
        }
Esempio n. 14
0
        public override IPlugInAPI.strMultiReturn ActionProcessPostUI([AllowNull] NameValueCollection postData, IPlugInAPI.strTrigActInfo actionInfo)
        {
            LogDebug("Handling ActionProcessPostUI");
            var value = new IPlugInAPI.strMultiReturn();

            value.TrigActInfo = actionInfo;

            var config = new SendMessageActionConfig();

            if (postData != null && postData.HasKeys())
            {
                foreach (var key in postData.Keys)
                {
                    LogDebug(key + " has a value of " + postData[key.ToString()]);
                }

                LogDebug("Setting number to " + postData[0]);
                LogDebug("Setting message to " + postData[1]);
                config.ToNumber = postData[0];
                config.Message  = postData[1];
                value.DataOut   = SendMessageActionConfig.SerializeActionConfig(config);
            }
            return(value);
        }
Esempio n. 15
0
        public string PostBackProc(string data, [AllowNull] string user, int userRights)
        {
            NameValueCollection parts = HttpUtility.ParseQueryString(data);

            string form = parts["id"];

            if (form == "id_sendTestButton")
            {
                PluginConfig testConfig;
                using (testConfig = new PluginConfig(HS, offline: true))
                {
                    PopulatePluginConfig(testConfig, parts);

                    string toNumber = parts["testNumber"];

                    TwilioServiceFacade twilioService = new TwilioServiceFacade(HS, testConfig.DebugLogging);

                    SendMessageActionConfig messageConfig = new SendMessageActionConfig();
                    messageConfig.ToNumber = toNumber;
                    messageConfig.Message  = @"$time: this is a test message from the HomeSeer Twilio Plugin.";

                    try
                    {
                        twilioService.SendMessageToTwilio(testConfig, messageConfig);
                        this.divToUpdate.Add(SuccessDivId, @"Test message was sent successfully!");
                        this.divToUpdate.Add(ErrorDivId, string.Empty);
                    }
                    catch (Exception e)
                    {
                        string errorMessage = e.Message;
                        if (errorMessage.Equals("Authenticate"))
                        {
                            errorMessage = "Invalid Auth Token";
                        }
                        this.divToUpdate.Add(SuccessDivId, string.Empty);
                        this.divToUpdate.Add(ErrorDivId, errorMessage);
                    }
                }
            }
            else if (form == NameToIdWithPrefix(SaveButtonName))
            {
                StringBuilder results = new StringBuilder();

                // Validate
                if (string.IsNullOrWhiteSpace(parts[AuthTokenId]))
                {
                    results.AppendLine("Auth Token is not Valid.<br>");
                }

                if (string.IsNullOrWhiteSpace(parts[AccountSIDId]))
                {
                    results.AppendLine("Account SID is not Valid.<br>");
                }

                if (string.IsNullOrWhiteSpace(parts[FromNumberId]))
                {
                    results.AppendLine("From Number is not Valid.<br>");
                }

                if (results.Length > 0)
                {
                    this.divToUpdate.Add(SuccessDivId, string.Empty);
                    this.divToUpdate.Add(ErrorDivId, results.ToString());
                }
                else
                {
                    this.divToUpdate.Add(SuccessDivId, "Settings have been saved successfully!");
                    this.divToUpdate.Add(ErrorDivId, string.Empty);

                    PopulatePluginConfig(this.pluginConfig, parts);

                    this.pluginConfig.FireConfigChanged();
                }
            }

            return(base.postBackProc(Name, data, user, userRights));
        }
Esempio n. 16
0
 public void SendMessageToTwilio(SendMessageActionConfig config)
 {
     this.twilioService.SendMessageToTwilio(this.pluginConfig, config);
 }