{"id":3150,"date":"2017-06-01T16:16:31","date_gmt":"2017-06-01T23:16:31","guid":{"rendered":"https:\/\/www.microsoft.com\/reallifecode\/?p=3150"},"modified":"2020-03-14T21:24:03","modified_gmt":"2020-03-15T04:24:03","slug":"deploying-bots-using-the-serverless-framework","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/ise\/deploying-bots-using-the-serverless-framework\/","title":{"rendered":"Deploying bots using the Serverless framework"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Microsoft recently partnered with the makers of the <a href=\"https:\/\/whimapp.com\">Whim\u00a0service<\/a>\u00a0to\u00a0experiment with conversational user interfaces. Whim is a Mobility-as-a-Service (MaaS) solution\u00a0that allows people to leverage multiple kinds of transportation options with a single monthly fee. The makers of Whim found conversational interfaces interesting in the context of everyday mobility and\u00a0as an additional interaction channel with their service.<\/p>\n<p>After exploring a few potential end-user scenarios, we decided to start building one of them using the <a href=\"https:\/\/dev.botframework.com\">Microsoft Bot Framework<\/a>. You can see the end result <a href=\"https:\/\/www.youtube.com\/watch?v=JrYwiruCyfk\">from this video<\/a>, which shows the basic features of the bot in action within Facebook Messenger.<\/p>\n<p>One of the goals was to integrate the development and deployment of the related\u00a0components with the existing tools and practices of the\u00a0company. The implementation of the Whim service is heavily based on\u00a0serverless computing and <a href=\"https:\/\/serverless.com\/learn\/bootstrapping-the-platform-for-a-billion-dollar-business-opportunity\/\">leveraging the\u00a0Serverless framework<\/a>.<\/p>\n<p>In this code story, we&#8217;ll talk about how we made it possible to\u00a0deploy a Node.js-based\u00a0bot using the Serverless framework and some challenges we had along the way.<\/p>\n<p><!--more--><\/p>\n<p>If you&#8217;d rather jump directly into the code, the <a href=\"https:\/\/github.com\/maasglobal\/whim-bot\">Whim bot can be found on GitHub<\/a>.<\/p>\n<h2>Deployment with\u00a0Serverless framework<\/h2>\n<p>The <a href=\"https:\/\/serverless.com\">Serverless framework<\/a> is a set of tools that make it easier to\u00a0manage projects targeting serverless runtimes. It supports providers like Azure Functions, AWS Lamba, and\u00a0Apache OpenWhisk by implementing abstractions and helpers for things like configuration,\u00a0deployment, and debugging. At\u00a0a high level,\u00a0the steps to deploy a serverless project would be something like:<\/p>\n<pre class=\"lang:sh decode:true\" title=\"Serverless usage\">$ npm install -g serverless\r\n$ cd &lt;your-serverless-project&gt;\r\n\/\/ edit serverless.yml with your configurations\r\n$ serverless deploy<\/pre>\n<p>For more about using the Serverless framework, check out the <a href=\"https:\/\/serverless.com\/framework\/docs\/providers\/aws\/guide\/intro\/\">documentation<\/a>.<\/p>\n<p>Since the Whim backend was already using the AWS Lambda provider, the initial deployment targeted it. While the Microsoft Bot Framework is agnostic as to where a bot is hosted, we ran into two issues that needed to be fixed before the bot was functioning properly.<\/p>\n<p>The first issue related to\u00a0the way authorization headers were passed between the systems. You can read more about <a href=\"https:\/\/github.com\/Microsoft\/BotBuilder\/issues\/1613\">the issue and our fix<\/a>\u00a0on GitHub. Afterward, the requests were properly authenticated\u00a0and reached the bot code from the Facebook Messenger channel as expected.<\/p>\n<p>The second challenge was the differences between the request and response formats expected by the Serverless framework provider vs. the Microsoft Bot Framework. The basic usage is as follows:<\/p>\n<pre class=\"lang:js mark:2,8,14 decode:true\" title=\"Bot sample\">\/\/ Setup Restify Server\r\nvar server = restify.createServer();\r\nserver.listen(process.env.port || process.env.PORT || 3978, function () {\r\n   console.log('%s listening to %s', server.name, server.url); \r\n});\r\n  \r\n\/\/ Create chat connector for communicating with the Bot Framework Service\r\nvar connector = new builder.ChatConnector({\r\n    appId: process.env.MICROSOFT_APP_ID,\r\n    appPassword: process.env.MICROSOFT_APP_PASSWORD\r\n});\r\n\r\n\/\/ Listen for messages from users \r\nserver.post('\/api\/messages', connector.listen());<\/pre>\n<p>You can see that\u00a0what connector.listen() returns above is directly given as a handler for the server created with the Restify framework. There is an assumption about what kind of request is passed to the bot framework code and how the bot framework code indicates when processing of the request has completed.<\/p>\n<p>Restify uses common\u00a0<a href=\"http:\/\/restify.com\/#request-api\">request<\/a> and <a href=\"http:\/\/restify.com\/#response-api\">response<\/a> APIs that, for example, the popular <a href=\"http:\/\/expressjs.com\">Express framework<\/a> supports. However, this format isn&#8217;t what\u00a0serverless providers like AWS Lambda use.<\/p>\n<p>Even though the format is not exactly the same, the concept is. As a result, it is possible to implement a small &#8220;glue code&#8221; in between the components to\u00a0achieve the desired functionality. You can see this solution in\u00a0<a href=\"https:\/\/github.com\/vjrantal\/bot-sample\/blob\/85d0bd58f013c0f827af14064e525ef0bdfd7a0a\/deployment\/serverless.js\">an example<\/a>\u00a0from the AWS Lambda case.<\/p>\n<h2>Other challenges<\/h2>\n<h3>User authentication<\/h3>\n<p>The Whim API requires two-factor authentication before the bot can interact with it. The goal was to make the authentication as easy\u00a0as possible and make it feel well integrated into the overall Facebook Messenger experience.<\/p>\n<p>For that, we leveraged the\u00a0<a href=\"https:\/\/developers.facebook.com\/docs\/messenger-platform\/account-linking\/v2.9\">Facebook Messenger Account Linking API<\/a>\u00a0through the Microsoft Bot Framework by using\u00a0channel-specific messages. You can read full details in\u00a0<a href=\"http:\/\/blog.vjrantal.net\/2016\/11\/15\/facebook-account-linking-with-microsoft-bot-framework\/\">this blog post<\/a>.<\/p>\n<h3>Determining location<\/h3>\n<p>User location is at the core of\u00a0the Whim service, so we paid specific attention to how we could provide the best location picking experience within Facebook Messenger. We achieved it by combining the suggested location from the service with the native location picker found within Facebook Messenger. You can see the implementation of this <a href=\"https:\/\/github.com\/maasglobal\/whim-bot\/blob\/da71e7e92bb207133d775ac9aa686e79c99b631b\/index.js#L479-L509\">custom dialog on GitHub<\/a>.<\/p>\n<h2>Conclusion<\/h2>\n<p>As you can see, we were able to\u00a0use the Serverless framework to manage a Node.js bot project based on the Microsoft Bot Framework. In addition to managing the core bot project, the same tooling can be used to manage other serverless programs. For example, a bot project could\u00a0also include an image-processing component that is implemented as a separate &#8220;microservice.&#8221; The Serverless framework can help in managing\u00a0all these various pieces and how they are tied together.<\/p>\n<p>To get started\u00a0deploying your simple\u00a0bot using the knowledge and mechanisms from this code story, please\u00a0take a look at <a href=\"https:\/\/github.com\/vjrantal\/bot-sample\">the related GitHub repository<\/a>.<\/p>\n<hr \/>\n<p>Cover image from Whim.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Microsoft partnered with MaaS Global  to build a bot using the Microsoft Bot Framework Node.js SDK that can be deployed with the Serverless framework.<\/p>\n","protected":false},"author":21387,"featured_media":10964,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[13,16],"tags":[110,249,275,324],"class_list":["post-3150","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bots","category-devops","tag-bots","tag-microsoft-bot-framework-mbf","tag-node","tag-serverless-framework"],"acf":[],"blog_post_summary":"<p>Microsoft partnered with MaaS Global  to build a bot using the Microsoft Bot Framework Node.js SDK that can be deployed with the Serverless framework.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/ise\/wp-json\/wp\/v2\/posts\/3150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/ise\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/ise\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/ise\/wp-json\/wp\/v2\/users\/21387"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/ise\/wp-json\/wp\/v2\/comments?post=3150"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/ise\/wp-json\/wp\/v2\/posts\/3150\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/ise\/wp-json\/wp\/v2\/media\/10964"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/ise\/wp-json\/wp\/v2\/media?parent=3150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/ise\/wp-json\/wp\/v2\/categories?post=3150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/ise\/wp-json\/wp\/v2\/tags?post=3150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}