Start a conversation using bot.beginDialog [duplicate] - node.js

I'm new with Microsoft Bot framework. Right now I'm testing my code on Emulator. I want to send Hello message as soon as your connect. Following is my code.
var restify = require('restify');
var builder = require('botbuilder');
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
var connector = new builder.ChatConnector({
appId: "-- APP ID --",
appPassword: "-- APP PASS --"
});
var bot = new builder.UniversalBot(connector);
server.post('/api/message/',connector.listen());
bot.dialog('/', function (session) {
session.send("Hello");
session.beginDialog('/createSubscription');
});
Above code sends Hello message when user initiate a conversation. I want to send this message as soon as user connects.

Hook into the conversationUpdate event and check when the bot is added. After that, you can just post a message or begin a new dialog (as in the code below I extracted from the ContosoFlowers Node.js sample, though there are many others doing the same).
// Send welcome when conversation with bot is started, by initiating the root dialog
bot.on('conversationUpdate', function (message) {
if (message.membersAdded) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
bot.beginDialog(message.address, '/');
}
});
}
});

Related

Create a brodcast botframework - SDK NodeJS

Can I send a message to all the users who spoke with the bot? It would be a message and it would be for all users. (Broadcast)?
botbuilder 3.14.0 - nodejs
You can use proactive messages for that as it allows you to send the user a message that is not directly related to the current topic of conversation.
Here's a sample on how to send an ad-hoc proactive message:
'use strict';
var restify = require('restify');
var builder = require('botbuilder');
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// setup bot credentials
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
// Bot Storage: Here we register the state storage for your bot.
// Default store: volatile in-memory store - Only for prototyping!
// We provide adapters for Azure Table, CosmosDb, SQL Azure, or you can implement your own!
// For samples and documentation, see: https://github.com/Microsoft/BotBuilder-Azure
var inMemoryStorage = new builder.MemoryBotStorage();
var bot = new builder.UniversalBot(connector).set('storage', inMemoryStorage); // Register in memory storage
// send simple notification
function sendProactiveMessage(address) {
var msg = new builder.Message().address(address);
msg.text('Hello, this is a notification');
msg.textLocale('en-US');
bot.send(msg);
}
var savedAddress;
server.post('/api/messages', connector.listen());
// Do GET this endpoint to delivey a notification
server.get('/api/CustomWebApi', (req, res, next) => {
sendProactiveMessage(savedAddress);
res.send('triggered');
next();
}
);
// root dialog
bot.dialog('/', function(session, args) {
savedAddress = session.message.address;
var message = 'Hello! In a few seconds I\'ll send you a message proactively to demonstrate how bots can initiate messages.';
session.send(message);
message = 'You can also make me send a message by accessing: ';
message += 'http://localhost:' + server.address().port + '/api/CustomWebApi';
session.send(message);
setTimeout(() => {
sendProactiveMessage(savedAddress);
}, 5000);
});
You can also find a complete sample that shows how to send proactive messages using the Bot Builder SDK for Node.js here

Bot Framework builder.prompt.text

I have problem with builder.prompt.text, when user response, then will redirect to main "/" dialog, it wont go to next waterfall/dialog
Question is what may cause that?
My bot runs every day, certain time and checks database which user needs questioning and then will create dialog
im using latest bot builder dependency.
Thanks
var restify = require('restify');
var builder = require('botbuilder');
.....
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 bot
var connector = new builder.ChatConnector({
appId: AppId,
appPassword: AppPass
});
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
.....
bot.dialog('/', [
function (session) {
console.log(session);
}
]);
....
bot.beginDialog(createAddress(user), '/users');
.....
bot.dialog('/users', [
function (session) {
session.sendTyping();
session.options.logger.isEnabled = true;
builder.Prompts.text(session, "Question comes here.");
// after response goes to dialog "/"
},
function (session, results) {
// wont come here
builder.Prompts.text(session, "Question comes here1.");
}
]);

Microsoft Bot Framework: Saving the chat history

I've created a simple chatbot using bot framework and i'm trying to save the bot's chat history locally on my device for now. I've used fs to save the values/arguments the user enters, into a file. For e.g.: their name.
However, I want to include the whole chat conversation i.e. the message the user sends and the reply the bot gives. I tried using fs.appendFile(filename, session, function(err) to capture those dialogs but it just displays [Object object] in the file.
How can I capture the whole chat history? Or at least whatever the user has sent?
My code sample:
var restify = require('restify');
var builder = require('botbuilder');
//=========================================================
// Bot Setup
//=========================================================
// 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 bot
var connector = new builder.ChatConnector({
appId: ''
appPassword: ''
});
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
server.get('/', restify.serveStatic({
directory: __dirname,
default: '/index.html'
}));
var fs = require("fs");
var filename = 'chathistory.json';
//=========================================================
// Bots Dialogs
//=========================================================
var test="test";
bot.dialog('/', new builder.IntentDialog()
.matchesAny([/hi/i, /hello/i], [
function (session) {
session.send('Hi, I am your chatbot.');
session.beginDialog('/step2')
},
bot.dialog('/step2', [
function (session) {
builder.Prompts.text(session,'What is your name?');
},
function(session, args, next) {
test=" , " +args.response;
fs.appendFile(filename, test, function(err){
});
session.send('Hello, ' + args.response + '. How may I help you today?');
name = args.response;
session.endConversation();
}
])
])
);
One way to capture user input and bot input is using middleware. Take a look at the middlewareLogging sample, where the logging scenario is being showcased.

Microsoft Bot framework: Sending Message on connect

I'm new with Microsoft Bot framework. Right now I'm testing my code on Emulator. I want to send Hello message as soon as your connect. Following is my code.
var restify = require('restify');
var builder = require('botbuilder');
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
var connector = new builder.ChatConnector({
appId: "-- APP ID --",
appPassword: "-- APP PASS --"
});
var bot = new builder.UniversalBot(connector);
server.post('/api/message/',connector.listen());
bot.dialog('/', function (session) {
session.send("Hello");
session.beginDialog('/createSubscription');
});
Above code sends Hello message when user initiate a conversation. I want to send this message as soon as user connects.
Hook into the conversationUpdate event and check when the bot is added. After that, you can just post a message or begin a new dialog (as in the code below I extracted from the ContosoFlowers Node.js sample, though there are many others doing the same).
// Send welcome when conversation with bot is started, by initiating the root dialog
bot.on('conversationUpdate', function (message) {
if (message.membersAdded) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
bot.beginDialog(message.address, '/');
}
});
}
});

Why does the bot message not send?

I am using c9 as my dev environment and when running under development the bot does not actually send the message through even though sending.batch was called.
var bot = new builder.UniversalBot(connector);
bot.dialog('/', function (session) {
session.send('Alec said ' + session.message.text);
});
function status(request,reply){
connector.listen(request.raw.req,request.raw.res);
return reply("ok");
}
if (useEmulator) {
var restify = require('restify');
var server = restify.createServer();
server.listen(8080, function() {
console.log('test bot endpont at http://localhost:8080/api/messages');
});
server.post('/api/messages', connector.listen());
} else {
module.exports = { default: connector.listen() }
}
That is the code use to implement server, as taken from azure bot setup, i edited the port as c9 cant use the default port.
results after sending a message through emulator
It seems using cloud 9 server would not allow me to send out a response message my solution was to ssh into my own server at which point the app would work as intended.

Resources