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.
Related
I have tried to understand the Microsoft SDK reference in how to communicate with the bot but there is simply no clear way shown of interacting with it.. From the documentation this is what I could put down so far:
const { BotFrameworkAdapter } = require('botbuilder');
const adapter = new BotFrameworkAdapter({
appId: '123',
appPassword: '123'
});
// Start a new conversation with the user
adapter.createConversation()
Any ideas ?
The Microsoft Bot Framework makes use of the restify server to create an API endpoint for the bot. A bot is simply a web service that will send and receive messages to and from the Bot Connector. You will have to use the following steps to communicate with the bot:
Install Restify
npm install --save restify
Include the restify module
const restify = require('restify');
Set up the restify server
const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978,
function () {
console.log(`\n${ server.name } listening to ${ server.url }`);
}
);
Create the main dialog (here the example uses Echo Bot)
const bot = new EchoBot();
Create a POST endpoint for your server and hook up the adapter to listen for incoming requests
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
// route to main dialog.
await bot.run(context);
});
});
Hope this helps.
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
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.
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, '/');
}
});
}
});
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, '/');
}
});
}
});