Professional Documents
Culture Documents
Textbook Practical Bot Development Designing and Building Bots With Node Js and Microsoft Bot Framework Szymon Rozga Ebook All Chapter PDF
Textbook Practical Bot Development Designing and Building Bots With Node Js and Microsoft Bot Framework Szymon Rozga Ebook All Chapter PDF
https://textbookfull.com/product/building-bots-with-microsoft-
bot-framework-creating-intelligent-conversational-interfaces-1st-
edition-kishore-gaddam/
https://textbookfull.com/product/developing-bots-with-microsoft-
bots-framework-create-intelligent-bots-using-ms-bot-framework-
and-azure-cognitive-services-1st-edition-srikanth-machiraju/
https://textbookfull.com/product/introducing-azure-bot-service-
building-bots-for-business-waghmare/
https://textbookfull.com/product/beginning-ai-bot-frameworks-
getting-started-with-bot-development-1st-edition-manisha-biswas/
Building Telegram Bots: Develop Bots in 12 Programming
Languages using the Telegram Bot API 1st Edition
Nicolas Modrzyk
https://textbookfull.com/product/building-telegram-bots-develop-
bots-in-12-programming-languages-using-the-telegram-bot-api-1st-
edition-nicolas-modrzyk/
https://textbookfull.com/product/practical-node-js-building-real-
world-scalable-web-apps-azat-mardan/
https://textbookfull.com/product/rest-api-development-with-node-
js-manage-and-understand-the-full-capabilities-of-successful-
rest-development-doglio/
https://textbookfull.com/product/the-definitive-guide-to-
adonisjs-building-node-js-applications-with-javascript-
christopher-pitt/
Practical Bot
Development
Designing and Building Bots
with Node.js and Microsoft
Bot Framework
—
Szymon Rozga
Practical Bot
Development
Designing and Building
Bots with Node.js and Microsoft
Bot Framework
Szymon Rozga
Practical Bot Development: Designing and Building Bots with Node.js and
Microsoft Bot Framework
Szymon Rozga
Port Washington, New York, USA
iii
Table of Contents
Discourse Analysis����������������������������������������������������������������������������������������42
Common Bot NLU Tasks��������������������������������������������������������������������������������������42
Cloud-Based NLU Systems���������������������������������������������������������������������������������44
Enterprise Space�������������������������������������������������������������������������������������������������45
Conclusion����������������������������������������������������������������������������������������������������������46
iv
Table of Contents
Import/Export Application���������������������������������������������������������������������������������124
Using the LUIS Authoring API����������������������������������������������������������������������������125
Troubleshooting Your Models����������������������������������������������������������������������������126
Conclusion��������������������������������������������������������������������������������������������������������128
v
Table of Contents
vi
Table of Contents
vii
Table of Contents
viii
Table of Contents
ix
Table of Contents
Analytics Platforms�������������������������������������������������������������������������������������������577
Integrating with Dashbot and Chatbase������������������������������������������������������������581
Conclusion��������������������������������������������������������������������������������������������������������590
Index�������������������������������������������������������������������������������������������������641
x
About the Author
Szymon Rozga 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 BlueMetal, 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.
xi
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.
xiii
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
xv
Preface
xvi
Preface
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
xvii
CHAPTER 1
Introduction to
Chat Bots
In recent years, chat bots and artificial intelligence (AI) have become
a hot topic in the tech sector and the public imagination. Chat bots,
computer programs that can communicate using natural language, are
doing everything from ordering pizza to buying clothes to saving money
on parking tickets1 to negotiating among themselves.2 Initially, developing
a chat bot was tantamount to developing an integration with a messaging
platform. There was no easy way to represent a conversation flow in code.
When Microsoft created the Bot Framework and the Bot Builder SDK, this
changed. Microsoft created a rich environment in which the developer
was liberated from the concerns of integrating with individual channels
and could focus on writing code that performed the conversational tasks
a chat bot needs to accomplish. The Bot Builder SDK provided a generic
approach to the development of conversational experiences. Microsoft’s
Bot Connectors implemented the logic to translate from the generic format
to channel-specific messages.
1
obot Lawyers Makes Case Against Parking Tickets:
R
http://www.npr.org/2017/01/16/510096767/
robot-lawyer-makes-the-case-against-parking-tickets
2
Deal or no deal? Training AI bots to negotiate: https://code.facebook.com/
posts/1686672014972296/deal-or-no-deal-training-ai-bots-to-negotiate/
The result is that chat bot development has become significantly more
accessible to millions of developers. Engineers no longer have to learn the
ins and outs of integrating with something like Facebook’s Messenger APIs
or Slack’s Web API. Instead, developers focus on core bot logic and the
conversational experience. Microsoft worries about the rest.
The Bot Builder SDK is available for .NET and Node.js and is run as
an open source MIT-licensed project on GitHub.3 The team is active in
both development and responding to the various issues that development
teams run into. And the team is friendly to boot!
In December 2017, Microsoft made both the Bot Framework and the
Language Understanding Intelligence Service (LUIS) generally available.
LUIS is Microsoft’s natural language service that will aid us in adding
conversational intelligence to our bots. The Bot Framework is now also
called the Azure Bot Service; the two refer to the same thing. As implied
by the name, the Azure Bot Service is now a full-fledged part of Microsoft’s
Azure cloud offering. Microsoft has also provided free tiers of the service so
we can play with the framework to our heart’s content. All of the samples
and techniques in the book can be experimented with at no cost!
Over the last years, all the big tech companies like Microsoft, Facebook,
and Google, as well as many smaller ones, have been taking a stab at
creating the best and easiest-to-use chat bot development frameworks.
The field is very dynamic. Frameworks come and go. Things seem
to change daily. Despite the space’s dynamic nature, Microsoft’s Bot
Framework remains the best platform for developing powerful, fast, and
flexible chat bots. I am thrilled to take you on a journey through chat bot
development using this tool.
3
icrosoft Bot Builder SDK on GitHub: https://github.com/Microsoft/
M
BotBuilder
2
Chapter 1 Introduction to Chat Bots
4
I BM Watson: The inside story of how the Jeopardy-winning supercomputer
was born, and what it wants to do next: http://www.techrepublic.com/
article/ibm-watson-the-inside-story-of-how-the-jeopardy-winning-
supercomputer-was-born-and-what-it-wants-to-do-next/
5
The Great A.I. Awakening: https://www.nytimes.com/2016/12/14/magazine/
the-great-ai-awakening.html
6
Google’s AlphaZero Destroys Stockfish In 100-Game Match: https://www.chess.
com/news/view/google-s-alphazero-destroys-stockfish-in-100-game-match
3
Chapter 1 Introduction to Chat Bots
Yet, reality does not match the expectations set forth by AI’s successes
in some very specific problem areas. Although we have made tremendous
leaps and bounds in terms of natural language processing, computer
vision, emotion detection, and so forth, composing all of these pieces
into a human-like intelligence, usually referred to as Artificial General
Intelligence AGI, is not yet within our grasp and is not a realistic target for
chat bots. For every article that celebrates the tremendous achievements
in the AI space, there’s a matching article downplaying the hype around
the same technology and showing examples of why this type of AI is still
far from perfect (think of the articles showing all the images that computer
vision algorithms still can’t correctly classify). As with any technology
that has been hyped up in the media, we must be reasonable with the
expectations we set on it.
Are our bots going to be agents with human-level intelligence having
conversations with our users? No. Given the technology and the tasks
we want our bots to accomplish, can we make our bots perform those
tasks very well? Absolutely. This book aims to equip the reader with
the necessary skill to build compelling, engaging, and useful chat bots.
It is up to the engineer how much of the latest AI techniques you want
to incorporate during this journey. Certainly, these techniques are not
required for a great chat bot.
4
Chapter 1 Introduction to Chat Bots
Figure 1-1 illustrates our first bot built using Microsoft Bot Framework.
This bot simply returns the same message to the user prefixed by the string
“echo: ”. The logic that runs this experience on the Bot Framework is brain-
dead simple.
5
Chapter 1 Introduction to Chat Bots
This is a chat bot. Basic and not terribly useful, right? We can just as
easily create a YouTube bot that, given user text input, searches for videos
on that topic and sends the user links to those videos (Figures 1-2 and 1-3).
This is another basic bot that does just one thing, and it kind of does
it well. It integrates with the YouTube API, uses your input as a search
parameter, and returns what are referred to in the Bot Framework as cards,
something we will explore later in the book. The images make for a richer
and more engaging experience—a bit more interesting but still rather
basic.
6
Chapter 1 Introduction to Chat Bots
The code for this one is shown next. We make a request to YouTube
and translate the response from YouTube format to Bot Framework cards.
7
Chapter 1 Introduction to Chat Bots
return;
}
result.items.forEach(item => {
const card = new builder.HeroCard(session)
.title(item.snippet.title)
.text(item.snippet.description)
.images([
builder.CardImage.create(session, item.
snippet.thumbnails.medium.url)
])
.buttons([
builder.CardAction.openUrl(session,
'https://www.youtube.com/watch?v=' +
item.id.videoId, 'Watch Video')
]);
cards.push(card);
});
session.send(reply);
});
}
]);
8
Chapter 1 Introduction to Chat Bots
OK, how about this? We can have a bot that, given a statement, can
tell whether it’s a neutral, positive, or negative statement and return an
appropriate response (Figure 1-4). We don’t show it, but the code for this
one is as straightforward as the earlier examples: we fetch a sentiment
score from a simple sentiment REST API and use it to render an answer.
This is a simple example showing how easy our code can integrate with
AI, if we were to go that route. Bots don’t always have to follow a question-
response pattern. Bots can reach out to users proactively. For example, we
could have a fraud alert bot (Figure 1-5).
9
Chapter 1 Introduction to Chat Bots
10
Chapter 1 Introduction to Chat Bots
Now things are starting to get a bit more interesting. We are starting to
take natural language and to act on it.
W
hy Now?
Why are bots becoming such a big deal? Certainly, they have existed in
all kinds of incarnations in old-school apps like IRC7 and AOL Instant
Messenger.8 And these were not little experiments. IRC bots have been
around for a long time. I remember interacting with quite a few bots over
IRC. Being young and naïve when it came to technology, I initially thought
there was an actual human responding to my messages. I quickly grasped
the idea that there was a machine sitting somewhere responding to what I
was writing. The more I interacted with IRC bots, the more I treated them
like a command line. This, however, was all pretty niche technology at the
time. The public wasn’t interacting with bots on a daily basis so there was
no need to cater to natural language interactions.
Today, the way we interact with the technology around us is
completely different, and it is driven by three forces: advancements in AI,
the idea of messaging apps as a conversational intelligence platform, and
voice-activated conversational interfaces.
7
IRC Bots: https://en.wikipedia.org/wiki/IRC_bot
8
SmarterChild: https://en.wikipedia.org/wiki/SmarterChild
11
Chapter 1 Introduction to Chat Bots
9
niversal Turing Machine: https://en.wikipedia.org/wiki/
U
Universal_Turing_machine
10
Von Neumann Architecture: https://en.wikipedia.org/wiki/
Von_Neumann_architecture
11
A Logical Calculus of Ideas Immanent in Nervous Activity: http://www.cs.cmu.
edu/~epxing/Class/10715/reading/McCulloch.and.Pitts.pdf
12
The Three Laws of Robotics: https://en.wikipedia.org/wiki/
Three_Laws_of_Robotics
13
Programming a Computer for Playing Chess: http://archive.
computerhistory.org/projects/chess/related_materials/
text/2-0%20and%202-1.Programming_a_computer_for_playing_chess.
shannon/2-0%20and%202-1.Programming_a_computer_for_playing_chess.
shannon.062303002.pdf
12
Chapter 1 Introduction to Chat Bots
13
Another random document with
no related content on Scribd:
parmi les guides les plus actifs de la civilisation contemporaine, en
bien comme en mal.
— … Pas plus qu’un représentant qualifié du travail, de cette
formidable puissance qui s’appelle « les syndicats ouvriers ». Le
camarade Jouhaux n’a jamais songé à se présenter, et nul n’y pense
pour lui. L’Académie échantillonne les anciennes forces dirigeantes
de la communauté, non pas celles qui ne sont apparues que depuis
Richelieu. En cela elle manque d’imagination. Mais cela viendra un
jour. Par degrés. Très lentement. Comme toutes les vieilles
institutions, l’Académie ne peut évoluer qu’en ayant l’air de ne pas
évoluer. A cet égard elle est presque logée à la même enseigne que
l’Église catholique.
— Et, poursuivit Pamphile, est-ce qu’elle sert à quelque chose,
l’Académie ? J’avoue que je ne discerne pas bien à quoi. Vous
n’allez point, n’est-ce pas, me parler du Dictionnaire. Il serait
dérisoire d’assembler depuis quatre siècles quarante personnes, en
aucune façon du reste, pour la plus grande part, préparées par leur
profession à ce travail, et de les habiller en vert pomme, uniquement
pour rédiger un Dictionnaire !
— Rien de plus certain. Mais, Pamphile, à quoi sert aux Anglais
de mettre, dans l’abbaye de Westminster, les statues de leurs
grands hommes, dont la plupart ne se recommandent point des
mérites de leurs sculpteurs ?
— L’Angleterre les veut ainsi honorer ; ce faisant, elle s’honore
elle-même. Cela lui donne, aux yeux des étrangers et de ses
propres citoyens, quelque grandeur.
— L’Académie Française, pareillement, est une sorte de musée,
mais de personnages encore en vie. Et voyez un peu, entre
parenthèses, l’évolution qui s’est faite dans l’esprit national : en
associant lorsqu’elle fut créée, de grands seigneurs et de simples
écrivains, son fondateur entendait relever ceux-ci devant l’opinion ;
du moins c’est ainsi qu’on le considéra bientôt. A cette heure, c’est
plutôt la présence des écrivains qui relève, devant l’opinion, la
qualité de ceux de ses membres qui ne sont point des
professionnels de la pensée écrite. De là vient même cette erreur
générale, dont vous venez de vous faire l’écho, que pour faire partie
de l’Académie, l’on devrait être auteur. Cela prouve l’éminente
situation des écrivains dans la société contemporaine — en France,
car il n’en est pas tout à fait de même ailleurs. On peut dire que les
lettres de noblesse de la profession littéraire, chez nous, datent de
1635, année, comme chacun sait, de la fondation de l’Académie.
C’est pourquoi les écrivains tiennent tant à en être ; et la sélection
distinguée de la compagnie lui vaut, à l’étranger, une estime qui
n’est pas sans exercer une salutaire influence. L’Académie, on l’a vu
pendant la guerre, et depuis, est un excellent agent de propagande
nationale.
— Voilà pour l’étranger. Mais à l’intérieur ?
— A l’intérieur, au point de vue strictement littéraire, il est bien
possible qu’elle ne serve pas à grand’chose, malgré les
récompenses dont elle est dispensatrice. Indirectement, il n’en est
pas de même.
— Indirectement ?
— Elle agit comme frein régulateur. Il n’est pas d’écrivain de
quelque mérite, c’est-à-dire de quelque ambition, qui ne se figure
avoir l’épée d’académicien dans son plumier. Cela n’est pas sans
exercer une action, après tout bienfaisante, sur sa manière de
concevoir l’œuvre d’art, et son respect de la langue. Par essence, la
profession est anarchique, elle se place au-dessus des conventions
morales et sociales. Il arrive qu’on s’en aperçoive un peu trop, bien
qu’il ne me semble pas mauvais, en somme, qu’il en soit ainsi. Mais
son désordre et, si j’ose dire, son irrespect souvent heureux,
seraient bien plus grands encore si les écrivains ne songeaient
parfois à se réserver, le temps venu, les faveurs de celle qu’entre
eux ils appellent « la vieille dame ».
— Cela me paraît vrai… et je n’y avais point pensé.
— Mon cher Pamphile, ce qu’il y a toujours de plus difficile à
distinguer, c’est ce qu’on a quotidiennement sous les yeux,
justement parce qu’on a l’habitude de le voir, et qu’alors on n’y fait
plus attention. Telles sont les actions et les réactions des différents
éléments de la société contemporaine les uns sur les autres.
— Vous parliez tout à l’heure des prix, si nombreux, que
l’Académie distribue chaque année. Vous n’avez pas l’air d’y porter
grand intérêt.
— C’était pour aller vite, et parce que j’avais autre chose à dire.
En réalité, ils aident à vivre quelques modestes et sérieux
travailleurs que leurs ouvrages n’enrichissent pas, dans le domaine
de l’histoire, même littéraire, et de la morale. Pour ceux de pure
littérature, il n’en va pas tout à fait ainsi, par cette raison sans doute
qu’il y en a trop, et que l’attention s’y égare. Peut-être aussi parce
que, agissant, comme je l’ai dit, à la manière d’un frein, l’Académie
suit de loin le goût du public et les tendances des auteurs, au lieu de
les provoquer.
— Mais il y a aussi les prix de vertu, les prix d’encouragement
aux familles nombreuses, que sais-je encore !
— Oui. Cela est, en principe, excellent. Toutefois je n’envisage
pas sans une certaine inquiétude ce développement des attributions
de l’Académie. Son budget est considérable, elle dispose d’une
large fortune, qui va sans cesse en grandissant. Elle en fait, certes,
le meilleur usage. Pourtant je redoute que, comme celle des
congrégations, cette fortune ne finisse par susciter des convoitises
administratives, encouragées par quelques éléments extrêmes de
l’opinion publique.
— Et alors ?
— Alors, il y aura une crise de l’Académie, extérieure à elle, et
peut-être intérieure.
— Vous le regretteriez ?
— Je l’avoue. L’Académie demeure, quoi qu’on puisse dire, une
jolie plume au chapeau de la communauté française. Elle fait
quelque bien, et nul mal. Elle est connue, du moins de nom, du
dernier des paysans et des ouvriers. Elle est la preuve antique, et
toujours vivante à leur regard, qu’il est chez nous d’autres
puissances que celles de l’argent et de la politique. Cela n’est pas
rien.
OÙ L’ON VA…
CHAPITRE I.
CONSULTATION 5
CHAPITRE II.
LES DÉBUTS DE PAMPHILE 11
CHAPITRE III.
L’AMATEUR 17
CHAPITRE IV.
LA PROFESSION « SECONDE » 22
CHAPITRE V.
PREMIERS ESSAIS, PREMIERS ÉCHECS 28
CHAPITRE VI.
EXPÉRIENCES PERSONNELLES 34
CHAPITRE VII.
LE CONTE 39
CHAPITRE VIII.
DU JOURNALISME 44
CHAPITRE IX.
TYPES DE JOURNALISTES 50
CHAPITRE X.
POLÉMIQUES LITTÉRAIRES CONTEMPORAINES 55
CHAPITRE XI.
UNE OPINION POLITIQUE POUR L’ÉCRIVAIN 59
CHAPITRE XII.
ESPOIRS ET REGRETS 64
CHAPITRE XIII.
VACHES GRASSES ET VACHES MAIGRES 69
CHAPITRE XIV.
PUBLICITÉ LITTÉRAIRE 75
CHAPITRE XV.
LA CRITIQUE 80
CHAPITRE XVI.
PRIX LITTÉRAIRES 86
CHAPITRE XVII.
L’ÉCRIVAIN ET L’ARGENT 91
CHAPITRE XVIII.
LE MARIAGE DE L’ÉCRIVAIN. L’ÉCRIVAINE 97
CHAPITRE XIX.
SALONS LITTÉRAIRES 101
CHAPITRE XX.
L’ÉCRIVAIN ET L’ACADÉMIE 107
CHAPITRE XXI.
OÙ L’ON VA… 118
IMPRIMERIE CRÉTÉ
CORBEIL (S.-ET-O.)
5527-25
*** END OF THE PROJECT GUTENBERG EBOOK L'ÉCRIVAIN ***
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.