Sample template for building bots using Microsoft Bot Framework
If you like this project, give it a
. If you find issues, create an issue.
- A Skill is a mapping of Intents to Dialogs
When I say 'Login' I want the 'LoginDialog'
git clone git@github.com:User1m/botbuilder-template.git
npm i
npm run build
npm start | npm run server
-
package.json
- Look in thescripts
section for commands you can run currently. This will give you a good idea of how to use this template -
src/console.ts
- Starting your bot with a Console ConnectorConnects a UniversalBot to the command line via a console window.
-
src/server.ts
- Starting your bot with a Chat Connector (used for Bot Framework Emulator)Connects a UniversalBot to multiple channels via the Bot Framework.
-
src/bot.ts
- The core of your bot logic -
src/Skills/
- The core of your bot Dialogs -
src/test/
- Add your bot unit tests
-
Clone botbuilder-template
-
Copy
Start
to<NewSkill>
-
Rename occurrences of the name
"Start"
to<NewSkill>
in copied folder -
Remove
Login, Start
skill imports frombot.ts
-
Register
<NewSkill>
dialogs inbot.ts
-
Change occurrences of
Start
with<NewSkill>
inbot.ts
-
Add your custom dialogs to
<NewSkill>.Dialogs.ts
NOTE:
When you find yourself adding hard-coded bot response strings - move them to
NewSkill.Messages.ts
Cards & Attachments should be put in to
NewSkill.Messages.ts
Remember to break dialogs up to mini-modules - allows you to easily replace and swap out dialogs
Api calls should live in a service
When you want to add a BF component but don't know how... do a quick google search (i.e "Bot Framework Cards") - there are tons of sample code online
-
Configure
Root/Root.Dialog.ts
to lead into your starting dialog -
Remove
Login, Start
fromRoot/Root.Dialog.ts
-
Delete
Login & Start
Skills folders -
npm run clean
-
npm run build
-
npm run watch
- runs a typescript watcher to compile your code in the background -
npm run server
- runs the non-webpacked emulator code so that you can edit your.ts
files and see changes realtime -
npm run test
- test your bot code
npm test
npm run jasmine
Using botbuilder-unit you can write unit test as bot / user scripts like:
hiScript.ts
export = [
{
"user" : "hi"
},
{
"bot" :"How should I call you?"
},
{
"user" : "Timmy"
},
{
"bot" : "Nice to meet you, \"Timmy\"!"
}
];
and update the src/specs/botSpec.ts
to use your conversation scripts:
botSpec.ts
it('Test start flow', (done) => {
const messages = require('./hiScript');
unit(bot, messages).then(function () {
done();
}).catch((error) => {
console.log(error);
});
});
Writing tests requires a good understanding on the testing concept of "mocking". Because the MS Bot Framework employs a lot of complex under-the-hood magic - which is coupled to your code, the best way to do unit testing is just to mock the Bot Framework classes and test for the invocation of methods you write.
This template used SinonJS for mocking.
On a high level:
A Sinon
spy
allows you to wiretap a function. You can get information about the number of times a function was called, what arguments it was called with, and numerous other details. When using a spy, the original function will behave just as normal.
A Sinon
stub
provides the same inspection functionality of a spy, but it allows you to replace the stubbed function with one of your own (often simply returning some fake data).
You'll need a good understanding of Sinon to write effective tests.
As a debugging benefit (if you're using VSCode - highly suggested), there are Mocha Tests
and Run mocha
run configurations that allow you to step through your tests.
See Azure branch for an example
Build the project which will compile your typescript (.ts)
files and Webpack
the project for your into a bundle.js
.
Commit the bundle.js
file and connect your Azure Web App Deployment Option
to your repo (local git, github, etc.).
This will pull the bundle.js
and run right away (No need to npm i
).
- Microsoft BotFramework - The bot framework used
- SWELL Generator
- transmute-bot
Facilitating Growth Capital Funding in Africa with Bots
- Claudius Mbemba - Maintainer - user1m
- Lilian Kasem - LilianKasem
This project is licensed under the MIT License