Wednesday 24 May 2017

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();
     }
    ])
    ])
);



via Anish

No comments:

Post a Comment