botnaut
Version:
Facebook Messenger Chatbot Framework
1,006 lines (446 loc) • 29.8 kB
HTML
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Building a Conversation · 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="buildingConversation.html" />
<link rel="prev" href="../" />
</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 active" 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 " 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=".." >Building a Conversation</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="building-a-conversation"><a name="building-a-conversation" class="plugin-anchor" href="#building-a-conversation"><i class="fa fa-link" aria-hidden="true"></i></a>Building a Conversation</h1>
<h2 id="echo-reacting-to-text-messages"><a name="echo-reacting-to-text-messages" class="plugin-anchor" href="#echo-reacting-to-text-messages"><i class="fa fa-link" aria-hidden="true"></i></a>Echo: Reacting to text messages</h2>
<p>Let's start with simple example. This is how to make an echo.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { Router } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'botnaut'</span>);
<span class="hljs-keyword">const</span> bot = <span class="hljs-keyword">new</span> Router();
<span class="hljs-comment">// this route processes all incomming messages</span>
bot.use((req, res) => {
res.text(<span class="hljs-string">'I\'ve got this message:'</span>)
.text(req.text());
});
<span class="hljs-built_in">module</span>.exports = bot;
</code></pre>
<p>This works like this:</p>
<pre><code>*-----------------------------------*
| Hello! |
| I've got this message: |
| Hello! |
| |
| It smells like updog |
| I've got this message: |
| It smells like updog |
*-----------------------------------*
</code></pre><h2 id="asking-a-question"><a name="asking-a-question" class="plugin-anchor" href="#asking-a-question"><i class="fa fa-link" aria-hidden="true"></i></a>Asking a question</h2>
<p>When asking user a question, it's important to keeping reply in context. The <code>res.expected(<path>)</code> method ensures, that response will be dispatched by <code>/whichCat</code> route.</p>
<p>Routes shoud always begin with <code>/</code> slash, but when referencing route, starting slash is treated as absolute path reference. When you omit the starting slash, path is treated as relative.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { Router } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'botnaut'</span>);
<span class="hljs-keyword">const</span> bot = <span class="hljs-keyword">new</span> Router();
bot.use(<span class="hljs-string">'/whichCat'</span>, (req, res, postBack) => {
<span class="hljs-keyword">const</span> lowercaseText = req.text(<span class="hljs-literal">true</span>); <span class="hljs-comment">// converts "Nýan. Cát" to "nyan-cat"</span>
<span class="hljs-keyword">if</span> (lowercaseText.match(<span class="hljs-regexp">/nyan-cat/</span>)) {
res.text(<span class="hljs-string">'Yeah, nyan cat!'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (lowercaseText.match(<span class="hljs-regexp">/grumpy-cat/</span>)) {
res.text(<span class="hljs-string">'Ugh, grumpy cat!'</span>);
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">return</span> Router.CONTINUE;
}
});
bot.use((req, res) => {
res.text(<span class="hljs-string">'So, Nyan cat or grumpy cat?'</span>)
.expected(<span class="hljs-string">'whichCat'</span>); <span class="hljs-comment">// set the route, which will process next text request</span>
});
<span class="hljs-built_in">module</span>.exports = bot;
</code></pre>
<p>This works like this:</p>
<pre><code>*-----------------------------------*
| Nyan cat |
| So, Nyan cat or grumpy cat? |
| |
| Nyan cat |
| Yeah, nyan cat! |
| |
| What? |
| So, Nyan cat or grumpy cat? |
*-----------------------------------*
</code></pre><h2 id="using-quick-replies-for-controlling-conversation"><a name="using-quick-replies-for-controlling-conversation" class="plugin-anchor" href="#using-quick-replies-for-controlling-conversation"><i class="fa fa-link" aria-hidden="true"></i></a>Using Quick Replies for controlling conversation</h2>
<p>Click is always simplier then writing a text. And also working with events is much easier then text processing.
Let's lead conversation throuch quick replies.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { Router } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'botnaut'</span>);
<span class="hljs-keyword">const</span> bot = <span class="hljs-keyword">new</span> Router();
bot.use(<span class="hljs-string">'/grumpy'</span>, (req, res) => {
res.text(<span class="hljs-string">'Ugh, grumpy cat!'</span>);
});
bot.use(<span class="hljs-string">'/nyan'</span>, (req, res) => {
res.text(<span class="hljs-string">'Yeah, nyan cat!'</span>);
});
bot.use((req, res) => {
<span class="hljs-comment">// let's give two options to user</span>
res.text(<span class="hljs-string">'So, Nyan cat or grumpy cat?'</span>, {
grumpy: <span class="hljs-string">'Grumpy cat'</span>,
nyan: <span class="hljs-string">'Nyan cat'</span>
});
});
<span class="hljs-built_in">module</span>.exports = bot;
</code></pre>
<p>This code works <strong>exacly like previous example</strong>, because when quick replies are used, treir text representation
is used to match the users input. String <code>"Nyan cat"</code> is converted to regular expression <code>/nyan-cat/</code>;</p>
<h2 id="post-back-is-also-an-action"><a name="post-back-is-also-an-action" class="plugin-anchor" href="#post-back-is-also-an-action"><i class="fa fa-link" aria-hidden="true"></i></a>Post Back is also an action!</h2>
<p>We've normalized all Quick Replies, Post Backs, or Referrals to actions! You don't have to care about action type.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { Router } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'botnaut'</span>);
<span class="hljs-keyword">const</span> bot = <span class="hljs-keyword">new</span> Router();
bot.use(<span class="hljs-string">'/grumpy'</span>, (req, res) => {
res.text(<span class="hljs-string">'Ugh, grumpy cat!'</span>);
});
bot.use(<span class="hljs-string">'/nyan'</span>, (req, res) => {
res.text(<span class="hljs-string">'Yeah, nyan cat!'</span>);
});
bot.use((req, res) => {
<span class="hljs-comment">// allow access grumpy cat with button</span>
res.button(<span class="hljs-string">'Lets choose grumpy cat'</span>)
.postBackButton(<span class="hljs-string">'Call it'</span>, <span class="hljs-string">'grumpy'</span>)
.send();
<span class="hljs-comment">// and nyan cat with quick reply</span>
res.text(<span class="hljs-string">'So, Nyan cat or grumpy cat?'</span>, {
nyan: <span class="hljs-string">'Nyan cat'</span>
});
});
<span class="hljs-built_in">module</span>.exports = bot;
</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="../" class="navigation navigation-prev " aria-label="Previous page: Getting Started">
<i class="fa fa-angle-left"></i>
</a>
<a href="buildingConversation.html#asking-a-question" class="navigation navigation-next " aria-label="Next page: Asking a question">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"Building a Conversation","level":"1.2","depth":1,"next":{"title":"Asking a question","level":"1.2.1","depth":2,"anchor":"#asking-a-question","path":"tutorial/buildingConversation.md","ref":"tutorial/buildingConversation.md#asking-a-question","articles":[]},"previous":{"title":"Getting Started","level":"1.1","depth":1,"path":"README.md","ref":"README.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":"tutorial/buildingConversation.md","mtime":"2017-08-07T06:58:07.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>