Created
February 22, 2015 02:18
-
-
Save kirsle/21629219e8b513f0650e to your computer and use it in GitHub Desktop.
RiveScript Node TCP Server with Logging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/node/tcp-server.js b/node/tcp-server.js | |
index 22b383f..08e9c02 100644 | |
--- a/node/tcp-server.js | |
+++ b/node/tcp-server.js | |
@@ -4,6 +4,7 @@ | |
// | |
// Run this and then telnet to localhost:2001 and chat with the bot! | |
+var fs = require("fs"); | |
var net = require("net"); | |
var RiveScript = require("../lib/rivescript.js"); | |
@@ -50,6 +51,13 @@ function success_handler (loadcount) { | |
// Log it for the server terminal to see! | |
console.log("[" + socket.name + "] " + message); | |
console.log("[Bot] " + reply + "\n"); | |
+ | |
+ // Log the conversation to disk | |
+ var log = socket.name + ": " + message + "\n" | |
+ + "Bot> " + reply + "\n\n"; | |
+ fs.appendFile("logs.txt", log, function(err) { | |
+ console.error("Failed to save log file: " + err); | |
+ }); | |
}); | |
// Handle disconnects. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// RiveScript-JS | |
// | |
// Node.JS Simple TCP Server | |
// | |
// Run this and then telnet to localhost:2001 and chat with the bot! | |
var fs = require("fs"); | |
var net = require("net"); | |
var RiveScript = require("../lib/rivescript.js"); | |
// Create the bot. | |
var bot = new RiveScript({ debug: false }); | |
bot.loadDirectory("../eg/brain", success_handler, error_handler); | |
//bot.loadFile("../eg/brain/testsuite.rive", success_handler, error_handler); | |
function success_handler (loadcount) { | |
console.log("Load #" + loadcount + " completed!"); | |
bot.sortReplies(); | |
// Start the TCP server. | |
net.createServer(function (socket) { | |
// Identify this client. | |
socket.name = socket.remoteAddress + ":" + socket.remotePort; | |
console.log("User '" + socket.name + "' has connected.\n"); | |
// Send a welcome message. | |
socket.write("Hello " + socket.name + "! This is RiveScript.js v" | |
+ bot.version() + " running on Node!\n" | |
+ "Type /quit to disconnect.\n\n"); | |
// Send their prompt. | |
socket.write("You> "); | |
// Handle incoming messages. | |
socket.on("data", function (data) { | |
var message = "" + data; | |
message = message.replace(/[\x0D\x0A]/g, ""); | |
if (message.indexOf("/quit") == 0) { | |
console.log("User '" + socket.name + "' has quit via /quit.\n"); | |
socket.write("Good-bye!\n"); | |
socket.end(); | |
return; | |
} | |
var reply = bot.reply(socket.name, message); | |
socket.write("Bot> " + reply + "\n"); | |
socket.write("You> "); | |
// Log it for the server terminal to see! | |
console.log("[" + socket.name + "] " + message); | |
console.log("[Bot] " + reply + "\n"); | |
// Log the conversation to disk | |
var log = socket.name + ": " + message + "\n" | |
+ "Bot> " + reply + "\n\n"; | |
fs.appendFile("logs.txt", log, function(err) { | |
console.error("Failed to save log file: " + err); | |
}); | |
}); | |
// Handle disconnects. | |
socket.on("end", function () { | |
console.log("User '" + socket.name + "' has disconnected.\n"); | |
}); | |
}).listen(2001); | |
console.log("TCP server running on port 2001.\n"); | |
} | |
function error_handler (loadcount, err) { | |
console.log("Error loading batch #" + loadcount + ": " + err + "\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment