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
126 lines (121 loc) • 4.7 kB
HTML
<script type="text/javascript">
$.RedBot.registerType('chatbot-inline-query', {
category: $.RedBot.config.name,
color: '#FFCC66',
defaults: {
personal: {
value: false
},
caching: {
value: 300
},
inlineQueryAnswer: {
value: ''
}
},
inputs: 1,
outputs: 1,
icon: 'chatbot-inline-query.png',
paletteLabel: 'Inline Query',
label: function() {
return 'Inline Query';
},
oneditsave: function() {
this.inlineQueryAnswer = $('#node-input-inlineQueryAnswer').typedInput('value');
},
oneditprepare: function() {
$("#node-input-caching").spinner({min: 0, step: 100});
var widget = $('#node-input-inlineQueryAnswer');
widget.typedInput({
'default': 'json',
types: ['json']
});
widget.typedInput('value', this.inlineQueryAnswer);
}
});
</script>
<script type="text/x-red" data-template-name="chatbot-inline-query">
<div class="form-row">
<label for="node-input-caching">Cache</label>
<input type="text" id="node-input-caching" style="text-align:end; width:60px !important"> seconds
</div>
<div class="form-row">
<label for="node-input-personal">Personal</label>
<input type="checkbox" value="true" id="node-input-personal">
<div class="redbot-form-hint">
True if results may be cached on the server side only for the user that sent the query.
</div>
</div>
<div class="form-row">
<label for="node-input-inlineQueryAnswer">Response</label>
<input type="text" id="node-input-inlineQueryAnswer" placeholder="Inline Query Answer">
<div class="redbot-form-hint">
Consult Telegram API <a href="https://core.telegram.org/bots/api#inline-mode" target="_blank">inline query</a> on how to build query results.
</div>
</div>
</script>
<script type="text/x-red" data-help-name="chatbot-inline-query"><p>Inline mode allows the user to query a chatbot from a different chat, it’s a powerful tool to make your chatbot go viral, read more <a href="https://core.telegram.org/bots/inline">here</a>.</p>
<p>Every query invoked with a line like <code>@mychatbot how are you?</code> are routed through the <a href="https://www.notion.so/4132ce6c78dc4dbbab0fe9eb7e1c3c9b">Telegram Receiver node</a> as a message of type <code>inline-query</code>, it will look like</p>
<pre><code class="language-javascript">{
originalMessage: {
// ...
},
payload: {
content: 'how are you?',
type: 'inline_query',
// ...
}
}
</code></pre>
<p>and can be sent to any other parser nodes like other messages.</p>
<p>At some point of the flow it might be useful to route the incoming message on a different path based on the type, for example in order to handle the inline query differently, here is where the <code>Switch node</code> might be useful</p>
<p><img src="https://s3.us-west-2.amazonaws.com/secure.notion-static.com/4b30a036-7b2d-4e71-9650-1fce62605977/example-switch.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20230218%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20230218T124918Z&X-Amz-Expires=3600&X-Amz-Signature=7160ca10083484dbb3bab14c96d545278398496c02097f609e7056c3da01c8ca&X-Amz-SignedHeaders=host&x-id=GetObject" alt="Switch node example"></p>
<p>After a inline query is received, use a <code>Function node</code> to prepare the payload to send to a <code>Inline Query Results node</code> and then to a <code>Telegram Sender node</code>.</p>
<p>The <code>Function node</code></p>
<pre><code class="language-javascript">msg.payload = [
{
type: 'article',
id: 'AAA-123',
title: 'Title #1',
input_message_content: {
message_text: 'You selected title #1',
}
},
{
type: 'article',
id: 'AAA-124',
title: 'Title #2',
input_message_content: {
message_text: 'You selected title #2'
}
}
]
return msg;
</code></pre>
<p>Learn more <a href="https://core.telegram.org/bots/api#inline-mode">here</a> about what kind of message it’s possible to send as inline query answer.</p>
<p>Available parameters for <code>msg.payload</code></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody><tr>
<td>inlineQueryAnswer</td>
<td>array of obj</td>
<td>The query results to show to the client</td>
</tr>
<tr>
<td>caching</td>
<td>integer</td>
<td>How long results are cached, in seconds</td>
</tr>
<tr>
<td>personal</td>
<td>boolean</td>
<td>If the results should be presented privately to the user</td>
</tr>
</tbody></table>
</script>