This tutorial is about creating a chatbot running on a Raspberry Pi and adding a Cortana Skills Channel. Simple step by step instructions to introduce someone to the Bot Framework and Cortana Skills. Gentle introduction, consider this as simple 'Hello World' bot.
Why Raspberry Pi? This opens up to a lot of possibilities. You can connect this Johnny-Five and control your IoT devices, use this inside Node-Red, push data to a VR website. Lots of ideas, lots of combinations.
The Invocation name for this bot is 'Echo'. To use, just say 'Cortana, tell Echo hi Raspberry Pi', the command will be routed to Raspberry Pi then sent back for Cortana to repeat what was said.
Microsoft Bot FrameworkBot Framework enables chatbot capability on multiple channels like Skype, Web, Slack, Email, FB and many more. Recently, Cortana channel announced last Microsoft Build 2017. The underlying technologies is just ASP.NET Web API. You can create chatbot from Azure Console, which allows you to create a BotID, an AppID and App Password. These parameters are crucial to associate ChatBot subscription to your API.
Chatbots can be hosted in Azure App service or any server that runs .NET or Node.js. We will host it in Raspberry Pi and use NodeJS.
Bot framework only provides a chat interface and multiple channels support, it's just a web API. LUIS.ai is the intelligence natural language processing (NLP), which is a service from Azure. We'll keep it simple and just focus on Bot Framework, and build the service.
Bot Framework on Raspberry PiFirst, setup Raspbian and install NodeJS on a Raspberry Pi.
Setting Up Your Raspberry Pi
- First, ensure that the latest version of Raspbian is running in your RPi, If you don’t know how to prepare your Pi, you can read here my Getting Started with Raspberry Pi Guide.
Installing Node.js in Your RPi
- Connect your Raspberry Pi to the Inernet, then open your terminal window. Type the following commands:
wget http://node-arm.herokuapp.com/node_latest_armhf.deb sudo dpkg -i node_latest_armhf.deb
Check Your Node.js Installation
- Ensure that your Raspberry Pi has Node.js installed type the following command:
node -v
It should prompt for version number.
Install Ngrok
- Ngrok allows you to expose a web server running on your local machine to the internet. Just tell ngrok what port your web server is listening on.
To install, go to https://ngrok.com/
Clone this repo
https://github.com/rondagdag/cortana-rpibots
git clone
The code is simple. Think about it as a web API server that replies back what was sent.
require('dotenv').config();
var restify = require('restify');
var builder = require('botbuilder');
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
// Listen for messages from users
server.post('/api/messages', connector.listen());
// Receive messages from the user and respond by echoing each message back (prefixed with 'You said:')
var bot = new builder.UniversalBot(connector, function (session) {
console.log(session.message.text);
session.say(session.message.text, session.message.text);
});
This opens up to a lot of possibilities. You can connect this Johnny-Five and control your IoT devices, use this inside Node-Red, push data to a VR website. Lots of ideas, lots of combinations.
Start Ngrok
Since we're running the bot in Raspberry, we need to get an endpoint to use by running tunneling software. To use ngrok to get an endpoint, from a console window type:
ngrok http 3978
This will configure and display an ngrok forwarding link that forwards requests to your bot hosted on port 3978. The URL to the forwarding link should look something like this: https://0d6c4024.ngrok.io.
Append /api/messages
to the link, to form an endpoint URL, it should look something like this: https://0d6c4024.ngrok.io/api/messages
Use this endpoint URL in the Configuration section for your bot in the Bot Framework Portal.
Register a New Bot
- Open Browser and go to https://dev.botframework.com/bots
Fill in Display Name, Bot Handle and Description:
Supply the correct Messaging endpoint from ngrok.io. Create a Microsoft App ID and Password. You'll need this later, take note of it. Fill in your App ID:
Fill in Owners information. Use the email that you use to login to Windows 10 and Cortana.
Now it's time to connect to channels, by default Skype and Web chat is enabled. Click on Cortana Channel:
Fill in Display Name, Invocation name (must be unique and also easy to say), Description then Click Save.
Go back to Raspberry pi, open new terminal, and create a .env file
#install dependencies
cd cortana-rpibots
npm install
# create a .env file
nano .env
- Replace contents to this. Use your App ID and App Password from Bot Framework page.
MICROSOFT_APP_ID='36c01976-XXXXXX-XXXXX-032e6b64cfbe'
MICROSOFT_APP_PASSWORD='XXXXXXXXXXXXXXXXXXX'
- Save using ctrl-x.
Start app.js
node app.js
Test the new bot
- Go back to dev.botframework.com page, and click the Test Button.
- Type your message; if it works, it should return the same message back. Echo... Echo... Echo...
You can also add more testers by going to 'Manage in Cortana Dashboard' page and click on 'Deploy to group'. If you're running windows 10 click on Microphone, and say 'Tell <bot invocation> hello'.
In my case: Tell Echo Hello.
Cortana would say 'Hello'.
Let's try it on Android. Install Cortana Digital Assistant: https://play.google.com/store/apps/details?id=com.microsoft.cortana&hl=en
If everything works, you should see something like this...
This is just a simple use case. Now we can build our personal chatbots and add remote voice control capabilities.
If this project interested you in learning more about Cortana, Bot Framework, NodeJS and Raspberry Pi, please click 'Add Respect' and follow me. Stay tuned, we'll work on more projects related to this.
Checkout this project too...
DIY Vending Machine with Cortana Skills and Bot Framework
Comments