node-red-contrib-chatbot
Version:
REDBot a Chat bot for a full featured chat bot for Telegram, Facebook Messenger and Slack. Almost no coding skills required
149 lines (141 loc) • 5.57 kB
HTML
<script type="text/javascript">
$.RedBot.registerType('chatbot-rivescript', {
category: $.RedBot.config.name + ' Parsers',
color: '#FFCC66',
defaults: {
name: {
value: ''
},
script: {
value: '! version = 2.0\n\n'
},
scriptFile: {
value: ''
},
debug: {
value: false
}
},
inputs: 1,
outputs: 2,
paletteLabel: 'Rivescript',
icon: 'chatbot-rivescript.png',
label: function() {
return this.name || 'RiveScript';
},
oneditprepare: function() {
var node = this;
function updateUI(type) {
if (type === 'script-file') {
$('.form-row-editor').hide();
$('#node-input-scriptFile').attr('disabled', false);
$('input[value="script-file"]').prop('checked', true);
node.editor.setValue('');
} else if (type === 'script') {
$('.form-row-editor').show();
$('#node-input-scriptFile').attr('disabled', true).val('');
$('input[value="script"]').prop('checked', true);
node.editor.setValue('! version = 2.0\n\n');
}
}
var isScriptFile = this.scriptFile != null && this.scriptFile !== '';
node.editor = RED.editor.createEditor({
id: 'node-input-script-editor',
mode: 'ace/mode/text',
value: $('#node-input-script').val(),
globals: {}
});
$('input[name="rivescript-type"]')
.click(function() {
updateUI($(this).val());
});
updateUI(isScriptFile ? 'script-file' : 'script');
node.editor.setValue(this.script);
},
oneditsave: function() {
$('#node-input-script').val(this.editor.getValue());
delete this.editor;
},
oneditresize: function(size) {
var dialogForm = $('#dialog-form');
var rowName = $('.form-row-name', dialogForm);
var rowScriptFile = $('.form-row-scriptFile', dialogForm);
var rowDebug = $('.form-row-debug', dialogForm);
var height = dialogForm.height() - rowName.height() - rowScriptFile.height() - rowDebug.height() - 30;
$('.node-text-editor').css('height', height + 'px');
this.editor.resize();
}
});
</script>
<script type="text/x-red" data-template-name="chatbot-rivescript">
<div class="form-row form-row-name">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
<input type="hidden" id="node-input-script" autofocus="autofocus">
</div>
<div class="form-row form-row-scriptFile">
<input type="radio" value="script-file" name="rivescript-type" style="width:20px;margin-top:-2px;"> Script file
<input type="text" id="node-input-scriptFile" placeholder="*.rive" style="width:250px;">
</div>
<div class="form-row form-row-scriptFile">
<input type="radio" value="script" name="rivescript-type" style="width:20px;margin-top:-2px;"> RiveScript
</div>
<div class="form-row form-row-editor">
<div style="height: 250px; min-height:150px;" class="node-text-editor" id="node-input-script-editor"></div>
</div>
<div class="form-row form-row-debug">
<label for="node-input-answer">Debug</label>
<input type="checkbox" value="true" id="node-input-debug">
<span style="max-width: 460px;font-size: 12px;color: #999999;line-height: 14px;display:block;">
Show <b>Rivescript</b> debug information in the Node-RED debug panel
</span>
</div>
</script>
<script type="text/x-red" data-help-name="chatbot-rivescript"><p>Reply to an incoming message using <strong>RiveScript</strong>.</p>
<p>This node answers with a string to the first output if the script is able to elaborate a reply, otherwise a string error is sent to the second output. This node output is generally connected to a <code>Message Node</code>. The node will not try to parse command-like sentences (for example <code>/my_command</code>).</p>
<p>A <strong>RiveScript</strong> file looks like</p>
<pre><code class="language-plain">! version = 2.0
+ hello bot
- Hello, human!
</code></pre>
<p>It’s also able to parse the sentence and store data to the chat context</p>
<pre><code class="language-plain">! version = 2.0
+ my name is guido
- &lt;set name=&lt;star&gt;&gt;ok, I'll remember your name as &lt;get name&gt;
</code></pre>
<p><strong>RiveScript</strong> can get/set variables in the chat context.</p>
<p>You can use the chat context variable <em>topic</em> to scope some <strong>RiveScript</strong> triggers into specific subjects</p>
<pre><code class="language-plain">! version = 2.0
> topic name_ok
+ *
+ hello!
< topic
</code></pre>
<p>In this case the trigger <em>"*"</em> will be executed only if the topic is <em>name_ok</em> (it’s possible to set the intent using a <code>Context node</code> or in <strong>RiveScript</strong> with <code>{topic=new_topic}</code>). Note that <strong>RiveScript</strong>’s <em>topic</em> and <strong>RedBot</strong>’s <em>intent</em> are the same thing.</p>
<p>Read a <strong>RiveScript</strong> tutorial <a href="https://www.rivescript.com/docs/tutorial">tutorial here</a></p>
<p>Available parameters for the <code>msg.payload</code></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody><tr>
<td>script</td>
<td>string</td>
<td>The Rivescript script</td>
</tr>
<tr>
<td>scriptFile</td>
<td>string</td>
<td>Absolute path of Rivescript file</td>
</tr>
<tr>
<td>debug</td>
<td>boolean</td>
<td>Show debug information</td>
</tr>
</tbody></table>
</script>