UNPKG

botnaut

Version:

Facebook Messenger Chatbot Framework

1,064 lines (508 loc) 29.1 kB
<!DOCTYPE HTML> <html lang="" > <head> <meta charset="UTF-8"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>Router · 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="next" href="Tester.html" /> <link rel="prev" href="Responder.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 active" 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 " 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=".." >Router</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="Router">&#xA0;</div> <h2 id="router-&#x21D0;-reducerwrapper"><a name="router-&#x21D0;-reducerwrapper" class="plugin-anchor" href="#router-&#x21D0;-reducerwrapper"><i class="fa fa-link" aria-hidden="true"></i></a>Router &#x21D0; <code>ReducerWrapper</code></h2> <p><strong>Kind</strong>: global class<br><strong>Extends</strong>: <code>ReducerWrapper</code> </p> <ul> <li><a href="#Router">Router</a> &#x21D0; <code>ReducerWrapper</code><ul> <li><a href="#new_Router_new">new Router()</a></li> <li><em>instance</em><ul> <li><a href="#Router_use">.use([action], [matcher], ...reducers)</a> &#x21D2; <code>Object</code></li> </ul> </li> <li><em>static</em><ul> <li><a href="#Router_CONTINUE">.CONTINUE</a></li> <li><a href="#Router_BREAK">.BREAK</a></li> <li><a href="#Router_END">.END</a></li> <li><a href="#Router_exit">.exit(action, [data])</a> &#x21D2; <code>Array</code></li> </ul> </li> </ul> </li> </ul> <div id="new_Router_new">&#xA0;</div> <h3 id="new-router"><a name="new-router" class="plugin-anchor" href="#new-router"><i class="fa fa-link" aria-hidden="true"></i></a>new Router()</h3> <p>Cascading router</p> <div id="Router_use">&#xA0;</div> <h3 id="routeruseaction-matcher-reducers-&#x21D2;-object"><a name="routeruseaction-matcher-reducers-&#x21D2;-object" class="plugin-anchor" href="#routeruseaction-matcher-reducers-&#x21D2;-object"><i class="fa fa-link" aria-hidden="true"></i></a>router.use([action], [matcher], ...reducers) &#x21D2; <code>Object</code></h3> <p>Appends middleware, action handler or another router</p> <p><strong>Kind</strong>: instance method of <a href="#Router"><code>Router</code></a> </p> <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td>[action]</td> <td><code>string</code></td> <td>name of the action</td> </tr> <tr> <td>[matcher]</td> <td><code>RegExp</code> \</td> <td><code>string</code> \</td> <td><code>function</code></td> <td>The function can be async</td> </tr> <tr> <td>...reducers</td> <td><code>function</code> \</td> <td><a href="#Router"><code>Router</code></a></td> </tr> </tbody> </table> <p><strong>Example</strong> </p> <pre><code class="lang-javascript"><span class="hljs-comment">// middleware</span> router.use((req, res, postBack) =&gt; Router.CONTINUE); <span class="hljs-comment">// route with matching regexp</span> router.use(<span class="hljs-regexp">/help/</span>, (req, res) =&gt; { res.text(<span class="hljs-string">&apos;Hello!&apos;</span>); }); <span class="hljs-comment">// route with matching function (the function is considered as matcher</span> <span class="hljs-comment">// in case of the function accepts zero or one argument)</span> router.use(<span class="hljs-string">&apos;action&apos;</span>, req =&gt; req.text() === <span class="hljs-string">&apos;a&apos;</span>, (req, res) =&gt; { res.text(<span class="hljs-string">&apos;Hello!&apos;</span>); }); <span class="hljs-comment">// use multiple reducers</span> router.use(<span class="hljs-string">&apos;/path&apos;</span>, reducer1, reducer2) .onExit(<span class="hljs-string">&apos;exitAction&apos;</span>, (data, req, res, postBack) =&gt; { postBack(<span class="hljs-string">&apos;anotherAction&apos;</span>, { someData: <span class="hljs-literal">true</span> }) }); <span class="hljs-comment">// append router with exit action</span> router.use(<span class="hljs-string">&apos;/path&apos;</span>, subRouter) .onExit(<span class="hljs-string">&apos;exitAction&apos;</span>, (data, req, res, postBack) =&gt; { postBack(<span class="hljs-string">&apos;anotherAction&apos;</span>, { someData: <span class="hljs-literal">true</span> }) }); </code></pre> <div id="Router_CONTINUE">&#xA0;</div> <h3 id="routercontinue"><a name="routercontinue" class="plugin-anchor" href="#routercontinue"><i class="fa fa-link" aria-hidden="true"></i></a>Router.CONTINUE</h3> <p>Return <code>Router.CONTINUE</code> when action matches your route Its same as returning <code>true</code></p> <p><strong>Kind</strong>: static property of <a href="#Router"><code>Router</code></a><br><strong>Properties</strong></p> <table> <thead> <tr> <th>Type</th> </tr> </thead> <tbody> <tr> <td><code>boolean</code></td> </tr> </tbody> </table> <div id="Router_BREAK">&#xA0;</div> <h3 id="routerbreak"><a name="routerbreak" class="plugin-anchor" href="#routerbreak"><i class="fa fa-link" aria-hidden="true"></i></a>Router.BREAK</h3> <p>Return <code>Router.BREAK</code> when action does not match your route Its same as returning <code>false</code></p> <p><strong>Kind</strong>: static property of <a href="#Router"><code>Router</code></a><br><strong>Properties</strong></p> <table> <thead> <tr> <th>Type</th> </tr> </thead> <tbody> <tr> <td><code>boolean</code></td> </tr> </tbody> </table> <div id="Router_END">&#xA0;</div> <h3 id="routerend"><a name="routerend" class="plugin-anchor" href="#routerend"><i class="fa fa-link" aria-hidden="true"></i></a>Router.END</h3> <p>Returning <code>Router.END</code> constant stops dispatching request Its same as returning <code>undefined</code></p> <p><strong>Kind</strong>: static property of <a href="#Router"><code>Router</code></a><br><strong>Properties</strong></p> <table> <thead> <tr> <th>Type</th> </tr> </thead> <tbody> <tr> <td><code>null</code></td> </tr> </tbody> </table> <div id="Router_exit">&#xA0;</div> <h3 id="routerexitaction-data-&#x21D2;-array"><a name="routerexitaction-data-&#x21D2;-array" class="plugin-anchor" href="#routerexitaction-data-&#x21D2;-array"><i class="fa fa-link" aria-hidden="true"></i></a>Router.exit(action, [data]) &#x21D2; <code>Array</code></h3> <p>Create the exit point Its same as returning <code>[&apos;action&apos;, { data }]</code></p> <p><strong>Kind</strong>: static method of <a href="#Router"><code>Router</code></a> </p> <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td>action</td> <td><code>string</code></td> <td>the exit action</td> </tr> <tr> <td>[data]</td> <td><code>Object</code></td> <td>the data</td> </tr> </tbody> </table> <p><strong>Example</strong> </p> <pre><code class="lang-javascript">router.use((req, res) =&gt; { <span class="hljs-keyword">return</span> Router.exit(<span class="hljs-string">&apos;exitName&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="Responder.html" class="navigation navigation-prev " aria-label="Previous page: Responder"> <i class="fa fa-angle-left"></i> </a> <a href="Tester.html" class="navigation navigation-next " aria-label="Next page: Tester"> <i class="fa fa-angle-right"></i> </a> </div> <script> var gitbook = gitbook || []; gitbook.push(function() { gitbook.page.hasChanged({"page":{"title":"Router","level":"2.3","depth":1,"next":{"title":"Tester","level":"2.4","depth":1,"path":"api/Tester.md","ref":"api/Tester.md","articles":[]},"previous":{"title":"Responder","level":"2.2","depth":1,"path":"api/Responder.md","ref":"api/Responder.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/Router.md","mtime":"2018-05-23T15:31:55.055Z","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>