How I accidentally created a creepy Halloween bot ... and you can too!
Tags: azure, bot, node, ai
Published: 2020-10-23 03:29:56

> Do you like scary movies?

I decided to create a fun and smart [Halloween bot](https://github.com/lynnaloo/scream-caller-bot) using Azure Bot Service and Cognitive services. Specifically, a bot about Halloween movies. Halloween movies, like Scream.

![Scream movie](https://media.giphy.com/media/xT9KVzBIcz7xsaVjQ4/giphy.gif) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

If I'm making a bot featuring the movie, [Scream](https://en.wikipedia.org/wiki/Scream_(1996_film)), then *why not* replace the main character of the Scream movie - the "caller" - with a new automated robot version? 

![Scream caller](https://media.giphy.com/media/iSbYEhQDyjn20/giphy.gif) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

Why *not* is because an automated robot Scream caller can accidentally end up being a little creepy. And the more accurate the "brains" are for the Scream-bot, the creepier it becomes. 

---

![Transcript of chat](https://dev-to-uploads.s3.amazonaws.com/i/vtnia7tl60bc4eoy8ijr.png) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

---

![Leave me alone](https://media.giphy.com/media/xhaHU2l56OSYM/giphy.gif) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

*But it's Halloween, the time of year for a little creepy and a lot of scary!*

---

![Creepier bot](https://dev-to-uploads.s3.amazonaws.com/i/j8pn2dp8ly7hishecgk5.png)

---

So, now **you** should make a Halloween bot too! Let's get started.

![Killer](https://media.giphy.com/media/3o7aTsqGG4816rTKQU/giphy.gif)

---

### Create a new bot 

Azure Bot Service provides the basic tools for developing bots: the open-source [Bot Framework SDK](https://dev.botframework.com/) and the bot service for connecting bots to channels. The only requirement for getting started is an [Azure subscription](portal.azure.com) and some development experience with Node.js, .NET, or Python for adding some extra bot features.

You can begin with a [sample](https://github.com/microsoft/BotBuilder-Samples) or Yeoman-generated project and connect it to a Bot Service instance, but I prefer to start with Bot Service so that I can download a starter project, including the correct credentials.

#### Create a Bot Service instance in Azure

* Find Bot Services in the [Azure portal](portal.azure.com) and add a new service.
* Select "Web App Bot" as the type and then `Create`.
* Your "Bot handle" must be unique, but you can change the display name for your bot later in the settings.
* "App name" will be the value that will be in your application url: `<app-name>.azurewebsites.net`
* For the starter template, we'll be using the Node.js Echo Bot
* Once it's created, you can visit your resource and test your bot in web chat. 

#### Download your code

* Select "Build" in the Bot Management section of your bot resource and then `Download Bot source code`.

*Note: When downloading your source code, check the box to include settings. This will include your application keys in `.env`.*

* Now that you have the code, you can start development on the bot locally, in your preferred dev environment.

--- 

### Create a bank of knowledge for your bots

![Certain rules](https://media.giphy.com/media/3o7aTkjnoAzNaxPes8/giphy.gif) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

To give your scary (and maybe creepy) bot the ability to answer questions, we'll use QnA Maker service and a knowledge base. You can fill the knowledge base with personalized answers and fill in the conversation gaps with some pre-made "chit-chat." 

* Sign into [QnA Maker Portal](https://qnamaker.ai/) using your Azure credentials.
* Create a new QnA knowledge base or import an [existing creepy Scream knowledge base](https://github.com/lynnaloo/scream-caller-bot/blob/main/scream-knowledge-base.tsv).
* When you're done tweaking the question and answers for your bot and training the model, select "PUBLISH."
* Once your QnA Maker app is published, select "SETTINGS" and note the values from the "Deployment details" section. You’ll need these later.

``` bash
POST /knowledgebases/<knowledge-base-id>/generateAnswer
Host: https://<yourqna>.azurewebsites.net/qnamaker
Authorization: EndpointKey <resource-key>
```

---

### Give your bot the brains

![Zombies](https://media.giphy.com/media/l2YWAOJso1n136fM4/giphy.gif) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;


* Add the botbuilder-ai module to your local bot project
``` bash
npm install -S botbuilder-ai
```

* Add the secret values from your QnA knowledge base to your `.env` file:

``` bash
QnAKnowledgebaseId="knowledge-base-id"
QnAAuthKey="resource-key"
QnAEndpointHostName="https://<yourqna>.azurewebsites.net/qnamaker" 
```

* Update `index.js`

Following the `// Create Adapter` section, add the following code to read your `.env` file. Or you can copy/paste from [here](https://github.com/lynnaloo/scream-caller-bot/blob/main/index.js).

``` javascript
const configuration = {
   knowledgeBaseId: process.env.QnAKnowledgebaseId,
   endpointKey: process.env.QnAAuthKey,
   host: process.env.QnAEndpointHostName
};
```

Update the bot construction to pass in the configuration information. Update `EchoBot` to `ScreamBot`

``` javascript
const screamBot = new ScreamBot(configuration, {});
```

* Update `bot.js` to add a reference to QnAMaker

``` javascript
const { QnAMaker } = require('botbuilder-ai');
```

You can see the final version of `bot.js` [here](https://github.com/lynnaloo/scream-caller-bot/blob/main/bot.js).

---

### Deploy your bot

![Scream mirror](https://media.giphy.com/media/3o7aTvjpZypM4rKH4s/giphy.gif) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

Since your bot is a Node.js web application, we deploy it to Azure just like we would our other applications.

* [VS Code](https://docs.microsoft.com/en-us/azure/devops/pipelines/targets/deploy-to-azure-vscode?view=azure-devops)
* [Command Line](https://docs.microsoft.com/en-us/azure/app-service/scripts/cli-deploy-github?toc=/cli/azure/toc.json)
* [Github Actions](https://docs.microsoft.com/en-us/azure/app-service/deploy-github-actions?tabs=applevel)/Azure DevOps/Other DevOps tool

**Note:** To connect to Bot Service and QnA Maker, it's important to make sure the environment variable are set in the configuration of your App Service instance, or that you deploy a `.env` file (do _not_ include that in your source control).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vuoriievn7ofky8ckcpc.png) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

##### CI/CD with Github Actions

* To deploy your bot for the first time and setup a CI/CD pipeline for your bot, first commit your local code to a repository in Github.
* In the Azure Portal, Select "All App service settings" under App Service Settings in your Bot Service instance.
* Then select "Deployment Center" in the Deployment section to create a Github Action deployment.
* Under the "Settings" tab, complete the form to create the Github Action in your Github repository. This will automatically commit an [Action file](https://github.com/lynnaloo/scream-caller-bot/blob/main/.github/workflows/main_scream-caller-2020.yml) to the `.github/workflows` folder and create the necessary Github secrets to deploy your application to your Azure Web App. 

From this point, your Github action will be triggered when changes are pushed to the `main` branch. You can see the results of your deployments in Github, or in the Azure portal.

![Screen shot](https://dev-to-uploads.s3.amazonaws.com/i/i242jzykl25h6wt3nc4x.png) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;


---

Now that your bot is deployed, you can use it in a variety of ways. Embed your bot in your website. Imagine your scary (creepy) bot as an Alexa voice integration or Slack bot that you can share with your friends? 

![Sidney](https://media.giphy.com/media/no2JeUjkz6EBG/giphy.gif) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;


---

### Next Steps for my super-creepy Halloween Bot

![Sequel](https://media.giphy.com/media/xT9KVe8ctR6MVLUuLm/giphy.gif) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;


* Connect the Scream-bot to a [channel]([channels](https://docs.microsoft.com/en-us/azure/bot-service/bot-service-manage-channels?view=azure-bot-service-4.0)) - Slack, Text, Teams, and more.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ridn3pie89uk4cyk4j0i.png) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

* Make Scream-bot smarter with [LUIS](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/what-is-luis#use-luis-in-a-chat-bot).

* Connect Scream-bot to [Azure Communication Services](https://azure.microsoft.com/en-us/services/communication-services).

**Resources:**
* [Scream-bot](https://github.com/lynnaloo/scream-caller-bot) 
* [Bot Framework Documentation](https://docs.botframework.com)
* [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0)
* [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0)
* [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0)
* [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0)