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
181 lines (174 loc) • 8.4 kB
HTML
<script type="text/javascript">
$.RedBot.registerType('chatbot-broadcast', {
category: $.RedBot.config.name + ' Platforms',
color: '#FFCC66',
defaults: {
bot: {
value: '',
type: 'chatbot-facebook-node',
required: true
},
botProduction: {
value: '',
type: 'chatbot-facebook-node',
required: false
},
command: {
value: 'store'
},
broadcastId: {
value: ''
},
messageId: {
value: ''
},
notificationType: {
value: 'REGULAR'
},
messagingType: {
value: ''
},
sendAt: {
value: ''
}
},
inputs: 1,
outputs: 1,
icon: 'chatbot-receiver.png',
paletteLabel: 'Broadcast API',
label: function () {
return 'Broadcast API' + (this.command != null ? ' (' + this.command + ')' : '');
},
oneditprepare: function() {
$('#node-input-command')
.change(function() {
var command = $(this).val();
$('.form-row.only-for').hide();
$('.form-row.only-for-' + command).show();
});
$('.form-row.only-for').hide();
$('.form-row.only-for-' + this.command).show();
console.log('set date', this.sendAt);
var sendAt = new Date(this.sendAt);
if (sendAt instanceof Date && !isNaN(sendAt)) {
$('#node-input-sendDate').val(sendAt.getFullYear() + '-' + (sendAt.getMonth() + 1) + '-' + sendAt.getDate());
$('#node-input-sendTime').val(sendAt.getHours() + ':' + sendAt.getMinutes());
}
},
oneditsave: function() {
// parse date
var date = $('#node-input-sendDate').val();
var time = $('#node-input-sendTime').val();
if (date != null && date != '' && time != null && time != '') {
var dateTime = new Date(date + ' ' + time);
this.sendAt = dateTime.toISOString();
}
}
});
</script>
<script type="text/x-red" data-template-name="chatbot-broadcast">
<div class="form-row">
<label for="node-input-bot" style="display:block;width:100%;">Bot configuration <span class="redbot-environment">(development)</span></label>
<input type="text" id="node-input-bot" placeholder="Bot">
</div>
<div class="form-row" style="margin-top:25px;">
<label for="node-input-botProduction" style="display:block;width:100%;">Bot configuration <span class="redbot-environment">(production)</span></label>
<input type="text" id="node-input-botProduction" placeholder="Bot">
</div>
<div class="redbot-form-hint">
Bot for <strong>production</strong> will be launched only if the global variable <em>"environment"</em> in <em>settings.js</em> is set to <em>"production"</em>, otherwise will be used the configuration for <strong>development</strong>.
</div>
<br/>
<div class="form-row">
<label for="node-input-command" style="width:150px;">Method</label>
<select id="node-input-command" placeholder="Broadcast method">
<option value="store">Store message</option>
<option value="metrics">Get message metrics</option>
<option value="status">Get message status</option>
<option value="schedule">Schedule message</option>
<option value="list">List scheduled messages</option>
<option value="cancel">Cancel scheduled message</option>
</select>
</div>
<div class="form-row only-for only-for-metrics only-for-status only-for-cancel">
<label for="node-input-broadcastId" style="width:150px;">Broadcast Id</label>
<input type="text" id="node-input-broadcastId" placeholder="Broadcast Id" style="width:300px;">
</div>
<div class="form-row only-for">
<label for="node-input-messageId" style="width:150px;">Message Id</label>
<input type="text" id="node-input-messageId" placeholder="Message Id" style="width:300px;">
</div>
<div class="form-row only-for only-for-schedule">
<label for="node-input-sendDate" style="width:150px;">Schedule At</label>
<input type="date" id="node-input-sendDate" placeholder="Schedule At" style="width:150px;">
<input type="time" id="node-input-sendTime" placeholder="Schedule At" style="width:100px;">
</div>
<div class="form-row only-for only-for-store">
<label for="node-input-messagingType" style="width:150px;">Message Type</label>
<input type="text" id="node-input-messagingType" placeholder="Messaging Type" style="width:300px;">
</div>
<div class="form-row only-for only-for-store">
<label for="node-input-notificationType" style="width:150px;">Notification Type</label>
<select id="node-input-notificationType" placeholder="Broadcast method">
<option value="REGULAR">Regular</option>
<option value="SILENT_PUSH">Silent Push</option>
<option value="NO_PUSH">No Push</option>
</select>
</div>
</script>
<script type="text/x-red" data-help-name="chatbot-broadcast"><p>The <code>Broadcast node</code> allows sending a message to all users that have an open conversation with the Facebook Messenger chatbot .</p>
<p>Only few message types are allowed: <code>message</code>, <code>generic-template</code>, <code>list-template</code> and <code>inline-buttons</code>.</p>
<p>A message must be <em>stored</em> before it can be broadcasted</p>
<p><img src="https://s3.us-west-2.amazonaws.com/secure.notion-static.com/32b7b355-281a-409c-b985-c0683a3dfec4/example-broadcast-1.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20230113%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20230113T150444Z&X-Amz-Expires=3600&X-Amz-Signature=944d1bde4fd806af0b93fcbdc3f148835871dd15f7d50288e4a344ee97bd611a&X-Amz-SignedHeaders=host&x-id=GetObject" alt="Broadcast Text Message"></p>
<p>In this example a simple to broadcast a text message: the <code>Broadcast node</code> store the message and returns a <code>broadcast</code> type message that will actually sent by the <a href="https://www.notion.so/cfee3d0ae8164faaa542374e0b449769">Telegram Sender Node</a> . A <a href="https://www.notion.so/1d90a6e8dfc448d6865c88358c459de2">Conversation node</a> is required in order at the beginning of the flow (in that case the <em>chatId</em> is left blank).</p>
<p>In the text messages tokens can be used</p>
<pre><code class="language-plain">Hello {{first_name}} {{last_name}}!
</code></pre>
<p>that will be replaced by the recipient’s first name and last name.</p>
<p>If the <code>Facebook sender</code> has the the pass-thru option enabled, the resulting message will the <em>broadcastId</em> which can be used to fetch the status and the metrics of the broadcasted message.</p>
<pre><code class="language-javascript">{
// ...
payload: {
broadcastId: '123456'
}
};
</code></pre>
<p>A <code>broadcast</code> message (which is basically a message stored on Facebook Messenger server ready to be sent) can also be scheduled</p>
<p><img src="https://s3.us-west-2.amazonaws.com/secure.notion-static.com/a87ce32b-0caf-4546-8b40-a015d4b07ff7/example-broadcast-2.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20230113%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20230113T150444Z&X-Amz-Expires=3600&X-Amz-Signature=1a73506f840c313bec41f80ae2610ebd7d437baeefed1b5a162a0ebdd9a355d4&X-Amz-SignedHeaders=host&x-id=GetObject" alt=""></p>
<p>Given the <em>broadcastId</em> it’s also possible to cancel or get the status of a scheduled message.</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>command</td>
<td>string</td>
<td>The method of the Broadcast API to execute. Could be: <em>store</em>, <em>schedule</em>, <em>metrics</em>, <em>list</em>, <em>cancel</em>, <em>status</em></td>
</tr>
<tr>
<td>broadcastId</td>
<td>string</td>
<td>The id of a broadcasted (or scheduled message)</td>
</tr>
<tr>
<td>messageId</td>
<td>string</td>
<td>The id of a stored message, ready to be broadcasted</td>
</tr>
<tr>
<td>messagingType</td>
<td>string</td>
<td>Type of notification for the message. Could be: <em>REGULAR</em>, <em>SILENT_PUSH</em>, <em>NO_PUSH</em></td>
</tr>
<tr>
<td>sendAt</td>
<td>string</td>
<td>ISO-8601 date format of the schedule date and time (<em>‘1970-01-02T00:00:00+0000’</em>)</td>
</tr>
</tbody></table>
</script>