<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/initiating-development-of-a-chatbot-with-plumber-and-ngrok/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://aliarsalankazmi.github.io/posts/r/2017/08/04/chatbot-r-intro1.html"> R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Chatbots have become a rage since some time now, with <a rel="nofollow" href="https://shopbot.ebay.com/" target="_blank">firms</a> from <a rel="nofollow" href="https://www.messenger.com/t/cnn/" target="_blank">various</a> <a rel="nofollow" href="https://viewfinder.expedia.com/features/introducing-expedia-bot-facebook-messenger/" target="_blank">sectors</a> investing in such bots that would reduce or remove the need of employing a call centre whilst maintaining similar levels of efficiency, if not greater. A notable example is <a rel="nofollow" href="https://donotpay-search-master.herokuapp.com/" target="_blank">DoNotPay</a>, a ‘Lawyer Chatbot’ which is said to give free legal aid to refugees seeking asylum in the US, Canada, and some asylum support in the UK.</p>
<p>It may be that most of such companies are utilising in-house technology for chatbot development, it is also true that with the advent of developers like <a rel="nofollow" href="http://aliarsalankazmi.github.io/posts/r/2017/08/04/www.api.ai" target="_blank">API.AI</a>, <a rel="nofollow" href="http://aliarsalankazmi.github.io/posts/r/2017/08/04/www.wit.ai" target="_blank">Wit.AI</a>, and with <a rel="nofollow" href="https://developers.facebook.com/docs/messenger-platform" target="_blank">Facebook exposing their API</a> and <a rel="nofollow" href="https://dev.botframework.com/" target="_blank">Microsoft providing their SDKs</a> for Chatbot development, it has become much easier to craft an intelligent, engaging bot.</p>
<p>Although other programming languages like Python and Javascript have a fair amount of <a rel="nofollow" href="https://chatbotsmagazine.com/building-a-facebook-messenger-bot-using-nodejs-heroku-api-ai-8b53c1102531" target="_blank">tutorials</a> and packages that can <a rel="nofollow" href="https://github.com/conbus/fbmq" target="_blank">directly</a> or <a rel="nofollow" href="http://flask.pocoo.org/" target="_blank">indirectly</a> interface with Facebook’s API, those for R have been rather scarce.</p>
<p>Following are my limited, baby steps into the world of chatbot development using R and <a rel="nofollow" href="https://ngrok.com/" target="_blank">ngrok</a>, making use of the Facebook Chatbot Development Platform.</p>
<p>Since we are using Facebook’s platform, we need to first set up a Facebook page, and then set up a Facebook app – both of these are easy steps, so I am going to skip covering them in detail. We create any page on Facebook, assign it a name, and we move on to <a rel="nofollow" href="https://developers.facebook.com/" target="_blank">Facebook for developers</a>, where we create a new Facebook App ID.</p>
<p><a rel="nofollow" href="https://i0.wp.com/aliarsalankazmi.github.io/blog_DA/assets/img/app_setup_page1.PNG" target="_blank"><img src="https://i0.wp.com/aliarsalankazmi.github.io/blog_DA/assets/img/app_setup_page1.PNG?w=456" data-recalc-dims="1"></a></p>
<p>Then, we add ‘Messenger’ as our product to the page, and generate a page access token that our chatbot will use later on.</p>
<p><a rel="nofollow" href="https://i0.wp.com/aliarsalankazmi.github.io/blog_DA/assets/img/app_setup_page2.PNG" target="_blank"><img src="https://i0.wp.com/aliarsalankazmi.github.io/blog_DA/assets/img/app_setup_page2.PNG?w=456" data-recalc-dims="1"></a></p>
<p>To be able to send real-time notifications to our chatbot (and also to verify us!), Facebook uses <a rel="nofollow" href="https://developers.facebook.com/docs/graph-api/webhooks" target="_blank">Webhooks</a>. We are required to list a URL here to which Facebook will direct any notifications we ask it to (keep your copy of <a rel="nofollow" href="https://developers.facebook.com/docs/messenger-platform" target="_blank">Messenger Platform Document</a> at hand, folks!).</p>
<p>But where do we host our app such that we can communicate it via its URL?<br>
A possible candidate is <a rel="nofollow" href="https://www.heroku.com/" target="_blank">Heroku</a> – we can develop a simple chatbot and deploy it to Heroku. It will be live, and thus able to receive any verification requests and notifications that Facebook might send. R does not have an official buildpack for Heroku at the time of this writing, however. There is an <a rel="nofollow" href="https://github.com/virtualstaticvoid/heroku-buildpack-r" target="_blank">unofficial buildpack</a> on Github, but I have been unable to get it working for me <em>and</em> I am feeling lazy. So instead of heroku, I am going to rely on ngrok.</p>
<h3>Set up Webhook and Complete our Verification</h3>
<p>We need to first setup our Webhook, as Facebook will initially send a <a rel="nofollow" href="https://developers.facebook.com/docs/graph-api/webhooks#verification" target="_blank">Verification request</a>. It is only after this verification that Facebook will send notifications to our chatbot via the callback URL. In a verification request, Facebook sends 3 parameters:</p>
<ol>
<li>
<p>hub.mode (this is set to ‘subscribe)</p>
</li>
<li>
<p>hub.challenge (this is a random string that Facebook expects us to send back)</p>
</li>
<li>
<p>hub.verify_token (this is the same Verification Token that we will be entering in our Webhook’s setup)</p>
</li>
</ol>
<h3>Step 1: Write a script with plumber package</h3>
<p>I am going to quickly script a few lines that will enable us to verify our callback URL, using the <a rel="nofollow" href="https://cran.r-project.org/web/packages/plumber/README.html" target="_blank">plumber</a> package. Plumber allows you to create APIs by merely decorating your existing R code with special annotations, and I encourage you to read their <a rel="nofollow" href="https://www.rplumber.io/docs/index.html" target="_blank">documentation</a>. I have, in my current project, found it to provide functionality similar to what flask does for Python.</p>
<div>
<pre><code>library(plumber)
library(httr)
library(data.table)
library(RCurl)
library(jsonlite)


#* @serializer html
#* @get /
myFunc &lt;- function(hub.mode, hub.challenge, hub.verify_token){
if(hub.verify_token == &quot;RRocker&quot;){
	return(hub.challenge)
 } else{
return(paste(&quot;Verification Token mismatch&quot;, 403))
 }
}
</code></pre>
</div>
<h3>Step 2: Save the Script and Run it!</h3>
<p>Once you have copied the script shown above, save it as an R file, open an R session, and run the following code:</p>
<div>
<pre><code>r &lt;- plumb(&quot;</code></pre></div>