AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

How to save the Chat History?
 
 

Hi, I would like to know if there are some quick trick to save the chat history in a log/txt file for each day or better for chat session, so then it is more easy to filter the chat sessions that contain the text “ERR: No Reply Matched” and make easily the quick “debug” of the chatbot.

 

 
  [ # 1 ]

Logging is up to the chatbot implementation; the RiveScript modules don’t have built-in support for it. So, how you implement logging depends on the programming language, and you’d use whatever method your language normally uses to append text to a file.

Example in Node.js:

var fs import("fs");

// your function for getting a reply
function get_reply(usernamemessage{
    
// get the reply from RiveScript as normal
    
var reply bot.reply(usernamemessage);

    
// text to add to the log file
    
var log username ": " message "\n";
    
log += "Bot: " reply "\n\n";

    
// add to log file
    
fs.appendFile("logs.txt"log, function(err) {});

    
// return the reply (to show the user, or w/e)
    
return reply;

Or like in Python,

def get_reply(usernamemessage):
    
# get a reply like normal
    
reply bot.reply(usernamemessage)

    
# log it
    
with open("log.txt""a") as fh:
        
fh.write("{user}: {message}\nBot: {reply}".format(
            
user=username,
            
message=message,
            
reply=reply,
        )

    return 
reply 

Real world example (Perl bot, see getReply and logTransaction): https://github.com/kirsle/aires-bot/blob/0c97aad9d9c575723b6784b7c819fca301afb985/src/Bot/AiRS.pm#L394

 

 
  [ # 2 ]

Thanks a lot for the quick answer!
I am using Node.js so in which file I should add this code?

var fs import("fs");

// your function for getting a reply
function get_reply(usernamemessage{
    
// get the reply from RiveScript as normal
    
var reply bot.reply(usernamemessage);

    
// text to add to the log file
    
var log username ": " message "\n";
    
log += "Bot: " reply "\n\n";

    
// add to log file
    
fs.appendFile("logs.txt"log, function(err) {});

    
// return the reply (to show the user, or w/e)
    
return reply;
 

 
  [ # 3 ]

In whatever file in your project that currently gets the reply for the user.

It really depends on how your project’s code is structured. I can’t give a copy/paste answer without knowing anything about your code. wink

I assume your code, at some point, actually gets a reply from RiveScript for the user. So, put the code to save the log file to disk at that spot.

 

 
  [ # 4 ]

My vps managed hosting installed node.js for me and the developer installedrivescript following your standard instructions.
Adding the code at the bottom of chat.html not work, the same for rivescript.js.
Adding at the bottom of tcp-server.js and shell.js and test-base.js and test-rivescript.js m they don’t break the chat, but also I don’t find saved files named logs.txt after some seconds..

 

 
  [ # 5 ]

So, RiveScript.js when used on the web browser vs. a Node server are in two entirely different environments.

First I’ll address using RiveScript on the server side (like in node/tcp-server.js), which is what my code snippets above were assuming would be the case.

Here is tcp-server.js modified to do logging: https://gist.github.com/kirsle/21629219e8b513f0650e there is a diff in there that compares it to the original tcp-server.js, and the full source of the new tcp-server.js.

If I run that locally, i.e. `node tcp-server.js` and connect to it in a different terminal, it ends up writing a file named “logs.txt” in the same folder as tcp-server.js (actually, logs.txt would be written in the current working directory, which happened to be the same folder as tcp-server.js when I ran it).

If it can’t create the logs.txt file it may be a permissions issue, depending on how your server is set up. Ask your friendly neighborhood sysadmin (or hosting provider) if you get stuck there. You can also try writing to “/tmp/logs.txt” and see if that works, as /tmp is typically world writable. Make sure that the user that is running the node server has write permissions on the directory it’s trying to write the logs to, and if the current working directory of the server is the right location.

As for running RiveScript.js from the web browser, there is no easy solution for getting a server side log file for the end users of the bot, because RiveScript will be running entirely in their own browser. You’d need some sort of server side scripting (PHP script would do) and have the front-end JS code make an ajax request to send the logs to the PHP script, which could then save them to disk.

 

 
  [ # 6 ]

Hi, if i want to store the chat logs inside a folder on my desktop, i just paste the URL link to the logs.txt?

Example in Node.js:

var fs import("fs");

// your function for getting a reply
function get_reply(usernamemessage{
    
// get the reply from RiveScript as normal
    
var reply bot.reply(usernamemessage);

    
// text to add to the log file
    
var log username ": " message "\n";
    
log += "Bot: " reply "\n\n";

    
// add to log file
    
fs.appendFile("logs.txt"log, function(err) {});

    
// return the reply (to show the user, or w/e)
    
return reply;

 

 

 
  [ # 7 ]
Max Yeo - Apr 23, 2018:

Hi, if i want to store the chat logs inside a folder on my desktop, i just paste the URL link to the logs.txt?

Yeah. That code as written would append to a file named “logs.txt” in the current working directory of the Node process (i.e. if you pointed your terminal at the folder with your bot.js script and ran `node bot.js`, it would write to a `logs.txt` in the same folder). You could change the “logs.txt” to an absolute path like “C:/bot/logs.txt” instead to write it to a specific place regardless of where the program is running from.

 

 
  [ # 8 ]

I did it but to no avail yet :(

I see what u said on top. The line won’t work without node.js?

Like it must be in a js file? Or it can be in a rive file.

 

 
  [ # 9 ]

For it to use `fs.appendFile()` it has to be a Node app because it needs filesystem access, which JavaScript in a web browser can’t get.

If you’re running RiveScript in the web browser, you’d have to save the logs by posting an ajax request to a server-side script (like PHP) instead.

Like (with jQuery)

// your function for getting a reply
function get_reply(usernamemessage{
    
// get the reply from RiveScript as normal
    
var reply bot.reply(usernamemessage);

    
// text to add to the log file
    
var log username ": " message "\n";
    
log += "Bot: " reply "\n\n";

    
// ajax request to log the entry
    
$.ajax({
        url
"/log.cgi",
        
method"POST",
        
data{
            log
log
        }
    }
);

    
// return the reply (to show the user, or w/e)
    
return reply;

The back-end code would then be able to write to the (server) file system and save the log to a text file.

Like Perl example,

#!/usr/bin/env perl

use strict;
use 
warnings;
use 
CGI;

my $q = new CGI();

# get the log param from the ajax request
my $log $q->param("log");

# put it in a file
open(my $fh">>""log.txt");
print 
{$fh} $log;
close($fh);

# return response to ajax request
print "Content-Type: text/plain\n\n";
print 
"logged"
 

 
  [ # 10 ]

Hi Matteo,

Have you tried something like this…

node --inspect chatbot.js 
console.log('ERR: No Reply Matched'); 

 

 

 
  login or register to react