UNPKG

botnaut

Version:

Facebook Messenger Chatbot Framework

1,108 lines (551 loc) 32.3 kB
<!DOCTYPE HTML> <html lang="" > <head> <meta charset="UTF-8"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>Middlewares · Botnaut</title> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="description" content=""> <meta name="generator" content="GitBook 3.2.3"> <meta name="author" content="David Menger"> <link rel="stylesheet" href="../gitbook/style.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css"> <meta name="HandheldFriendly" content="true"/> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png"> <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon"> <link rel="prev" href="BuildRouter.html" /> </head> <body> <div class="book"> <div class="book-summary"> <div id="book-search-input" role="search"> <input type="text" placeholder="Type to search" /> </div> <nav role="navigation"> <ul class="summary"> <li class="header">Building a chat bot</li> <li class="chapter " data-level="1.1" data-path="../"> <a href="../"> Getting Started </a> </li> <li class="chapter " data-level="1.2" data-path="../tutorial/buildingConversation.html"> <a href="../tutorial/buildingConversation.html"> Building a Conversation </a> <ul class="articles"> <li class="chapter " data-level="1.2.1" data-path="../tutorial/buildingConversation.html"> <a href="../tutorial/buildingConversation.html#asking-a-question"> Asking a question </a> </li> <li class="chapter " data-level="1.2.2" data-path="../tutorial/buildingConversation.html"> <a href="../tutorial/buildingConversation.html#using-quick-replies-for-controlling-conversation"> Using quick replies </a> </li> <li class="chapter " data-level="1.2.3" data-path="../tutorial/buildingConversation.html"> <a href="../tutorial/buildingConversation.html#post-back-is-also-an-action"> Postbacks </a> </li> </ul> </li> <li class="chapter " data-level="1.3" data-path="../tutorial/conversationState.html"> <a href="../tutorial/conversationState.html"> Conversation State </a> <ul class="articles"> <li class="chapter " data-level="1.3.1" data-path="../tutorial/conversationState.html"> <a href="../tutorial/conversationState.html#accessing-user-profile"> User profile </a> </li> <li class="chapter " data-level="1.3.2" data-path="../tutorial/conversationState.html"> <a href="../tutorial/conversationState.html#using-the-state-in-navigation"> Using state in navigation </a> </li> <li class="chapter " data-level="1.3.3" data-path="../tutorial/conversationState.html"> <a href="../tutorial/conversationState.html#reading-and-modifiing-user-state"> Reading and writing state </a> </li> </ul> </li> <li class="chapter " data-level="1.4" data-path="../tutorial/asyncActions.html"> <a href="../tutorial/asyncActions.html"> Asynchronous Actions </a> <ul class="articles"> <li class="chapter " data-level="1.4.1" data-path="../tutorial/asyncActions.html"> <a href="../tutorial/asyncActions.html#blocking-asynchronous-actions"> Blocking Actions </a> </li> <li class="chapter " data-level="1.4.2" data-path="../tutorial/asyncActions.html"> <a href="../tutorial/asyncActions.html#non-blocking-asynchronous-actions"> Non Blocking Actions </a> </li> <li class="chapter " data-level="1.4.3" data-path="../tutorial/asyncActions.html"> <a href="../tutorial/asyncActions.html#loading-attachments-to-buffer"> Loading Attachments </a> </li> </ul> </li> <li class="chapter " data-level="1.5" data-path="../tutorial/advancedRouting.html"> <a href="../tutorial/advancedRouting.html"> Advanced Routing </a> <ul class="articles"> <li class="chapter " data-level="1.5.1" data-path="../tutorial/advancedRouting.html"> <a href="../tutorial/advancedRouting.html#resolving-actions"> Resolving Actions </a> </li> <li class="chapter " data-level="1.5.2" data-path="../tutorial/advancedRouting.html"> <a href="../tutorial/advancedRouting.html#simple-text-actions"> Text Actions </a> </li> <li class="chapter " data-level="1.5.3" data-path="../tutorial/advancedRouting.html"> <a href="../tutorial/advancedRouting.html#integrating-with-ai-prediction"> Integrating AI </a> </li> <li class="chapter " data-level="1.5.4" data-path="../tutorial/advancedRouting.html"> <a href="../tutorial/advancedRouting.html#redirecting-flow-with-post-back"> Redirecting with Postback </a> </li> </ul> </li> <li class="chapter " data-level="1.6" data-path="../tutorial/nestingRouters.html"> <a href="../tutorial/nestingRouters.html"> Nesting - Reusable Blocks </a> <ul class="articles"> <li class="chapter " data-level="1.6.1" data-path="../tutorial/nestingRouters.html"> <a href="../tutorial/nestingRouters.html#basic-concept"> Concept </a> </li> <li class="chapter " data-level="1.6.2" data-path="../tutorial/nestingRouters.html"> <a href="../tutorial/nestingRouters.html#example"> Example </a> </li> </ul> </li> <li class="chapter " data-level="1.7" data-path="../tutorial/templates.html"> <a href="../tutorial/templates.html"> Sending Templates </a> <ul class="articles"> <li class="chapter " data-level="1.7.1" data-path="../tutorial/templates.html"> <a href="../tutorial/templates.html#button-template"> Button Template </a> </li> <li class="chapter " data-level="1.7.2" data-path="../tutorial/templates.html"> <a href="../tutorial/templates.html#generic-template"> Generic Template </a> </li> <li class="chapter " data-level="1.7.3" data-path="../tutorial/templates.html"> <a href="../tutorial/templates.html#receipt-template"> Receipt Template </a> </li> <li class="chapter " data-level="1.7.4" data-path="../tutorial/templates.html"> <a href="../tutorial/templates.html#working-with-webviews"> Webviews </a> </li> </ul> </li> <li class="chapter " data-level="1.8" data-path="../tutorial/testing.html"> <a href="../tutorial/testing.html"> Testing </a> </li> <li class="chapter " data-level="1.9" data-path="../tutorial/analytics.html"> <a href="../tutorial/analytics.html"> Analytics </a> </li> <li class="chapter " data-level="1.10" data-path="../tutorial/usingWithWingbot.html"> <a href="../tutorial/usingWithWingbot.html"> Using with Wingbot.ai </a> <ul class="articles"> <li class="chapter " data-level="1.10.1" data-path="../tutorial/usingWithWingbot.html"> <a href="../tutorial/usingWithWingbot.html#bot-configuration"> Bot configuration </a> </li> <li class="chapter " data-level="1.10.2" data-path="../tutorial/usingWithWingbot.html"> <a href="../tutorial/usingWithWingbot.html#express-mongodb"> Express Mongodb </a> </li> <li class="chapter " data-level="1.10.3" data-path="../tutorial/usingWithWingbot.html"> <a href="../tutorial/usingWithWingbot.html#serverless-aws-lambda"> Serverless AWS Lambda </a> </li> </ul> </li> <li class="header">API</li> <li class="chapter " data-level="2.1" data-path="Request.html"> <a href="Request.html"> Request </a> </li> <li class="chapter " data-level="2.2" data-path="Responder.html"> <a href="Responder.html"> Responder </a> </li> <li class="chapter " data-level="2.3" data-path="Router.html"> <a href="Router.html"> Router </a> </li> <li class="chapter " data-level="2.4" data-path="Tester.html"> <a href="Tester.html"> Tester </a> </li> <li class="chapter " data-level="2.5" data-path="ButtonTemplate.html"> <a href="ButtonTemplate.html"> Template Builders </a> <ul class="articles"> <li class="chapter " data-level="2.5.1" data-path="ButtonTemplate.html"> <a href="ButtonTemplate.html#ButtonTemplate"> ButtonTemplate </a> </li> <li class="chapter " data-level="2.5.2" data-path="ButtonTemplate.html"> <a href="ButtonTemplate.html#GenericTemplate"> GenericTemplate </a> </li> <li class="chapter " data-level="2.5.3" data-path="ButtonTemplate.html"> <a href="ButtonTemplate.html#ReceiptTemplate"> ReceiptTemplate </a> </li> </ul> </li> <li class="chapter " data-level="2.6" data-path="Settings.html"> <a href="Settings.html"> Bot Settings </a> </li> <li class="chapter " data-level="2.7" data-path="express.html"> <a href="express.html"> Express/MongoDb tools </a> </li> <li class="chapter " data-level="2.8" data-path="bufferloader.html"> <a href="bufferloader.html"> Utils </a> </li> <li class="chapter " data-level="2.9" data-path="Ai.html"> <a href="Ai.html"> Ai </a> </li> <li class="chapter " data-level="2.10" data-path="serverlessAWS.html"> <a href="serverlessAWS.html"> Serverless </a> </li> <li class="chapter " data-level="2.11" data-path="BuildRouter.html"> <a href="BuildRouter.html"> Wingbot.ai </a> </li> <li class="chapter active" data-level="2.12" data-path="callback.html"> <a href="callback.html"> Middlewares </a> </li> <li class="divider"></li> <li> <a href="https://www.gitbook.com" target="blank" class="gitbook-link"> Published with GitBook </a> </li> </ul> </nav> </div> <div class="book-body"> <div class="body-inner"> <div class="book-header" role="navigation"> <!-- Title --> <h1> <i class="fa fa-circle-o-notch fa-spin"></i> <a href=".." >Middlewares</a> </h1> </div> <div class="page-wrapper" tabindex="-1" role="main"> <div class="page-inner"> <div id="book-search-results"> <div class="search-noresults"> <section class="normal markdown-section"> <div id="module_callbackMiddleware">&#xA0;</div> <h2 id="callbackmiddleware-&#x21D2;-function"><a name="callbackmiddleware-&#x21D2;-function" class="plugin-anchor" href="#callbackmiddleware-&#x21D2;-function"><i class="fa fa-link" aria-hidden="true"></i></a>callbackMiddleware &#x21D2; <code>function</code></h2> <p>Creates callback middleware, which allows to get user back to previous context</p> <p><strong>Example</strong> </p> <pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { Router, callbackMiddleware, ai } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;botnaut&apos;</span>); <span class="hljs-keyword">const</span> bot = <span class="hljs-keyword">new</span> Router(); bot.use(callbackMiddleware()); bot.use([<span class="hljs-string">&apos;fooRoute&apos;</span>, <span class="hljs-regexp">/^foo$/</span>], (req, res) =&gt; { <span class="hljs-keyword">if</span> (!req.isFromCallback()) { <span class="hljs-comment">// is hidden, when user is just comming back</span> res.text(<span class="hljs-string">&apos;This is your FOO response&apos;</span>); } <span class="hljs-keyword">if</span> (!req.hasCallback()) { res.setCallback(<span class="hljs-string">&apos;fooRoute&apos;</span>); } <span class="hljs-comment">// ability get back to previous content</span> res.addCallbackQuickReply(<span class="hljs-string">&apos;Go back&apos;</span>); res.text(<span class="hljs-string">&apos;So, what you want?&apos;</span>, { barRoute: <span class="hljs-string">&apos;Go to bar&apos;</span> }); }) bot.use([<span class="hljs-string">&apos;barRoute&apos;</span>, <span class="hljs-regexp">/^bar$/</span>], (req, res) =&gt; { <span class="hljs-keyword">if</span> (!req.isFromCallback()) { res.text(<span class="hljs-string">&apos;This is your BAR response&apos;</span>); } <span class="hljs-keyword">if</span> (!req.hasCallback()) { res.setCallback(<span class="hljs-string">&apos;barRoute&apos;</span>); } <span class="hljs-keyword">if</span> (!res.proceedCallback()) { res.text(<span class="hljs-string">&apos;So, what\&apos;s next?&apos;</span>, { fooRoute: <span class="hljs-string">&apos;Go to foo&apos;</span> }); } }); </code></pre> <ul> <li><a href="#module_callbackMiddleware">callbackMiddleware</a> &#x21D2; <code>function</code><ul> <li><a href="#exp_module_callbackMiddleware--setCallback">setCallback(action, [callbackContext], [callbackText])</a> &#x21D2; <code>this</code> &#x23CF;</li> <li><a href="#exp_module_callbackMiddleware--hasCallback">hasCallback([callbackContext])</a> &#x21D2; <code>boolean</code> &#x23CF;</li> <li><a href="#exp_module_callbackMiddleware--isFromCallback">isFromCallback([callbackContext])</a> &#x21D2; <code>boolean</code> &#x23CF;</li> <li><a href="#exp_module_callbackMiddleware--proceedCallback">proceedCallback([callbackContext])</a> &#x21D2; <code>boolean</code> &#x23CF;</li> <li><a href="#exp_module_callbackMiddleware--addCallbackQuickReply">addCallbackQuickReply(replyText)</a> &#x21D2; <code>this</code> &#x23CF;</li> </ul> </li> </ul> <div id="exp_module_callbackMiddleware--setCallback">&#xA0;</div> <h3 id="setcallbackaction-callbackcontext-callbacktext-&#x21D2;-this-&#x23CF;"><a name="setcallbackaction-callbackcontext-callbacktext-&#x21D2;-this-&#x23CF;" class="plugin-anchor" href="#setcallbackaction-callbackcontext-callbacktext-&#x21D2;-this-&#x23CF;"><i class="fa fa-link" aria-hidden="true"></i></a>setCallback(action, [callbackContext], [callbackText]) &#x21D2; <code>this</code> &#x23CF;</h3> <p>Sets action, where to go back, when user responds with text</p> <p><strong>Kind</strong>: Exported function </p> <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Default</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td>action</td> <td><code>string</code></td> <td></td> <td>relative or absolute action (usualy current action)</td> </tr> <tr> <td>[callbackContext]</td> <td><code>string</code> \</td> <td><code>null</code></td> <td><code>null</code></td> <td>context of callback</td> </tr> <tr> <td>[callbackText]</td> <td><code>string</code> \</td> <td><code>null</code></td> <td><code>null</code></td> <td>custom text response</td> </tr> </tbody> </table> <p><strong>Example</strong> </p> <pre><code class="lang-javascript">bot.use(<span class="hljs-string">&apos;myAction&apos;</span>, (req, res) =&gt; { res.setCallback(<span class="hljs-string">&apos;myAction&apos;</span>); <span class="hljs-comment">// return back</span> }); </code></pre> <div id="exp_module_callbackMiddleware--hasCallback">&#xA0;</div> <h3 id="hascallbackcallbackcontext-&#x21D2;-boolean-&#x23CF;"><a name="hascallbackcallbackcontext-&#x21D2;-boolean-&#x23CF;" class="plugin-anchor" href="#hascallbackcallbackcontext-&#x21D2;-boolean-&#x23CF;"><i class="fa fa-link" aria-hidden="true"></i></a>hasCallback([callbackContext]) &#x21D2; <code>boolean</code> &#x23CF;</h3> <p>Returns true, when callback has been prevously set. It&apos;s usefull, when you don&apos;t want to bouce back the methods.</p> <p><strong>Kind</strong>: Exported function </p> <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Default</th> </tr> </thead> <tbody> <tr> <td>[callbackContext]</td> <td><code>string</code></td> <td><code>null</code></td> </tr> </tbody> </table> <p><strong>Example</strong> </p> <pre><code class="lang-javascript">bot.use([<span class="hljs-string">&apos;fooRoute&apos;</span>, <span class="hljs-regexp">/^foo$/</span>], (req, res) =&gt; { <span class="hljs-comment">// set callback, only when this request does not have one</span> <span class="hljs-keyword">if</span> (!req.hasCallback()) { res.setCallback(<span class="hljs-string">&apos;fooRoute&apos;</span>); } }); </code></pre> <div id="exp_module_callbackMiddleware--isFromCallback">&#xA0;</div> <h3 id="isfromcallbackcallbackcontext-&#x21D2;-boolean-&#x23CF;"><a name="isfromcallbackcallbackcontext-&#x21D2;-boolean-&#x23CF;" class="plugin-anchor" href="#isfromcallbackcallbackcontext-&#x21D2;-boolean-&#x23CF;"><i class="fa fa-link" aria-hidden="true"></i></a>isFromCallback([callbackContext]) &#x21D2; <code>boolean</code> &#x23CF;</h3> <p>Returns true, when user is comming back from callback Comeback is initialised with <code>res.proceedCallback()</code> or quick reply Usefull for hidding the text, user has already seen</p> <p><strong>Kind</strong>: Exported function </p> <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Default</th> </tr> </thead> <tbody> <tr> <td>[callbackContext]</td> <td><code>string</code></td> <td><code>null</code></td> </tr> </tbody> </table> <p><strong>Example</strong> </p> <pre><code class="lang-javascript">bot.use([<span class="hljs-string">&apos;fooRoute&apos;</span>, <span class="hljs-regexp">/^foo$/</span>], (req, res) =&gt; { <span class="hljs-comment">// set callback, only when this request does not have one</span> <span class="hljs-keyword">if</span> (!req.isFromCallback()) { res.text(<span class="hljs-string">&apos;this is the response, you dont want to read again&apos;</span>); } }); </code></pre> <div id="exp_module_callbackMiddleware--proceedCallback">&#xA0;</div> <h3 id="proceedcallbackcallbackcontext-&#x21D2;-boolean-&#x23CF;"><a name="proceedcallbackcallbackcontext-&#x21D2;-boolean-&#x23CF;" class="plugin-anchor" href="#proceedcallbackcallbackcontext-&#x21D2;-boolean-&#x23CF;"><i class="fa fa-link" aria-hidden="true"></i></a>proceedCallback([callbackContext]) &#x21D2; <code>boolean</code> &#x23CF;</h3> <p>Proceed a callback, when exists (go to action, where the callback has been previously set) Returns true, when postback will occur</p> <p><strong>Kind</strong>: Exported function </p> <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td>[callbackContext]</td> <td><code>string</code></td> <td>the context</td> </tr> </tbody> </table> <p><strong>Example</strong> </p> <pre><code class="lang-javascript">bot.use([<span class="hljs-string">&apos;fooRoute&apos;</span>, <span class="hljs-regexp">/^foo$/</span>], (req, res) =&gt; { <span class="hljs-comment">// set callback, only when this request does not have one</span> <span class="hljs-keyword">if</span> (!res.proceedCallback()) { res.text(<span class="hljs-string">&apos;this is the followup question&apos;</span>, { followupAction: <span class="hljs-string">&apos;Continue&apos;</span> }); } }); </code></pre> <div id="exp_module_callbackMiddleware--addCallbackQuickReply">&#xA0;</div> <h3 id="addcallbackquickreplyreplytext-&#x21D2;-this-&#x23CF;"><a name="addcallbackquickreplyreplytext-&#x21D2;-this-&#x23CF;" class="plugin-anchor" href="#addcallbackquickreplyreplytext-&#x21D2;-this-&#x23CF;"><i class="fa fa-link" aria-hidden="true"></i></a>addCallbackQuickReply(replyText) &#x21D2; <code>this</code> &#x23CF;</h3> <p>Adds &quot;back&quot; quick reply to other replies (alternative to <code>proceedCallback()</code>)</p> <p><strong>Kind</strong>: Exported function </p> <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td>replyText</td> <td><code>string</code></td> <td>the default text</td> </tr> </tbody> </table> <p><strong>Example</strong> </p> <pre><code class="lang-javascript">bot.use([<span class="hljs-string">&apos;fooRoute&apos;</span>, <span class="hljs-regexp">/^foo$/</span>], (req, res) =&gt; { <span class="hljs-comment">// ability get back to previous context</span> res.addCallbackQuickReply(<span class="hljs-string">&apos;Go back&apos;</span>); res.text(<span class="hljs-string">&apos;So, what you want?&apos;</span>, { barRoute: <span class="hljs-string">&apos;Go to bar&apos;</span> }); }); </code></pre> </section> </div> <div class="search-results"> <div class="has-results"> <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1> <ul class="search-results-list"></ul> </div> <div class="no-results"> <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1> </div> </div> </div> </div> </div> </div> <a href="BuildRouter.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: Wingbot.ai"> <i class="fa fa-angle-left"></i> </a> </div> <script> var gitbook = gitbook || []; gitbook.push(function() { gitbook.page.hasChanged({"page":{"title":"Middlewares","level":"2.12","depth":1,"previous":{"title":"Wingbot.ai","level":"2.11","depth":1,"path":"api/BuildRouter.md","ref":"api/BuildRouter.md","articles":[]},"dir":"ltr"},"config":{"plugins":["theme-default","collapsible-menu","anchors"],"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"},"pluginsConfig":{"collapsible-menu":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"theme-api":{"theme":"dark","languages":[{"lang":"js","name":"JavaScript","default":true}]},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"showLevel":false,"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}},"anchors":{}},"theme":"default","author":"David Menger","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{"twitter":"https://twitter.com/davidmenger"},"title":"Botnaut","gitbook":"*","description":"Facebook Messenger framework for Node.js"},"file":{"path":"api/callback.md","mtime":"2018-05-23T15:31:55.159Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-05-23T15:31:59.833Z"},"basePath":"..","book":{"language":""}}); }); </script> </div> <script src="../gitbook/gitbook.js"></script> <script src="../gitbook/theme.js"></script> <script src="../gitbook/gitbook-plugin-collapsible-menu/plugin.js"></script> <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script> <script src="../gitbook/gitbook-plugin-search/search.js"></script> <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script> <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script> <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script> <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script> </body> </html>