Git Product home page Git Product logo

botbuilder-watson's Introduction

A Microsoft Bot Framework & IBM Watson Bot sample

A sample bot using Microsoft Bot framework as communication channel support and IBM Watson Conversation

Deploy to Bluemix

Introduction

The Microsoft Bot Framework provides just what you need to build and connect intelligent bots that interact naturally wherever your users are talking, from text/sms to Skype, Slack, Office 365 mail and other popular services. More details

IBM Watson Conversation service help you to quickly build and deploy chatbots and virtual agents across a variety of channels, including mobile devices, messaging platforms, and even robots. More details

Why using Microsoft Bot Framework & IBM Watson : Openess

Prerequisites

The minimum prerequisites to run this sample are:

  • Create a Bluemix account & a Watson Conversation Workspace

    • In case, you don't have it : Sign up in Bluemix, or use an existing account $ Acquire Watson Conversation credentials

    • The code needs you to provide the username, password, and workspace_id of your Watson Conversation chat bot. If you have an existing Conversation service instance, follow these steps to get your credentials. If you do not have a Conversation service instance, follow these steps to get started.

  • Latest Node.js with NPM. Download it from here.

  • The [Cloud Foundry][cloud_foundry] command-line client

    Note: Ensure that you Cloud Foundry version is up to date
    
    Note: When pushing to cloud foundry you must explicitly specify what buildpack to use i.e. 
    
    cf push -b https://github.com/cloudfoundry/nodejs-buildpack
    
  • The Bot Framework Emulator. To install the Bot Framework Emulator, download it from here. Please refer to this documentation article to know more about the Bot Framework Emulator.

  • Register your bot with the Microsoft Bot Framework. Please refer to this for the instructions. Once you complete the registration, update your bot configuration with the registered config values (See Debugging locally using ngrok or Deploying to IBM Bluemix)

  • Set up your Azure storage with the following steps.

    • Create an Azure account if you dod not already have one. This should be the account that you used for creating your bot
    • Create a CosmoDB database inside that account named botdocs
      screenshot_new_db
    • Inside that database, create a collection named botdata make sure that the partition key is id screenshot_add_container
    • Make a note of the PRIMARY KEY associated with your account. This must be stored in the environment file as storageKey
    • Take note of the URI associated with your bot account, It will be something like : https://<bot_name>.documents.azure.com:443/ - this will be the storageURL you will save in your environment file below.

Instructions

  • Copy or rename the .env_example file to .env (nothing before the dot) and add your Watson conversations details and Microsoft Bot app keys.
# Environment variables
WORKSPACE_ID=
CONVERSATION_USERNAME=
CONVERSATION_PASSWORD=
#Microsoft Bot Info
appId=
appPassword=
storageKey=
storageURL=
  • Before deploying that code, I recommend you fork it to test it locally with BotFramework emulator. more details

botframework

Code Explanation

Watson Conversation Node.js code : I retrieve the Bot session message, pass that message to Watson Conversation, retrieve the response and send back the response to Bot session.

var bot = new builder.UniversalBot(connector, function (session) {

    var payload = {
        workspace_id: workspace,
        context:'',
        input: { text: session.message.text}
    };

   // I use the Bot Conversation Id as identifier.
    var conversationContext = findOrCreateContext(session.message.address.conversation.id);	
    if (!conversationContext) conversationContext = {};
    payload.context = conversationContext.watsonContext;

    conversation.message(payload, function(err, response) {
     if (err) {
       session.send(err);
     } else {
       console.log(JSON.stringify(response, null, 2));
       session.send(response.output.text);
       conversationContext.watsonContext = response.context;
     }
    });

});

I have a specific function to handle the conversation context.

function findOrCreateContext (convId){
      // Let's see if we already have a session for the user convId
    if (!contexts)
        contexts = [];
        
    if (!contexts[convId]) {
        // No session found for user convId, let's create a new one
        //with Michelin concervsation workspace by default
        contexts[convId] = {workspaceId: workspace, watsonContext: {}};
        //console.log ("new session : " + convId);
    }
return contexts[convId];
}

Results

Channels supported by Bot Framework Channels

My bot tested with Bot Framework test client Test Client in Bot Framework

My conversation with Watson via Microsoft Skype Skype

More Information

To get more information about how to get started, please review the following resources:

botbuilder-watson's People

Contributors

bodonova avatar vperrinfr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

botbuilder-watson's Issues

Unable to push the app to cloudfoundary.

