├── .gitignore ├── app.js ├── connectorSetup.js ├── demo.gif ├── package.json └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | # git ls-files --others --exclude-from=.git/info/exclude 2 | # Lines that start with '#' are comments. 3 | # For a project mostly in C, the following would be a good set of 4 | # exclude patterns (uncomment them if you want to use them): 5 | # *.[oa] 6 | # *~ 7 | 8 | /lib/tools/ 9 | # Logs 10 | logs 11 | *.log 12 | npm-debug.log* 13 | 14 | # Runtime data 15 | pids 16 | *.pid 17 | *.seed 18 | 19 | # Directory for instrumented libs generated by jscoverage/JSCover 20 | lib-cov 21 | 22 | # Coverage directory used by tools like istanbul 23 | coverage 24 | 25 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 26 | .grunt 27 | 28 | # node-waf configuration 29 | .lock-wscript 30 | 31 | # Compiled binary addons (http://nodejs.org/api/addons.html) 32 | build/Release 33 | 34 | # Dependency directory 35 | node_modules 36 | 37 | # Optional npm cache directory 38 | .npm 39 | 40 | # Optional REPL history 41 | .node_repl_history 42 | 43 | #Azure 44 | iisnode.yml 45 | 46 | .vscode -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | require('./connectorSetup.js')(); 2 | 3 | //Bot listening for inbound backchannel events - in this case it only listens for events named "buttonClicked" 4 | bot.on("event", function (event) { 5 | var msg = new builder.Message().address(event.address); 6 | msg.textLocale("en-us"); 7 | if (event.name === "buttonClicked") { 8 | msg.text("I see that you just pushed that button"); 9 | } 10 | bot.send(msg); 11 | }) 12 | 13 | //Basic root dialog which takes an inputted color and sends a changeBackground event. No NLP, regex, validation here - just grabs input and sends it back as an event. 14 | bot.dialog('/', [ 15 | function (session) { 16 | var reply = createEvent("changeBackground", session.message.text, session.message.address); 17 | session.endDialog(reply); 18 | } 19 | ]); 20 | 21 | //Creates a backchannel event 22 | const createEvent = (eventName, value, address) => { 23 | var msg = new builder.Message().address(address); 24 | msg.data.type = "event"; 25 | msg.data.name = eventName; 26 | msg.data.value = value; 27 | return msg; 28 | } 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /connectorSetup.js: -------------------------------------------------------------------------------- 1 | module.exports = function() { 2 | 3 | var restify = require('restify'); 4 | global.builder = require('botbuilder'); 5 | 6 | var connector = new builder.ChatConnector({ 7 | appId: process.env.MICROSOFT_APP_ID ? process.env.MICROSOFT_APP_ID : '', 8 | appPassword: process.env.MICROSOFT_APP_PASSWORD ? process.env.MICROSOFT_APP_PASSWORD : '', 9 | gzipData: true 10 | }); 11 | 12 | global.bot = new builder.UniversalBot(connector); 13 | 14 | // Setup Restify Server 15 | var server = restify.createServer(); 16 | server.listen(process.env.port || 3978, function () { 17 | console.log('%s listening to %s', server.name, server.url); 18 | }); 19 | server.post('/api/messages', connector.listen()); 20 | bot.use(builder.Middleware.dialogVersion({ version: 0.2, resetCommand: /^reset/i })); 21 | 22 | } -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryanvolum/backChannelBot/68861fb88da77d6b44b9c5729f1a732cb5e5c2f4/demo.gif -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "backchannelbot", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "dependencies": { 7 | "botbuilder": "^3.5.4", 8 | "restify": "^4.3.0" 9 | }, 10 | "devDependencies": {}, 11 | "scripts": { 12 | "test": "echo \"Error: no test specified\" && exit 1" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/ryanvolum/backChannelBot.git" 17 | }, 18 | "author": "Ryan Volum", 19 | "license": "ISC", 20 | "bugs": { 21 | "url": "https://github.com/ryanvolum/backChannelBot/issues" 22 | }, 23 | "homepage": "https://github.com/ryanvolum/backChannelBot#readme" 24 | } 25 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Back Channel Bot 2 | 3 | NOTE: Running this demo requires running the [WebChat backchannel sample page](https://github.com/billba/BotFramework-WebChat/tree/master/samples/backchannel) which sends events of activity name "buttonClicked" and receives events with activity name "changeBackground. 4 | 5 | This is a basic bot that uses the [DirectLine](https://docs.botframework.com/en-us/restapi/directline3/) backchannel to send and receive event messages to an instance of [WebChat](https://github.com/billba/BotFramework-WebChat). 6 | 7 | This highlights the ability for a bot to communicate with a page that embeds the bot through WebChat. In other words, our bot can: 8 | 9 | * Send events to a page that hosts an instance of WebChat - demonstrated by the bot sending an activity of type "event" and of name "changeBackground", which changes the background color of the parent page. 10 | * Listen for events from the page that hosts an instance of WebChat - demonstrated by the bot responding "I see you clicked that button" when it receives an event named buttonClicked. 11 | 12 | ![Dialog Structure](./demo.gif) 13 | 14 | To run the demo, pull the [WebChat repo](https://github.com/billba/BotFramework-WebChat) and follow instructions for running the samples. Create a bot from the [bot portal](dev.botframework.com) and (for testing) add keys to the connectorSetup.js file. Either [deploy the bot to Azure](https://docs.botframework.com/en-us/node/builder/guides/deploying-to-azure/) or [use ngrok to tunnel back into your machine](https://docs.botframework.com/en-us/node/builder/guides/core-concepts/#debugging-locally-using-ngrok) 15 | 16 | 17 | --------------------------------------------------------------------------------