Skip to content

Commit

Permalink
Add 83.named-pipe-sample (microsoft#3852)
Browse files Browse the repository at this point in the history
  • Loading branch information
ceciliaavila authored Nov 30, 2022
1 parent 22fcff6 commit 0525264
Show file tree
Hide file tree
Showing 37 changed files with 2,552 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
MicrosoftAppType=
MicrosoftAppId=<BOT-APP-ID>
MicrosoftAppPassword=<BOT-APP-SECRET>
MicrosoftAppTenantId=
WEBSITE_NAME=<BOT-HOST-NAME>
DIRECT_LINE_SECRET=<DIRECT-LINE-SECRET>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable */
module.exports = {
"extends": "standard",
"rules": {
"semi": [2, "always"],
"indent": [2, 4],
"no-return-await": 0,
"space-before-function-paren": [2, {
"named": "never",
"anonymous": "never",
"asyncArrow": "always"
}],
"template-curly-spacing": [2, "always"]
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# NamedPipeBot

Bot Framework v4 named pipe bot sample.

This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to use named pipes to connect the bot with the Direct Line App Service Extension.

## Prerequisites

- [Node.js](https://nodejs.org) version 10.14 or higher

```bash
# determine node version
node --version
```

## To try this sample

- Clone the repository

```bash
git clone https://github.com/microsoft/botbuilder-samples.git
```

- In a terminal, navigate to `samples/javascript_nodejs/83.named-pipe-sample/named-pipe-bot`

```bash
cd samples/javascript_nodejs/83.named-pipe-sample/named-pipe-bot
```

- Install modules

```bash
npm install
```

- Start the bot

```bash
npm start
```

## Try the bot using BotFramework Emulator

The bot should respond using the emulator, however, the named pipes functionality must be tested using webSockets.
Follow the sections below to connect the bot to the DirectLine App Service Extension and test the sample with a WebChat client.

## Deploy the bot to Azure

To test named pipes, the bot needs to be deployed in Azure.
Create the resources in Azure and deploy your bot. See [Deploy your bot to Azure](https://learn.microsoft.com/en-us/azure/bot-service/provision-and-publish-a-bot?view=azure-bot-service-4.0&tabs=multitenant%2Cjavascript) for a complete list of deployment instructions.
Make sure the `web.config` file is generated and add the following settings:
- Enable websockets:
```xml
<webSocket enabled="true" />
```
- Add the _AspNetCore_ handler and rule needed by Direct Line App Service extension to service requests:
```xml
<handlers>
<!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
<add name="aspNetCore" path="*/.bot/*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<rewrite>
<rules>
<!-- Do not interfere with Direct Line App Service extension requests. (This rule should be as high in the rules section as possible to avoid conflicts.) -->
<rule name ="DLASE" stopProcessing="true">
<conditions>
<add input="{REQUEST_URI}" pattern="^/.bot"/>
</conditions>
</rule>
</rules>
</rewrite>
```

## Configure DirectLine App Service Extension

Follow the instructions in [Enable DL ASE](https://learn.microsoft.com/en-us/azure/bot-service/bot-service-channel-directline-extension-node-bot?view=azure-bot-service-4.0#enable-bot-direct-line-app-service-extension) to configure the DirectLine App Service extension in the bot.

> **Note:** The AppService must have CORS Allowed Origins set as '*' for the webchat client to communicate with the bot.
Set this value in the AppService configuration under the API settings:
![corsConfig](media/corsConfig.png)

> **Note:** The `WEBSITE_NAME` and `DIRECT_LINE_SECRET` settings must be filled with the AppService name and the DirectLine secret in the `.env` file or in the AppService configuration in Azure Portal.

## Start the WebChat client

Followed this [README](../webchat-client/README.md) to run the WebChat client included in this sample and communicate with your bot.

## Further reading

- [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)
- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?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)
- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest)
- [Azure Portal](https://portal.azure.com)
- [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0)
- [Direct Line App Service Extension](https://docs.microsoft.com/en-us/azure/bot-service/bot-service-channel-directline-extension?view=azure-bot-service-4.0)
- [Cross-Origin Resource Sharing (CORS)](https://docs.microsoft.com/en-us/learn/modules/set-up-cors-website-storage/)
- [Restify](https://www.npmjs.com/package/restify)
- [dotenv](https://www.npmjs.com/package/dotenv)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

const { ActivityHandler, MessageFactory } = require('botbuilder');

class NamedPipeBot extends ActivityHandler {
constructor() {
super();
// See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
this.onMessage(async (context, next) => {
const replyText = `Echo: ${ context.activity.text }`;
await context.sendActivity(MessageFactory.text(replyText, replyText));
// By calling next() you ensure that the next BotHandler is run.
await next();
});

this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
const welcomeText = 'Hello and welcome!';
for (let cnt = 0; cnt < membersAdded.length; ++cnt) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
await context.sendActivity(MessageFactory.text(welcomeText, welcomeText));
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}
}

module.exports.NamedPipeBot = NamedPipeBot;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[config]
command = ./deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/bash

# ----------------------
# KUDU Deployment Script
# Version: 1.0.17
# ----------------------

# Helpers
# -------

exitWithMessageOnError () {
if [ ! $? -eq 0 ]; then
echo "An error has occurred during web site deployment."
echo $1
exit 1
fi
}

# Prerequisites
# -------------

# Verify node.js installed
hash node 2>/dev/null
exitWithMessageOnError "Missing node.js executable, please install node.js, if already installed make sure it can be reached from current environment."

# Setup
# -----

SCRIPT_DIR="${BASH_SOURCE[0]%\\*}"
SCRIPT_DIR="${SCRIPT_DIR%/*}"
ARTIFACTS=$SCRIPT_DIR/../artifacts
KUDU_SYNC_CMD=${KUDU_SYNC_CMD//\"}

if [[ ! -n "$DEPLOYMENT_SOURCE" ]]; then
DEPLOYMENT_SOURCE=$SCRIPT_DIR
fi

if [[ ! -n "$NEXT_MANIFEST_PATH" ]]; then
NEXT_MANIFEST_PATH=$ARTIFACTS/manifest

if [[ ! -n "$PREVIOUS_MANIFEST_PATH" ]]; then
PREVIOUS_MANIFEST_PATH=$NEXT_MANIFEST_PATH
fi
fi

if [[ ! -n "$DEPLOYMENT_TARGET" ]]; then
DEPLOYMENT_TARGET=$ARTIFACTS/wwwroot
else
KUDU_SERVICE=true
fi

if [[ ! -n "$KUDU_SYNC_CMD" ]]; then
# Install kudu sync
echo Installing Kudu Sync
npm install kudusync -g --silent
exitWithMessageOnError "npm failed"

if [[ ! -n "$KUDU_SERVICE" ]]; then
# In case we are running locally this is the correct location of kuduSync
KUDU_SYNC_CMD=kuduSync
else
# In case we are running on kudu service this is the correct location of kuduSync
KUDU_SYNC_CMD=$APPDATA/npm/node_modules/kuduSync/bin/kuduSync
fi
fi

# Node Helpers
# ------------

selectNodeVersion () {
NPM_CMD=npm
NODE_EXE=node
}

##################################################################################################################################
# Deployment
# ----------

echo Handling node.js deployment.

# 1. KuduSync
if [[ "$IN_PLACE_DEPLOYMENT" -ne "1" ]]; then
"$KUDU_SYNC_CMD" -v 50 -f "$DEPLOYMENT_SOURCE" -t "$DEPLOYMENT_TARGET" -n "$NEXT_MANIFEST_PATH" -p "$PREVIOUS_MANIFEST_PATH" -i ".git;.hg;.deployment;deploy.sh"
exitWithMessageOnError "Kudu Sync failed"
fi

# 2. Select node version
selectNodeVersion

# 3. Install npm packages
if [ -e "$DEPLOYMENT_TARGET/package.json" ]; then
cd "$DEPLOYMENT_TARGET"
echo "Running $NPM_CMD install --production"
eval $NPM_CMD install --production
exitWithMessageOnError "npm failed"
cd - > /dev/null
fi

##################################################################################################################################
echo "Finished successfully."
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[config]
command = deploy.cmd
Loading

0 comments on commit 0525264

Please sign in to comment.