2013-11-13 22:27:35 +01:00
|
|
|
<!--
|
2014-11-26 21:37:54 +01:00
|
|
|
Copyright 2014 Andrew D Lindsay @AndrewDLindsay
|
2013-11-13 22:27:35 +01:00
|
|
|
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.
|
|
|
|
-->
|
|
|
|
|
2014-02-09 12:55:01 +01:00
|
|
|
<script type="text/x-red" data-template-name="twilio out">
|
2014-04-09 12:23:46 +02:00
|
|
|
<div class="form-row" id="node-input-credentials-row">
|
2014-07-27 21:09:19 +02:00
|
|
|
<label for="node-input-creds"><i class="fa fa-folder-o"></i> Credentials</label>
|
2014-04-09 12:23:46 +02:00
|
|
|
<select id="node-input-creds">
|
|
|
|
<option value="global">Use global credentials</option>
|
|
|
|
<option value="local">Use local credentials</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
<div class="form-row" id="node-input-twilio-row">
|
2014-07-27 21:09:19 +02:00
|
|
|
<label for="node-input-twilio"><i class="fa fa-user"></i> Twilio</label>
|
2014-04-09 12:23:46 +02:00
|
|
|
<input type="text" id="node-input-twilio">
|
|
|
|
</div>
|
2014-11-26 21:37:54 +01:00
|
|
|
<div class="form-row node-input-twiliotype-row">
|
|
|
|
<label for="node-input-twilioType"><i class="fa fa-list-ul"></i> Output</label>
|
|
|
|
<select id="node-input-twilioType" style="width:125px !important">
|
|
|
|
<option value="sms">SMS</option>
|
|
|
|
<option value="call">Call</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
2014-12-06 17:42:59 +01:00
|
|
|
|
2013-11-13 22:27:35 +01:00
|
|
|
<div class="form-row">
|
2014-12-06 17:42:59 +01:00
|
|
|
<label for="node-input-number">
|
|
|
|
<i class="fa fa-envelope-o" id="node-input-number-icon-sms"></i>
|
|
|
|
<i class="fa fa-phone hidden" id="node-input-number-icon-call"></i>
|
|
|
|
To</label>
|
2014-02-09 12:55:01 +01:00
|
|
|
<input type="text" id="node-input-number" placeholder="01234 5678901">
|
2013-11-13 22:27:35 +01:00
|
|
|
</div>
|
2014-11-26 21:37:54 +01:00
|
|
|
<div class="form-row hidden" id="node-input-twiliourl-row">
|
|
|
|
<label for="node-input-url"><i class="fa fa-globe"></i> URL</label>
|
|
|
|
<input type="text" id="node-input-url" placeholder="http://someurl.com/twiml.xml" >
|
|
|
|
</div>
|
2013-11-13 22:27:35 +01:00
|
|
|
<div class="form-row">
|
2014-07-27 21:09:19 +02:00
|
|
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
2013-11-13 22:27:35 +01:00
|
|
|
<input type="text" id="node-input-name" placeholder="Name">
|
|
|
|
</div>
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<script type="text/x-red" data-help-name="twilio out">
|
2014-11-26 21:37:54 +01:00
|
|
|
<p>Sends an SMS message or makes a call using the Twilio service.</p>
|
2014-12-06 17:42:59 +01:00
|
|
|
<p><code>msg.payload</code> can either contain the text of the SMS message,
|
|
|
|
<i>or</i> the URL of the <a href="https://www.twilio.com/docs/api/twiml" target = "_new">TWiML</a> used to create the call.
|
|
|
|
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>.
|
|
|
|
If the node is configured to make a call then the TWiML URL must be publically accessible.
|
2014-04-09 12:23:46 +02:00
|
|
|
<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 globally using either the settings file or a file
|
|
|
|
called 'twiliokey.js' located in the directory above node-red.</p>
|
|
|
|
<p>To use the settings.js file, add an entry such as:
|
|
|
|
<pre>twilio: { account:'My-ACCOUNT-SID', authtoken:'TWILIO-TOKEN', from:'FROM-NUMBER' }</pre></p>
|
|
|
|
<p>To use the 'twiliokey.js' file in the directory <b>above</b> node-red, use the following format:
|
|
|
|
<pre>module.exports = { account:'My-ACCOUNT-SID', authtoken:'TWILIO-TOKEN',from:'FROM-NUMBER' }</pre></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">
|
2014-09-03 17:12:26 +02:00
|
|
|
<label for="node-config-input-from"><i class="fa fa-envelope"></i> From</label>
|
2014-04-09 12:23:46 +02:00
|
|
|
<input type="text" id="node-config-input-from" placeholder="01234 5678901">
|
|
|
|
</div>
|
|
|
|
<div class="form-row">
|
2014-09-03 17:12:26 +02:00
|
|
|
<label for="node-config-input-token"><i class="fa fa-lock"></i> Token</label>
|
2014-04-09 12:23:46 +02:00
|
|
|
<input type="password" id="node-config-input-token">
|
|
|
|
</div>
|
|
|
|
<div class="form-row">
|
2014-09-03 17:12:26 +02:00
|
|
|
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
|
2014-04-09 12:23:46 +02:00
|
|
|
<input type="text" id="node-config-input-name" placeholder="Name">
|
|
|
|
</div>
|
2013-11-13 22:27:35 +01:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
2014-04-09 12:23:46 +02:00
|
|
|
(function() {
|
|
|
|
var hasGlobal = false;
|
|
|
|
$.getJSON('twilio-api/global',function(data) {
|
|
|
|
hasGlobal = data.hasToken;
|
|
|
|
});
|
2014-07-27 21:09:19 +02:00
|
|
|
|
2014-04-09 12:23:46 +02:00
|
|
|
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('');
|
|
|
|
}
|
2014-07-27 21:09:19 +02:00
|
|
|
|
2014-04-09 12:23:46 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
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) {}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-07-27 21:09:19 +02:00
|
|
|
|
2014-04-09 12:23:46 +02:00
|
|
|
RED.nodes.registerType('twilio out',{
|
|
|
|
category: 'output',
|
|
|
|
defaults: {
|
|
|
|
twilio:{type:"twilio-api",validate:function(v) {
|
|
|
|
return hasGlobal || (v && v!="_ADD_");
|
|
|
|
}},
|
2014-12-06 17:42:59 +01:00
|
|
|
twilioType: {value:"sms"},
|
|
|
|
url: {value:""},
|
2014-04-09 12:23:46 +02:00
|
|
|
number: {value:""},
|
|
|
|
name: {value:""}
|
|
|
|
},
|
|
|
|
color:"#FF595F",
|
|
|
|
inputs:1,
|
|
|
|
outputs:0,
|
|
|
|
icon: "twilio.png",
|
|
|
|
align: "right",
|
|
|
|
label: function() {
|
|
|
|
return this.name||this.title||"twilio";
|
|
|
|
},
|
|
|
|
labelStyle: function() {
|
|
|
|
return this.name?"node_label_italic":"";
|
|
|
|
},
|
|
|
|
oneditprepare: function() {
|
|
|
|
if (hasGlobal) {
|
|
|
|
$("#node-input-creds").change(function() {
|
|
|
|
var val = $(this).val();
|
|
|
|
if (val == "global") {
|
|
|
|
$("#node-input-twilio-row").hide();
|
|
|
|
} else {
|
|
|
|
$("#node-input-twilio-row").show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$("#node-input-credentials-row").show();
|
|
|
|
if (!this.twilio) {
|
|
|
|
$("#node-input-creds").val("global");
|
|
|
|
} else {
|
|
|
|
$("#node-input-creds").val("local");
|
|
|
|
}
|
|
|
|
$("#node-input-creds").change();
|
|
|
|
} else {
|
|
|
|
$("#node-input-credentials-row").hide();
|
|
|
|
}
|
2014-11-26 21:37:54 +01:00
|
|
|
|
|
|
|
if (this.twilioType == null) {
|
|
|
|
if (this.url == "") {
|
|
|
|
this.twilioType = "call";
|
|
|
|
} else {
|
|
|
|
this.twilioType = "sms";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$("#node-input-twilioType").change(function() {
|
|
|
|
var twilioType = $("#node-input-twilioType option:selected").val();
|
|
|
|
if (twilioType == "call") {
|
|
|
|
$("#node-input-twiliourl-row").show();
|
2014-12-06 17:42:59 +01:00
|
|
|
$("#node-input-number-icon-call").show();
|
|
|
|
$("#node-input-number-icon-sms").hide();
|
2014-11-26 21:37:54 +01:00
|
|
|
} else {
|
|
|
|
$("#node-input-twiliourl-row").hide();
|
2014-12-06 17:42:59 +01:00
|
|
|
$("#node-input-number-icon-call").hide();
|
|
|
|
$("#node-input-number-icon-sms").show();
|
2014-11-26 21:37:54 +01:00
|
|
|
}
|
|
|
|
});
|
2014-12-06 17:42:59 +01:00
|
|
|
$("#node-input-twilioType").val(this.twilioType);
|
|
|
|
$("#node-input-twilioType").change();
|
2014-04-09 12:23:46 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
})();
|
2013-11-13 22:27:35 +01:00
|
|
|
</script>
|