I am able to run this locally in emulator but getting this error while pushing to ibm cloud:
npm notice
2021-07-07T11:52:00.99+0530 [STG/0] OUT npm notice New minor version of npm available! 7.18.1 -> 7.19.1
2021-07-07T11:52:00.99+0530 [STG/0] OUT npm notice Changelog: https://github.com/npm/cli/releases/tag/v7.19.1
2021-07-07T11:52:00.99+0530 [STG/0] OUT npm notice Run npm install -g [email protected] to update!
2021-07-07T11:52:00.99+0530 [STG/0] OUT npm notice
2021-07-07T11:52:01.00+0530 [STG/0] OUT npm ERR! code ENOENT
2021-07-07T11:52:01.00+0530 [STG/0] OUT npm ERR! syscall open
2021-07-07T11:52:01.00+0530 [STG/0] OUT npm ERR! path /tmp/app/node_modules/botbuilder/skills-validator/skills-validator-1.0.0.tgz
2021-07-07T11:52:01.01+0530 [STG/0] OUT npm ERR! errno -2
2021-07-07T11:52:01.01+0530 [STG/0] OUT npm ERR! enoent ENOENT: no such file or directory, open '/tmp/app/node_modules/botbuilder/skills-validator/skills-validator-1.0.0.tgz'
2021-07-07T11:52:01.01+0530 [STG/0] OUT npm ERR! enoent This is related to npm not being able to find a file.
2021-07-07T11:52:01.01+0530 [STG/0] OUT npm ERR! enoent
2021-07-07T11:52:01.10+0530 [STG/0] OUT npm ERR! A complete log of this run can be found in:
2021-07-07T11:52:01.11+0530 [STG/0] OUT npm ERR! /tmp/cache/final/.npm/_logs/2021-07-07T06_22_01_035Z-debug.log
2021-07-07T11:52:01.17+0530 [STG/0] OUT ERROR Unable to build dependencies: exit status 254
2021-07-07T11:52:01.64+0530 [STG/0] ERR Failed to compile droplet: Failed to run all supply scripts: exit status 14
2021-07-07T11:52:01.66+0530 [STG/0] OUT Exit status 223
2021-07-07T11:52:01.91+0530 [STG/0] OUT Cell fe12f71d-7f0c-4253-aad2-75fb6d829ddb stopping instance adb0ca58-7ade-4835-91ec-5efd23a06ffa
2021-07-07T11:52:01.91+0530 [STG/0] OUT Cell fe12f71d-7f0c-4253-aad2-75fb6d829ddb destroying container for instance adb0ca58-7ade-4835-91ec-5efd23a06ffa
2021-07-07T11:52:02.16+0530 [API/2] ERR Failed to stage build: staging failed
2021-07-07T11:52:05.15+0530 [STG/0] OUT Cell fe12f71d-7f0c-4253-aad2-75fb6d829ddb successfully destroyed container for instance adb0ca58-7ade-4835-91ec-5efd23a06ffa

Watson Assistant of type API Key Authentication: Getting Error while modifying the code

Dear Vincent,

This code is working perfectly except one issue with my Watson Assistant that it requires API Key authentication instead of username, password. Hence I took the reference of https://github.com/watson-developer-cloud/assistant-simple.git to update app.js file accordingly.

But there is an error coming in conversation - "session ID is not valid". I tried multiple combinations from the Message object:

Message detail:
{
"text": "hi",
"textFormat": "plain",
"type": "message",
"timestamp": "2020-04-12T07:08:08.914Z",
"entities": [
{
"requiresBotState": true,
"supportsListening": true,
"supportsTts": true,
"type": "ClientCapabilities"
}
],
"localTimestamp": "2020-04-12T12:38:08+05:30",
"textLocale": "en-US",
"sourceEvent": {
"clientActivityID": "1586675288910iiy4uzkuz2j",
"clientTimestamp": "2020-04-12T07:08:08.910Z"
},
"attachments": [],
"address": {
"id": "5d337320-7c8c-11ea-bec6-4f2a8c4ac18f",
"channelId": "emulator",
"user": {
"id": "94aa9068-a55f-41f4-b388-52d3b6db9c05",
"name": "User",
"role": "user"
},
"conversation": {
"id": "5a84cde0-7c8c-11ea-9b6d-9798cbf3a923|livechat"
},
"bot": {
"id": "4a6e15f0-7c83-11ea-9b6d-9798cbf3a923",
"name": "Bot",
"role": "bot"
},
"serviceUrl": "https://62a3805d.ngrok.io"
},
"source": "emulator",
"agent": "botbuilder",
"user": {
"id": "94aa9068-a55f-41f4-b388-52d3b6db9c05",
"name": "User",
"role": "user"
}
}

Below is the edited app.js I tried.

/**

  • Copyright 2017 IBM Corp. All Rights Reserved.
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at
  •  http://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.
    */

var restify = require('restify');
var builder = require('botbuilder');
var Conversation = require('ibm-watson/assistant/v2'); // watson sdk
const { IamAuthenticator, BearerTokenAuthenticator } = require('ibm-watson/auth');

