Microsoft Bot Framework: Saving the chat history

2019-09-15 00:33发布

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

1条回答
等我变得足够好
2楼-- · 2019-09-15 00:49

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.

查看更多
登录 后发表回答