botnaut
Version:
Facebook Messenger Chatbot Framework
1,270 lines (694 loc) • 37.3 kB
HTML
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Utils · 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="Ai.html" />
<link rel="prev" href="express.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 active" 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=".." >Utils</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">
<h2 id="classes"><a name="classes" class="plugin-anchor" href="#classes"><i class="fa fa-link" aria-hidden="true"></i></a>Classes</h2>
<dl>
<dt><a href="#MemoryStateStorage">MemoryStateStorage</a></dt>
<dd><p>Memory conversation state storage for testing purposes</p>
</dd>
<dt><a href="#Translate">Translate</a></dt>
<dd></dd>
<dt><a href="#Translate">Translate</a></dt>
<dd></dd>
</dl>
<h2 id="functions"><a name="functions" class="plugin-anchor" href="#functions"><i class="fa fa-link" aria-hidden="true"></i></a>Functions</h2>
<dl>
<dt><a href="#bufferloader">bufferloader(url, [limit], [limitJustByBody], [redirCount])</a> ⇒</dt>
<dd><p>Downloads a file from url into a buffer. Supports size limits and redirects.</p>
</dd>
</dl>
<div id="MemoryStateStorage"> </div>
<h2 id="memorystatestorage"><a name="memorystatestorage" class="plugin-anchor" href="#memorystatestorage"><i class="fa fa-link" aria-hidden="true"></i></a>MemoryStateStorage</h2>
<p>Memory conversation state storage for testing purposes</p>
<p><strong>Kind</strong>: global class </p>
<ul>
<li><a href="#MemoryStateStorage">MemoryStateStorage</a><ul>
<li><a href="#MemoryStateStorage_getOrCreateAndLock">.getOrCreateAndLock(senderId, defaultState)</a> ⇒ <code>Promise.<Object></code></li>
<li><a href="#MemoryStateStorage_onAfterStateLoad">.onAfterStateLoad(req, state)</a> ⇒ <code>Promise.<Object></code></li>
<li><a href="#MemoryStateStorage_saveState">.saveState(state)</a> ⇒ <code>Promise</code></li>
</ul>
</li>
</ul>
<div id="MemoryStateStorage_getOrCreateAndLock"> </div>
<h3 id="memorystatestoragegetorcreateandlocksenderid-defaultstate-⇒-promiseobject"><a name="memorystatestoragegetorcreateandlocksenderid-defaultstate-⇒-promiseobject" class="plugin-anchor" href="#memorystatestoragegetorcreateandlocksenderid-defaultstate-⇒-promiseobject"><i class="fa fa-link" aria-hidden="true"></i></a>memoryStateStorage.getOrCreateAndLock(senderId, defaultState) ⇒ <code>Promise.<Object></code></h3>
<p><strong>Kind</strong>: instance method of <a href="#MemoryStateStorage"><code>MemoryStateStorage</code></a><br><strong>Returns</strong>: <code>Promise.<Object></code> - - conversation state </p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>senderId</td>
<td><code>any</code></td>
<td>sender identifier</td>
</tr>
<tr>
<td>defaultState</td>
<td><code>Object</code></td>
<td>default state of the conversation</td>
</tr>
</tbody>
</table>
<div id="MemoryStateStorage_onAfterStateLoad"> </div>
<h3 id="memorystatestorageonafterstateloadreq-state-⇒-promiseobject"><a name="memorystatestorageonafterstateloadreq-state-⇒-promiseobject" class="plugin-anchor" href="#memorystatestorageonafterstateloadreq-state-⇒-promiseobject"><i class="fa fa-link" aria-hidden="true"></i></a>memoryStateStorage.onAfterStateLoad(req, state) ⇒ <code>Promise.<Object></code></h3>
<p><strong>Kind</strong>: instance method of <a href="#MemoryStateStorage"><code>MemoryStateStorage</code></a><br><strong>Returns</strong>: <code>Promise.<Object></code> - - conversation state </p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>req</td>
<td><code>Request</code></td>
<td>chat request</td>
</tr>
<tr>
<td>state</td>
<td><code>Object</code></td>
<td>conversation state</td>
</tr>
</tbody>
</table>
<div id="MemoryStateStorage_saveState"> </div>
<h3 id="memorystatestoragesavestatestate-⇒-promise"><a name="memorystatestoragesavestatestate-⇒-promise" class="plugin-anchor" href="#memorystatestoragesavestatestate-⇒-promise"><i class="fa fa-link" aria-hidden="true"></i></a>memoryStateStorage.saveState(state) ⇒ <code>Promise</code></h3>
<p><strong>Kind</strong>: instance method of <a href="#MemoryStateStorage"><code>MemoryStateStorage</code></a> </p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>state</td>
<td><code>Object</code></td>
<td>conversation state</td>
</tr>
</tbody>
</table>
<div id="Translate"> </div>
<h2 id="translate"><a name="translate" class="plugin-anchor" href="#translate"><i class="fa fa-link" aria-hidden="true"></i></a>Translate</h2>
<p><strong>Kind</strong>: global class </p>
<ul>
<li><a href="#Translate">Translate</a><ul>
<li><a href="#new_Translate_new">new Translate()</a></li>
<li><a href="#new_Translate_new">new Translate([options])</a></li>
<li><a href="#Translate_translator">.translator(languages)</a> ⇒ <code>Promise.<object></code></li>
<li><a href="#Translate_middleware">.middleware(languageResolver)</a> ⇒ <code>function</code></li>
</ul>
</li>
</ul>
<div id="new_Translate_new"> </div>
<h3 id="new-translate"><a name="new-translate" class="plugin-anchor" href="#new-translate"><i class="fa fa-link" aria-hidden="true"></i></a>new Translate()</h3>
<p>Tool for text translation</p>
<div id="new_Translate_new"> </div>
<h3 id="new-translateoptions"><a name="new-translateoptions" class="plugin-anchor" href="#new-translateoptions"><i class="fa fa-link" aria-hidden="true"></i></a>new Translate([options])</h3>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>[options]</td>
<td><code>object</code></td>
<td></td>
</tr>
<tr>
<td>[options.sourcePath]</td>
<td><code>string</code></td>
<td>optional source path of translation folder</td>
</tr>
<tr>
<td>[options.fileSuffix]</td>
<td><code>string</code></td>
<td>by default <code>.locale.po</code></td>
</tr>
</tbody>
</table>
<div id="Translate_translator"> </div>
<h3 id="translatetranslatorlanguages-⇒-promiseobject"><a name="translatetranslatorlanguages-⇒-promiseobject" class="plugin-anchor" href="#translatetranslatorlanguages-⇒-promiseobject"><i class="fa fa-link" aria-hidden="true"></i></a>translate.translator(languages) ⇒ <code>Promise.<object></code></h3>
<p>Creates static translator for static settings</p>
<p><strong>Kind</strong>: instance method of <a href="#Translate"><code>Translate</code></a> </p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>languages</td>
<td><code>Array.<string></code></td>
<td>list of required languages</td>
</tr>
</tbody>
</table>
<p><strong>Example</strong> </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { Translate } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'botnaut'</span>);
<span class="hljs-keyword">const</span> translate = <span class="hljs-keyword">new</span> Translate({ sourcePath: __dirname });
<span class="hljs-keyword">const</span> t = translate.translator([<span class="hljs-string">'cs'</span>, <span class="hljs-string">'en'</span>]);
<span class="hljs-comment">// czech</span>
t.cs.t(<span class="hljs-string">'requested text'</span>);
<span class="hljs-comment">// english</span>
t.en.t(<span class="hljs-string">'requested text'</span>);
</code></pre>
<div id="Translate_middleware"> </div>
<h3 id="translatemiddlewarelanguageresolver-⇒-function"><a name="translatemiddlewarelanguageresolver-⇒-function" class="plugin-anchor" href="#translatemiddlewarelanguageresolver-⇒-function"><i class="fa fa-link" aria-hidden="true"></i></a>translate.middleware(languageResolver) ⇒ <code>function</code></h3>
<p>Bots middleware for text translations</p>
<ul>
<li>will be looking for <code><lang>.locale.po</code> by default</li>
</ul>
<p><strong>Kind</strong>: instance method of <a href="#Translate"><code>Translate</code></a> </p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>languageResolver</td>
<td><code>function</code></td>
</tr>
</tbody>
</table>
<p><strong>Example</strong> </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { Translate } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'botnaut'</span>);
<span class="hljs-keyword">const</span> translate = <span class="hljs-keyword">new</span> Translate({ sourcePath: __dirname });
bot.use(translate.middleware((req, res) => <span class="hljs-string">'cs'</span>));
bot.use((req, res) => {
res.text(res.t(<span class="hljs-string">'Translated text'</span>));
});
</code></pre>
<div id="Translate"> </div>
<h2 id="translate"><a name="translate" class="plugin-anchor" href="#translate"><i class="fa fa-link" aria-hidden="true"></i></a>Translate</h2>
<p><strong>Kind</strong>: global class </p>
<ul>
<li><a href="#Translate">Translate</a><ul>
<li><a href="#new_Translate_new">new Translate()</a></li>
<li><a href="#new_Translate_new">new Translate([options])</a></li>
<li><a href="#Translate_translator">.translator(languages)</a> ⇒ <code>Promise.<object></code></li>
<li><a href="#Translate_middleware">.middleware(languageResolver)</a> ⇒ <code>function</code></li>
</ul>
</li>
</ul>
<div id="new_Translate_new"> </div>
<h3 id="new-translate"><a name="new-translate" class="plugin-anchor" href="#new-translate"><i class="fa fa-link" aria-hidden="true"></i></a>new Translate()</h3>
<p>Tool for text translation</p>
<div id="new_Translate_new"> </div>
<h3 id="new-translateoptions"><a name="new-translateoptions" class="plugin-anchor" href="#new-translateoptions"><i class="fa fa-link" aria-hidden="true"></i></a>new Translate([options])</h3>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>[options]</td>
<td><code>object</code></td>
<td></td>
</tr>
<tr>
<td>[options.sourcePath]</td>
<td><code>string</code></td>
<td>optional source path of translation folder</td>
</tr>
<tr>
<td>[options.fileSuffix]</td>
<td><code>string</code></td>
<td>by default <code>.locale.po</code></td>
</tr>
</tbody>
</table>
<div id="Translate_translator"> </div>
<h3 id="translatetranslatorlanguages-⇒-promiseobject"><a name="translatetranslatorlanguages-⇒-promiseobject" class="plugin-anchor" href="#translatetranslatorlanguages-⇒-promiseobject"><i class="fa fa-link" aria-hidden="true"></i></a>translate.translator(languages) ⇒ <code>Promise.<object></code></h3>
<p>Creates static translator for static settings</p>
<p><strong>Kind</strong>: instance method of <a href="#Translate"><code>Translate</code></a> </p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>languages</td>
<td><code>Array.<string></code></td>
<td>list of required languages</td>
</tr>
</tbody>
</table>
<p><strong>Example</strong> </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { Translate } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'botnaut'</span>);
<span class="hljs-keyword">const</span> translate = <span class="hljs-keyword">new</span> Translate({ sourcePath: __dirname });
<span class="hljs-keyword">const</span> t = translate.translator([<span class="hljs-string">'cs'</span>, <span class="hljs-string">'en'</span>]);
<span class="hljs-comment">// czech</span>
t.cs.t(<span class="hljs-string">'requested text'</span>);
<span class="hljs-comment">// english</span>
t.en.t(<span class="hljs-string">'requested text'</span>);
</code></pre>
<div id="Translate_middleware"> </div>
<h3 id="translatemiddlewarelanguageresolver-⇒-function"><a name="translatemiddlewarelanguageresolver-⇒-function" class="plugin-anchor" href="#translatemiddlewarelanguageresolver-⇒-function"><i class="fa fa-link" aria-hidden="true"></i></a>translate.middleware(languageResolver) ⇒ <code>function</code></h3>
<p>Bots middleware for text translations</p>
<ul>
<li>will be looking for <code><lang>.locale.po</code> by default</li>
</ul>
<p><strong>Kind</strong>: instance method of <a href="#Translate"><code>Translate</code></a> </p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>languageResolver</td>
<td><code>function</code></td>
</tr>
</tbody>
</table>
<p><strong>Example</strong> </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { Translate } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'botnaut'</span>);
<span class="hljs-keyword">const</span> translate = <span class="hljs-keyword">new</span> Translate({ sourcePath: __dirname });
bot.use(translate.middleware((req, res) => <span class="hljs-string">'cs'</span>));
bot.use((req, res) => {
res.text(res.t(<span class="hljs-string">'Translated text'</span>));
});
</code></pre>
<div id="bufferloader"> </div>
<h2 id="bufferloaderurl-limit-limitjustbybody-redircount-⇒"><a name="bufferloaderurl-limit-limitjustbybody-redircount-⇒" class="plugin-anchor" href="#bufferloaderurl-limit-limitjustbybody-redircount-⇒"><i class="fa fa-link" aria-hidden="true"></i></a>bufferloader(url, [limit], [limitJustByBody], [redirCount]) ⇒</h2>
<p>Downloads a file from url into a buffer. Supports size limits and redirects.</p>
<p><strong>Kind</strong>: global function<br><strong>Returns</strong>: Promise.<buffer> </buffer></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>url</td>
<td><code>string</code></td>
<td></td>
<td></td>
</tr>
<tr>
<td>[limit]</td>
<td><code>number</code></td>
<td><code>0</code></td>
<td>limit in bytes</td>
</tr>
<tr>
<td>[limitJustByBody]</td>
<td><code>boolean</code></td>
<td><code>false</code></td>
<td>when true, content size in header is ignored</td>
</tr>
<tr>
<td>[redirCount]</td>
<td><code>number</code></td>
<td><code>3</code></td>
<td>maximmum amount of redirects</td>
</tr>
</tbody>
</table>
<p><strong>Example</strong> </p>
<pre><code class="lang-javascript">router.use(<span class="hljs-string">'*'</span>, (req, res, postBack) => {
<span class="hljs-keyword">if</span> (req.isFile()) {
bufferloader(req.attachmentUrl())
.then(buffer => postBack(<span class="hljs-string">'downloaded'</span>, { data: buffer }))
.catch(err => postBack(<span class="hljs-string">'donwloaded'</span>, { err }))
}
});
</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="express.html" class="navigation navigation-prev " aria-label="Previous page: Express/MongoDb tools">
<i class="fa fa-angle-left"></i>
</a>
<a href="Ai.html" class="navigation navigation-next " aria-label="Next page: Ai">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"Utils","level":"2.8","depth":1,"next":{"title":"Ai","level":"2.9","depth":1,"path":"api/Ai.md","ref":"api/Ai.md","articles":[]},"previous":{"title":"Express/MongoDb tools","level":"2.7","depth":1,"path":"api/express.md","ref":"api/express.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/bufferloader.md","mtime":"2018-05-23T15:31:55.098Z","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>