Szymon Rozga
Practical Bot DevelopmentDesigning and Building Bots with Node.js and Microsoft Bot Framework
Szymon Rozga
Port Washington, New York, USA
ISBN 978-1-4842-3539-3e-ISBN 978-1-4842-3540-9
Library of Congress Control Number: 2018949897
© Szymon Rozga 2018
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
About the Author

Szymon has 15 years of hands on experience in the software development industry. He found a passion working on front end applications on Wall Street. The interest in attention to user interface details would take him on a tour of the different user interface technologies on the Windows, Web and iOS/Android platforms. He has managed teams of engineers on a variety of projects and since 2016, he has dedicated his time to building chat bots for clients across text and voice channels. He has built a practice around the technologies at Blue Metal, An Insight Company, and is involved in a handful of chat bot projects at any time. As Chief Architect of Emerging Technologies, he keeps cognitively flexible by reading and watching presentations about new technologies, educating clients, leading discovery sessions, scoping and guiding engagements, mentoring engineers and helping clients apply technologies such as Chat Bots, Blockchain and Augmented Reality to business problems.

In his spare time, he enjoys taking walks, reading fiction, going to the beach, playing guitar, and spending time with his wife Kim, his son Teddy, and his Golden Retriever, Chelsea.

Preface

In mid-2016, I started work on a fascinating project. A client wanted to have users with type 2 diabetes be able to get advice from an automated coach, in other words, a chat bot. It was a fascinating idea. I had many questions. Why would anyone want to have a natural language conversation with a machine? Is it possible to make this thing smart enough to accomplish its goals? How do you even begin creating a chat bot? Through which means are users supposed to interact with it? When the project ended, we quickly realized that the technologies we just utilized, natural language understanding, Microsoft’s Bot Framework, and custom machine learning models, could serve as the technical base for a wide array of natural language applications between users and computing systems. Natural language interfaces, after all, were all the rage. The Alexa-powered Echo Dot had just been released, and the general population was quickly becoming fascinated with the idea of communicating with digital assistants by talking to them. My friend and I jumped on the opportunity to become experts in this space.

We tried many of the different platforms such as Api.ai (now DialogFlow), Wit.ai, and Watson Conversation but decided to focus on Microsoft’s Bot Framework as we felt it best complemented the enterprise space. Chat bot startups sprung up across the landscape promising the best bot or bot platform. The field became saturated, and our customers started paying attention. Suddenly, I found myself talking to multiple clients a day. Initially, these were high-level mentorship sessions. What is a chat bot? How does it work? What channels does it work with? Does it learn by itself? Can it integrate with live chat?

Starting around mid-2017, these conversations slowly shifted from educating clients to scoping the development effort for all types of use cases. Clients were starting to apply the technology to solve their business problems. In the second half of 2017, while working on delivering multiple chat bot implementations, a colleague of mine connected me to the editors who would make this book happen. Taking on this project was an easy decision to make; it was a fascinating topic, it was a new space, and it had lots of possibilities. Being slightly on the naïvely optimistic side about a three-month old’s sleep patterns, I jumped on the opportunity.

I decided to write the book in the same way I would mentor engineers on the topics. It is divided roughly into three sections. First, I introduce the topics of chat bots and machine learning (ML) in Chapters 1 and 2 . Although chat bots can, and often do, exist independently of any ML algorithms, the truth is that users expect chat bots to exhibit some forms of intelligence, minimally a bit of natural language understanding. As such, I want to set the state on ML and how it may be applied in natural language conversations. Chapter 3 is a deep dive into Microsoft’s Language Understanding Intelligence Service (LUIS), which we will utilize to create natural language understanding models for our chat bot.

The second section is all about hands-on Bot Framework development. Chapter 4 introduces the concept of conversation design, which is the practice of modeling a chat bot conversation. Chapter 5 guides us through the creation of a Bot Framework chat bot connected to a LUIS model and its deployment into an Azure app service. In Chapter 6 , we take a step back to examine the features and capabilities of the Bot Builder SDK, and in Chapter 7 we integrate our chat bot with an OAuth implementation and external APIs. Chapter 8 is a deeper exploration into Slack bots, and, finally, Chapter 9 explores the ability to connect any channel to our chat bot via the Direct Line API. We marry Twilio Voice into the picture to create a chat bot we can talk to via a phone.

The third section addresses a few additional topics that are essential to chat bot development. Chapter 10 adds extra intelligence abilities to our chat bots by focusing on a select set of Microsoft’s Cognitive Services. Chapter 11 explores two manners of creating custom cards for our chat bots: adaptive cards and custom graphic renderings. We explore human handover in Chapter 12 , we learn about chat bot analytics in Chapter 13 , and, finally, in Chapter 14 we put all our newly gained knowledge to work by creating a simple Alexa skill using Amazon’s Alexa Skills Kit and then replicate the same experience using a Bot Framework bot.

The chat bot space is dynamic. During the development of this book, Facebook acquired Wit.ai and shifted its focus to natural language understanding, Google acquired Api.ai, LUIS changed user interfaces not once but twice, the Bot Framework was officially released and moved to Azure, QnA Maker was officially released, Alexa’s user interface changed after I had written all the content, and Microsoft announced tons of new features at Build 2018 (which luckily did not drastically change the topics treated in this book). We have reached a point where the subjects of this book are relatively stable. It is my hope that the content is essential to any developer looking to begin chat bot development using Microsoft’s Bot Framework.

This project has been a humbling experience and would not have been possible without a small group of people for whose support I am forever grateful. I would like to thank my wife Kim, without whose patience, kindness, support, and late-night help with editing I would have never completed the process. I would also like to thank Jeff Dodge for the collaboration on building our chat bot practice, Bob Familiar for introducing me to the Apress team, and BlueMetal for allowing me the room to work on this project. Big thanks to Matt, Jimmy, and Andrew, and my parents, Hanna and Krzysztof Rozga, for providing much needed moral support and words of encouragement during times when I felt I was white knuckling it. I would also like to acknowledge the Apress editors Natalie and Jessica for their support during the book writing process.

This book is dedicated to Teddy.

—Szymon Rozga

June 1, 2018

Port Washington, New York

Table of Contents

Index 641

About the Technical Reviewers

Alp Tunc

is a software engineer with a master of science degree from Ege University in Izmir, Turkey. He has 20 years of experience in the industry as a developer/architect/project manager on projects of various sizes. He has hands-on experience in a broad range of technologies. Besides technology, he loves freezing moments in spectacular photographs, trekking into the unknown, running, reading, and listening to jazz. He loves cats and dogs.

 
Jim O’Neil

is a Microsoft Azure MVP and senior architect at BlueMetal (an Insight Company), a modern application consulting firm headquartered in Watertown, Massachusetts, where he is primarily focused on designing and implementing IoT solutions for a variety of industries. A former Microsoft Developer Evangelist for the Northeast United States, he remains active in the New England software community as a speaker and organizer of technical and nonprofit events, namely, New England GiveCamp. In his spare time, he noodles around in genealogy and DNA testing through which he’s found both of his birth parents’ families.