UNPKG

node-red-bluemix-nodes

Version:

A collection of extra Node-RED nodes for IBM Bluemix.

180 lines (166 loc) 7.2 kB
<!-- Copyright 2013 Andrew D Lindsay @AndrewDLindsay http://blog.thiseldo.co.uk Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <script type="text/x-red" data-template-name="twilio out"> <div class="form-row"> <label for="node-input-service"><i class="icon-folder-close"></i> Service</label> <select id="node-input-service"> <option value="" disabled></option> <option value="_ext_">External service</option> </select> </div> <div class="form-row" id="node-input-twilio-row"> <label for="node-input-twilio"><i class="icon-user"></i> Twilio</label> <input type="text" id="node-input-twilio"> </div> <div class="form-row" id="node-input-from-row"> <label for="node-input-from"><i class="icon-user"></i> From</label> <input type="text" id="node-input-from" placeholder="+4412345678901"> </div> <div class="form-row"> <label for="node-input-number"><i class="icon-envelope"></i> SMS to</label> <input type="text" id="node-input-number" placeholder="+4412345678901"> </div> <div class="form-row"> <label for="node-input-name"><i class="icon-tag"></i> Name</label> <input type="text" id="node-input-name" placeholder="Name"> </div> </script> <script type="text/x-red" data-help-name="twilio out"> <p>Sends an SMS message using the Twilio service.</p> <p><code>msg.payload</code> is used as the body of the message. The node can be configured with the number to send the message to. Alternatively, if the number is left blank, it can be set using <code>msg.topic</code>.</p> <p>You must have an account with Twilio to use this node. You can register for one <a href="https://www.twilio.com/">here</a>.</p> <p>You can either set your account details within the node, or provide it as a bound service within Bluemix.</p> </script> <script type="text/x-red" data-template-name="twilio-api"> <div class="form-row"> <label for="node-config-input-sid">Account SID</label> <input type="text" id="node-config-input-sid"> </div> <div class="form-row"> <label for="node-config-input-from"><i class="icon-envelope"></i> From</label> <input type="text" id="node-config-input-from" placeholder="01234 5678901"> </div> <div class="form-row"> <label for="node-config-input-token"><i class="icon-lock"></i> Token</label> <input type="password" id="node-config-input-token"> </div> <div class="form-row"> <label for="node-config-input-name"><i class="icon-tag"></i> Name</label> <input type="text" id="node-config-input-name" placeholder="Name"> </div> </script> <script type="text/javascript"> (function() { RED.nodes.registerType('twilio-api',{ category: 'config', defaults: { sid: {value:"",required:true}, from: {value:"",required:true}, // token -> credentials name: { value: ""} }, label: function() { return this.name||this.from; }, oneditprepare: function() { $.getJSON('twilio-api/'+this.id,function(data) { if (data.hasToken) { $('#node-config-input-token').val('__PWRD__'); } else { $('#node-config-input-token').val(''); } }); }, oneditsave: function() { var newToken = $('#node-config-input-token').val(); if (newToken != '__PWRD__') { var credentials = {}; credentials.token = newToken; $.ajax({ url: 'twilio-api/'+this.id, type: 'POST', data: credentials, success:function(result){} }); } }, ondelete: function() { $.ajax({ url: 'twilio-api/'+this.id, type: 'DELETE', success: function(result) {} }); } }); RED.nodes.registerType('twilio out',{ category: 'output', defaults: { service: {value:"", required: true}, twilio:{type:"twilio-api",validate:function(v) { return (this.service && this.service != "_ext_") || (v&&v!="_ADD_"); }}, from: {value:""}, number: {value:""}, name: {value:""} }, color:"#FF595F", inputs:1, outputs:0, icon: "twilio.png", align: "right", label: function() { var api = RED.nodes.node(this.twilio); if (this.name) { return this.name; } if (this.service == "_ext_") { return (api?api.label():"twilio"); } return this.service||"twilio"; }, labelStyle: function() { return this.name?"node_label_italic":""; }, oneditprepare: function() { var select = $('#node-input-service') var node = this; $.getJSON('twilio-api/vcap/',function(data) { var last = select.children().last(); var opts = []; for (var i =0;i<data.length;i++) { opts.push('<option value="'+data[i].name+'"'+(node.service==data[i].name?" selected":"")+'>'+data[i].name+'</option>'); } if (opts.length == 0) { node.service = "_ext_"; select.find("option").filter(function() {return $(this).val() == node.service;}).attr('selected',true); } else { last.before(opts.join("")); } select.change(); }); select.change(function() { var service = select.val(); if (service == "_ext_") { $("#node-input-twilio-row").show(); $("#node-input-from-row").hide(); } else { $("#node-input-twilio-row").hide(); $("#node-input-from-row").show(); } }); } }); })(); </script>