UNPKG

botnaut

Version:

Facebook Messenger Chatbot Framework

979 lines (425 loc) 28.1 kB
<!DOCTYPE HTML> <html lang="" > <head> <meta charset="UTF-8"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>Conversation State · 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="conversationState.html" /> <link rel="prev" href="buildingConversation.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="buildingConversation.html"> <a href="buildingConversation.html"> Building a Conversation </a> <ul class="articles"> <li class="chapter " data-level="1.2.1" data-path="buildingConversation.html"> <a href="buildingConversation.html#asking-a-question"> Asking a question </a> </li> <li class="chapter " data-level="1.2.2" data-path="buildingConversation.html"> <a href="buildingConversation.html#using-quick-replies-for-controlling-conversation"> Using quick replies </a> </li> <li class="chapter " data-level="1.2.3" data-path="buildingConversation.html"> <a href="buildingConversation.html#post-back-is-also-an-action"> Postbacks </a> </li> </ul> </li> <li class="chapter active" data-level="1.3" data-path="conversationState.html"> <a href="conversationState.html"> Conversation State </a> <ul class="articles"> <li class="chapter " data-level="1.3.1" data-path="conversationState.html"> <a href="conversationState.html#accessing-user-profile"> User profile </a> </li> <li class="chapter " data-level="1.3.2" data-path="conversationState.html"> <a href="conversationState.html#using-the-state-in-navigation"> Using state in navigation </a> </li> <li class="chapter " data-level="1.3.3" data-path="conversationState.html"> <a href="conversationState.html#reading-and-modifiing-user-state"> Reading and writing state </a> </li> </ul> </li> <li class="chapter " data-level="1.4" data-path="asyncActions.html"> <a href="asyncActions.html"> Asynchronous Actions </a> <ul class="articles"> <li class="chapter " data-level="1.4.1" data-path="asyncActions.html"> <a href="asyncActions.html#blocking-asynchronous-actions"> Blocking Actions </a> </li> <li class="chapter " data-level="1.4.2" data-path="asyncActions.html"> <a href="asyncActions.html#non-blocking-asynchronous-actions"> Non Blocking Actions </a> </li> <li class="chapter " data-level="1.4.3" data-path="asyncActions.html"> <a href="asyncActions.html#loading-attachments-to-buffer"> Loading Attachments </a> </li> </ul> </li> <li class="chapter " data-level="1.5" data-path="advancedRouting.html"> <a href="advancedRouting.html"> Advanced Routing </a> <ul class="articles"> <li class="chapter " data-level="1.5.1" data-path="advancedRouting.html"> <a href="advancedRouting.html#resolving-actions"> Resolving Actions </a> </li> <li class="chapter " data-level="1.5.2" data-path="advancedRouting.html"> <a href="advancedRouting.html#simple-text-actions"> Text Actions </a> </li> <li class="chapter " data-level="1.5.3" data-path="advancedRouting.html"> <a href="advancedRouting.html#integrating-with-ai-prediction"> Integrating AI </a> </li> <li class="chapter " data-level="1.5.4" data-path="advancedRouting.html"> <a href="advancedRouting.html#redirecting-flow-with-post-back"> Redirecting with Postback </a> </li> </ul> </li> <li class="chapter " data-level="1.6" data-path="nestingRouters.html"> <a href="nestingRouters.html"> Nesting - Reusable Blocks </a> <ul class="articles"> <li class="chapter " data-level="1.6.1" data-path="nestingRouters.html"> <a href="nestingRouters.html#basic-concept"> Concept </a> </li> <li class="chapter " data-level="1.6.2" data-path="nestingRouters.html"> <a href="nestingRouters.html#example"> Example </a> </li> </ul> </li> <li class="chapter " data-level="1.7" data-path="templates.html"> <a href="templates.html"> Sending Templates </a> <ul class="articles"> <li class="chapter " data-level="1.7.1" data-path="templates.html"> <a href="templates.html#button-template"> Button Template </a> </li> <li class="chapter " data-level="1.7.2" data-path="templates.html"> <a href="templates.html#generic-template"> Generic Template </a> </li> <li class="chapter " data-level="1.7.3" data-path="templates.html"> <a href="templates.html#receipt-template"> Receipt Template </a> </li> <li class="chapter " data-level="1.7.4" data-path="templates.html"> <a href="templates.html#working-with-webviews"> Webviews </a> </li> </ul> </li> <li class="chapter " data-level="1.8" data-path="testing.html"> <a href="testing.html"> Testing </a> </li> <li class="chapter " data-level="1.9" data-path="analytics.html"> <a href="analytics.html"> Analytics </a> </li> <li class="chapter " data-level="1.10" data-path="usingWithWingbot.html"> <a href="usingWithWingbot.html"> Using with Wingbot.ai </a> <ul class="articles"> <li class="chapter " data-level="1.10.1" data-path="usingWithWingbot.html"> <a href="usingWithWingbot.html#bot-configuration"> Bot configuration </a> </li> <li class="chapter " data-level="1.10.2" data-path="usingWithWingbot.html"> <a href="usingWithWingbot.html#express-mongodb"> Express Mongodb </a> </li> <li class="chapter " data-level="1.10.3" data-path="usingWithWingbot.html"> <a href="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="../api/Request.html"> <a href="../api/Request.html"> Request </a> </li> <li class="chapter " data-level="2.2" data-path="../api/Responder.html"> <a href="../api/Responder.html"> Responder </a> </li> <li class="chapter " data-level="2.3" data-path="../api/Router.html"> <a href="../api/Router.html"> Router </a> </li> <li class="chapter " data-level="2.4" data-path="../api/Tester.html"> <a href="../api/Tester.html"> Tester </a> </li> <li class="chapter " data-level="2.5" data-path="../api/ButtonTemplate.html"> <a href="../api/ButtonTemplate.html"> Template Builders </a> <ul class="articles"> <li class="chapter " data-level="2.5.1" data-path="../api/ButtonTemplate.html"> <a href="../api/ButtonTemplate.html#ButtonTemplate"> ButtonTemplate </a> </li> <li class="chapter " data-level="2.5.2" data-path="../api/ButtonTemplate.html"> <a href="../api/ButtonTemplate.html#GenericTemplate"> GenericTemplate </a> </li> <li class="chapter " data-level="2.5.3" data-path="../api/ButtonTemplate.html"> <a href="../api/ButtonTemplate.html#ReceiptTemplate"> ReceiptTemplate </a> </li> </ul> </li> <li class="chapter " data-level="2.6" data-path="../api/Settings.html"> <a href="../api/Settings.html"> Bot Settings </a> </li> <li class="chapter " data-level="2.7" data-path="../api/express.html"> <a href="../api/express.html"> Express/MongoDb tools </a> </li> <li class="chapter " data-level="2.8" data-path="../api/bufferloader.html"> <a href="../api/bufferloader.html"> Utils </a> </li> <li class="chapter " data-level="2.9" data-path="../api/Ai.html"> <a href="../api/Ai.html"> Ai </a> </li> <li class="chapter " data-level="2.10" data-path="../api/serverlessAWS.html"> <a href="../api/serverlessAWS.html"> Serverless </a> </li> <li class="chapter " data-level="2.11" data-path="../api/BuildRouter.html"> <a href="../api/BuildRouter.html"> Wingbot.ai </a> </li> <li class="chapter " data-level="2.12" data-path="../api/callback.html"> <a href="../api/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=".." >Conversation State</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"> <h1 id="working-with-conversation-state"><a name="working-with-conversation-state" class="plugin-anchor" href="#working-with-conversation-state"><i class="fa fa-link" aria-hidden="true"></i></a>Working with conversation state</h1> <p>Stateless conversation is annoying. Noone wants to repeat, what was said. So <strong>each user has own state object</strong>.</p> <h2 id="accessing-user-profile"><a name="accessing-user-profile" class="plugin-anchor" href="#accessing-user-profile"><i class="fa fa-link" aria-hidden="true"></i></a>Accessing user profile</h2> <p>Yes, you already have user profile in state.</p> <pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { Router } = <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((req, res) =&gt; { <span class="hljs-keyword">const</span> { gender, locale, profilePic, lastName, firstName } = req.state.user; res.text(<span class="hljs-string">`Hello <span class="hljs-subst">${firstName || &apos;you!&apos;}</span>`</span>); }); <span class="hljs-built_in">module</span>.exports = bot; </code></pre> <h2 id="using-the-state-in-navigation"><a name="using-the-state-in-navigation" class="plugin-anchor" href="#using-the-state-in-navigation"><i class="fa fa-link" aria-hidden="true"></i></a>Using the state in navigation</h2> <p>Request object contains <code>.state</code> attribute, which represents state before the event, Responder updates new state with <code>Object.assign()</code> approach.</p> <pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { Router } = <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(<span class="hljs-string">&apos;/rename&apos;</span>, (req, res) =&gt; { res.text(<span class="hljs-string">&apos;So, how will you call me?&apos;</span>) .expected(<span class="hljs-string">&apos;setName&apos;</span>); }); bot.use(<span class="hljs-string">&apos;/setName&apos;</span>, (req, res) =&gt; { <span class="hljs-keyword">const</span> name = req.text(); res.text(<span class="hljs-string">`Now, I&apos;am <span class="hljs-subst">${name}</span>.`</span>) .setState({ name }); }); bot.use((req, res) =&gt; { <span class="hljs-keyword">const</span> { name } = req.state; <span class="hljs-keyword">if</span> (name) { res.text(<span class="hljs-string">`Call me <span class="hljs-subst">${name}</span>, ok?`</span>, { rename: <span class="hljs-string">&apos;It\&apos;s not ok&apos;</span> }); } <span class="hljs-keyword">else</span> { res.text(<span class="hljs-string">&apos;I have no name, give me one!&apos;</span>) .expected(<span class="hljs-string">&apos;setName&apos;</span>); } }); <span class="hljs-built_in">module</span>.exports = bot; </code></pre> <p>This example makes conversation flow like this:</p> <pre><code>*-----------------------------------* | Hello | | I have no name, give me one! | | | | Albert | | Now, I&apos;am Albert. | | | | Hello | | Call me Albert, ok? | | | | It&apos;s not ok | | So, how will you call me? | | | | Alfred | | Now, I&apos;am Alfred. | *-----------------------------------* </code></pre><h2 id="reading-and-modifiing-user-state"><a name="reading-and-modifiing-user-state" class="plugin-anchor" href="#reading-and-modifiing-user-state"><i class="fa fa-link" aria-hidden="true"></i></a>Reading and modifiing user state</h2> <p>It&apos;s good to know, that request state is not modified, when <code>res.setState()</code> is called. It will be modified after dispatching process.</p> <pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { Router } = <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((req, res) =&gt; { <span class="hljs-comment">// let&apos;s give two options to user</span> <span class="hljs-keyword">if</span> (req.state.odd) { res.setState({ odd: <span class="hljs-literal">false</span> }); <span class="hljs-comment">// still req.state.odd === true</span> <span class="hljs-keyword">this</span>.text(<span class="hljs-string">&apos;Odd&apos;</span>); } <span class="hljs-keyword">else</span> { res.setState({ odd: <span class="hljs-literal">true</span> }); <span class="hljs-comment">// still req.state.odd === false || req.state.odd === undefined</span> <span class="hljs-keyword">this</span>.text(<span class="hljs-string">&apos;Even&apos;</span>); } }); <span class="hljs-built_in">module</span>.exports = bot; </code></pre> <blockquote> <p>It&apos;s better to avoid using &quot;default state&quot;, because when you need to change default state in future updates, you should also migrate current users states to new structure.</p> </blockquote> </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="buildingConversation.html#post-back-is-also-an-action" class="navigation navigation-prev " aria-label="Previous page: Postbacks"> <i class="fa fa-angle-left"></i> </a> <a href="conversationState.html#accessing-user-profile" class="navigation navigation-next " aria-label="Next page: User profile"> <i class="fa fa-angle-right"></i> </a> </div> <script> var gitbook = gitbook || []; gitbook.push(function() { gitbook.page.hasChanged({"page":{"title":"Conversation State","level":"1.3","depth":1,"next":{"title":"User profile","level":"1.3.1","depth":2,"anchor":"#accessing-user-profile","path":"tutorial/conversationState.md","ref":"tutorial/conversationState.md#accessing-user-profile","articles":[]},"previous":{"title":"Postbacks","level":"1.2.3","depth":2,"anchor":"#post-back-is-also-an-action","path":"tutorial/buildingConversation.md","ref":"tutorial/buildingConversation.md#post-back-is-also-an-action","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":"tutorial/conversationState.md","mtime":"2017-08-29T10:36:23.000Z","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>