UNPKG

botnaut

Version:

Facebook Messenger Chatbot Framework

1,039 lines (477 loc) 33.1 kB
<!DOCTYPE HTML> <html lang="" > <head> <meta charset="UTF-8"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>Sending Templates · 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="templates.html" /> <link rel="prev" href="nestingRouters.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 " 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 active" 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=".." >Sending Templates</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="sending-templates"><a name="sending-templates" class="plugin-anchor" href="#sending-templates"><i class="fa fa-link" aria-hidden="true"></i></a>Sending Templates</h1> <ul> <li>When using extension, the url will contain a token for accessing the bot framework</li> <li>All links without a protocol will be treated as local links (<code>appUrl</code> from config will be used)</li> <li>All postback actions without leading <code>/</code> will be treated as relative actions</li> </ul> <h2 id="button-template"><a name="button-template" class="plugin-anchor" href="#button-template"><i class="fa fa-link" aria-hidden="true"></i></a>Button Template</h2> <p>Show the button template</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; { res.button(<span class="hljs-string">&apos;Hello&apos;</span>) .postBackButton(<span class="hljs-string">&apos;Text&apos;</span>, <span class="hljs-string">&apos;action&apos;</span>) <span class="hljs-comment">// makes a postback</span> .urlButton(<span class="hljs-string">&apos;Url button&apos;</span>, <span class="hljs-string">&apos;/internal&apos;</span>, <span class="hljs-literal">true</span>) <span class="hljs-comment">// opens authorized webview</span> .urlButton(<span class="hljs-string">&apos;Other button&apos;</span>, <span class="hljs-string">&apos;https://goo.gl&apos;</span>) <span class="hljs-comment">// opens in internal browser</span> .send(); }); <span class="hljs-built_in">module</span>.exports = bot; </code></pre> <h2 id="generic-template"><a name="generic-template" class="plugin-anchor" href="#generic-template"><i class="fa fa-link" aria-hidden="true"></i></a>Generic Template</h2> <p>Show the generic template</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; { res.genericTemplate() .addElement(<span class="hljs-string">&apos;title&apos;</span>, <span class="hljs-string">&apos;subtitle&apos;</span>) .setElementImage(<span class="hljs-string">&apos;/local.png&apos;</span>) <span class="hljs-comment">// set local image</span> .setElementUrl(<span class="hljs-string">&apos;https://www.seznam.cz&apos;</span>) .postBackButton(<span class="hljs-string">&apos;Button title&apos;</span>, <span class="hljs-string">&apos;action&apos;</span>, { actionData: <span class="hljs-number">1</span> }) .addElement(<span class="hljs-string">&apos;another&apos;</span>, <span class="hljs-string">&apos;subtitle&apos;</span>) .setElementImage(<span class="hljs-string">&apos;https://goo.gl/image.png&apos;</span>) <span class="hljs-comment">// use remote image</span> .setElementAction(<span class="hljs-string">&apos;action&apos;</span>, { actionData: <span class="hljs-number">1</span> }) .urlButton(<span class="hljs-string">&apos;Local link with extension&apos;</span>, <span class="hljs-string">&apos;/local/path&apos;</span>, <span class="hljs-literal">true</span>, <span class="hljs-string">&apos;compact&apos;</span>) .send(); }); <span class="hljs-built_in">module</span>.exports = bot; </code></pre> <h2 id="receipt-template"><a name="receipt-template" class="plugin-anchor" href="#receipt-template"><i class="fa fa-link" aria-hidden="true"></i></a>Receipt Template</h2> <p>Show the receipt template</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; { res.receipt(<span class="hljs-string">&apos;Name&apos;</span>, <span class="hljs-string">&apos;Cash&apos;</span>, <span class="hljs-string">&apos;CZK&apos;</span>, <span class="hljs-string">&apos;1&apos;</span>) .addElement(<span class="hljs-string">&apos;Element name&apos;</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-string">&apos;/inside.png&apos;</span>, <span class="hljs-string">&apos;text&apos;</span>) .send(); <span class="hljs-built_in">module</span>.exports = bot; </code></pre> <h2 id="working-with-webviews"><a name="working-with-webviews" class="plugin-anchor" href="#working-with-webviews"><i class="fa fa-link" aria-hidden="true"></i></a>Working with webviews</h2> <ul> <li><p><strong>Dont forget to whitelist your domain</strong></p> <pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { Settings } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;botnaut&apos;</span>); <span class="hljs-keyword">const</span> settings = <span class="hljs-keyword">new</span> Settings(<span class="hljs-string">&apos;pagetoken&apos;</span>); settings.whitelistDomain(<span class="hljs-string">&apos;https://mydomain.com&apos;</span>); </code></pre> </li> <li><p><strong>Include Facebook Code in your page</strong></p> <pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"> <span class="hljs-built_in">window</span>.FB_PAGE_ID = <span class="hljs-string">&apos;&apos;</span>; </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript"> (<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">d, s, id</span>)</span>{ <span class="hljs-keyword">var</span> js, fjs = d.getElementsByTagName(s)[<span class="hljs-number">0</span>]; <span class="hljs-keyword">if</span> (d.getElementById(id)) {<span class="hljs-keyword">return</span>;} js = d.createElement(s); js.id = id; js.src = <span class="hljs-string">&quot;//connect.facebook.com/en_US/messenger.Extensions.js&quot;</span>; fjs.parentNode.insertBefore(js, fjs); }(<span class="hljs-built_in">document</span>, <span class="hljs-string">&apos;script&apos;</span>, <span class="hljs-string">&apos;Messenger&apos;</span>)); </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span> </code></pre> </li> <li><p><strong>Closing the webview</strong></p> <pre><code class="lang-javascript">MessengerExtensions.requestCloseBrowser(() =&gt; { <span class="hljs-comment">// yes</span> }, () =&gt; { <span class="hljs-comment">// for classic browser</span> <span class="hljs-built_in">window</span>.location.href = <span class="hljs-string">`http://m.me/<span class="hljs-subst">${window.FB_PAGE_ID}</span>`</span>; }); </code></pre> </li> <li><p><strong>Getting token and sender from URL</strong></p> <pre><code class="lang-javascript"><span class="hljs-keyword">import</span> jsCookie <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;js-cookie&apos;</span>; <span class="hljs-keyword">let</span> sender = <span class="hljs-literal">null</span>; <span class="hljs-keyword">if</span> (<span class="hljs-built_in">window</span>.location.hash.match(<span class="hljs-regexp">/^#?.+/</span>)) { <span class="hljs-keyword">const</span> { token, senderId } = <span class="hljs-built_in">window</span>.location.hash .replace(<span class="hljs-regexp">/^#/</span>, <span class="hljs-string">&apos;&apos;</span>) .split(<span class="hljs-string">&apos;&amp;&apos;</span>) .map(a =&gt; <span class="hljs-string">`<span class="hljs-subst">${a}</span>`</span>.split(<span class="hljs-string">&apos;=&apos;</span>)) .reduce((o, item) =&gt; (item.length === <span class="hljs-number">2</span> ? <span class="hljs-built_in">Object</span>.assign(o, { [item[<span class="hljs-number">0</span>]]: <span class="hljs-built_in">decodeURIComponent</span>(item[<span class="hljs-number">1</span>]) }) : o), {}); sender = senderId; <span class="hljs-built_in">window</span>.location.hash = <span class="hljs-string">&apos;&apos;</span>; jsCookie.set(<span class="hljs-string">&apos;botToken&apos;</span>, token, { expires: <span class="hljs-number">7</span>, HttpOnly: <span class="hljs-literal">true</span> }); } </code></pre> </li> <li><p><strong>Sending the postback from browser to bot</strong></p> <pre><code class="lang-javascript"><span class="hljs-keyword">import</span> $ <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;jquery&apos;</span>; <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">postBack</span> (<span class="hljs-params">senderId, action, data = {}</span>) </span>{ <span class="hljs-keyword">return</span> { object: <span class="hljs-string">&apos;page&apos;</span>, entry: [{ id: <span class="hljs-string">&apos;page_id&apos;</span>, time: <span class="hljs-built_in">Date</span>.now(), messaging: [{ sender: { id: senderId }, postback: { payload: { action, data } } }] }] }; } <span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">jqueryRequest</span> (<span class="hljs-params">senderId, action, data, callback</span>) </span>{ $.ajax({ method: <span class="hljs-string">&apos;POST&apos;</span>, url: <span class="hljs-string">&apos;/bot&apos;</span>, contentType: <span class="hljs-string">&apos;application/json; charset=utf-8&apos;</span>, data: <span class="hljs-built_in">JSON</span>.stringify(postBack(senderId, action, data)), success: responseData =&gt; callback(<span class="hljs-literal">null</span>, responseData), error: jqXHR =&gt; callback(jqXHR) }); } </code></pre> </li> </ul> </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="nestingRouters.html#example" class="navigation navigation-prev " aria-label="Previous page: Example"> <i class="fa fa-angle-left"></i> </a> <a href="templates.html#button-template" class="navigation navigation-next " aria-label="Next page: Button Template"> <i class="fa fa-angle-right"></i> </a> </div> <script> var gitbook = gitbook || []; gitbook.push(function() { gitbook.page.hasChanged({"page":{"title":"Sending Templates","level":"1.7","depth":1,"next":{"title":"Button Template","level":"1.7.1","depth":2,"anchor":"#button-template","path":"tutorial/templates.md","ref":"tutorial/templates.md#button-template","articles":[]},"previous":{"title":"Example","level":"1.6.2","depth":2,"anchor":"#example","path":"tutorial/nestingRouters.md","ref":"tutorial/nestingRouters.md#example","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/templates.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>