require('dotenv').config({silent: true});

// set up Azure storegae for the bot
var azure = require('botbuilder-azure');

// storageKey and storageURL are required psrameters in the environment
var storageKey=process.env.storageKey;
if (storageKey) {
console.log("process.env.storageKey "+ process.env.storageKey);
} else {
console.error('storageKey must be specified in environment');
process.exit(1);
}
var storageURL=process.env.storageURL;
if (storageURL) {
console.log("process.env.storageURL "+ process.env.storageURL);
} else {
console.error('storageURL must be specified in environment');
process.exit(1);
}

var documentDbOptions = {
host: storageURL,
masterKey: storageKey,
database: 'botdocs',
collection: 'botdata'
};

var contexts= [];
var docDbClient = new azure.DocumentDbClient(documentDbOptions);
var cosmosStorage = new azure.AzureBotStorage({ gzipData: false }, docDbClient);

// 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 the service wrapper

let authenticator;
if (process.env.ASSISTANT_IAM_APIKEY) {
authenticator = new IamAuthenticator({
apikey: process.env.ASSISTANT_IAM_APIKEY
});
}

var workspace = process.env.WORKSPACE_ID || '';
assistantId=process.env.ASSISTANT_ID
var conv_url = process.env.CONVERSATION_URL || 'https://gateway.watsonplatform.net/assistant/api/';

var conversation = new Conversation({
version: '2019-02-28',
authenticator: authenticator,
conv_url: process.env.ASSISTANT_URL,
disableSslVerification: process.env.DISABLE_SSL_VERIFICATION === 'true' ? true : false
});
console.log("process.env.WORKSPACE_ID "+ process.env.WORKSPACE_ID);
console.log("process.env.appID "+ process.env.appId);
console.log("process.env.appPassword "+ process.env.appPassword);

// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.appId,
appPassword: process.env.appPassword
});

// 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("conversation ID "+ session.message.address.conversation.id);
console.log("Message detail:\n"+JSON.stringify(session.message, null, 2));

if (2048<session.message.text.length) {
  console.warn('Message length is too long '+session.message.text.length+' truncate to 2048');
  session.message.text = session.message.text.substring(0, 2047)
}

var regex = /[\t\n\r]/g
if (null != (bad_chars = session.message.text.match(regex))) {
  console.warn('Input contans bad characters', bad_chars);
  session.message.text = session.message.text.replace(regex, " ");
// } else {
//   console.log('No illegal characters in the input: '+session.message.text);
}

var payload = {
   assistantId: assistantId,
   sessionId: session.message.address.id,
   context:'',
    input: { text: session.message.text}
};

// If the user asked us to start over create a new context
if ((session.message.text.toLowerCase() == 'start over') || (session.message.text.toLowerCase() == 'start_over')) {
  var convId = session.message.address.conversation.id;
  console.log('Starting a new Conversation for '+convId);
  if (contexts[convId]) 
    delete contexts[convId];
}

var conversationContext = findOrCreateContext(session.message.address.conversation.id);	
if (!conversationContext) conversationContext = {};
payload.context = conversationContext.watsonContext;

conversation.message(payload, function(err, response) {
 if (err) {
  console.error(err);
  session.send("ERROR: "+err.message);
 } else {
   console.log("Response:\n"+JSON.stringify(response, null, 2));
   response.output.text.forEach(function(line) {
     console.log('Sending: '+line);
     session.send(line);
   });
  //  session.send(response.output.text);
   conversationContext.watsonContext = response.context;
 }
});

}).set('storage', cosmosStorage);

function findOrCreateContext (convId){
// Let's see if we already have a session for the user convId
if (!contexts[convId]) {
// No session found for user convId, let's create a new one
contexts[convId] = {assistantId: assistantId, watsonContext: {'client_type': 'MS_Teams'}};
console.log('Creating a new context structure for conversation '+ convId);
} else {
console.log("Reusing Context:\n"+JSON.stringify(contexts[convId], null, 2));
}
return contexts[convId];
}
BotEmulator

//Looking forward to your help for resolving the issue.

Bot not working in Test in Web Chat option in Azure.

I have followed all the steps and I was able to test it in the Botframework Emulator where it was working fine but when I pushed the code in the IBM Cloud and tested the bot in Test in Web Chat option in Azure Bot Service the bot was not working.

I don't understand how the connection between the node.js app deployed in IBM Cloud and Azure Bot service would be established.

In the Azure Bot-->Configuration-->Messaging Endpoint, I have kept the application url that we get in IBM Cloud after deploying the app.

I am attaching the screenshot of the error that I am getting on the Azure side in the Channels
webchat_issue

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.