Skip to content

Instantly share code, notes, and snippets.

@cohan
Created December 18, 2017 13:32
Show Gist options
  • Save cohan/ea28ad70941e53a2c925706f13975c36 to your computer and use it in GitHub Desktop.
Save cohan/ea28ad70941e53a2c925706f13975c36 to your computer and use it in GitHub Desktop.
An IRC bot interface to Botman. Would not recommend. Proof of concept only.
#!/usr/bin/env node
require('dotenv').config()
var irc = require('irc');
var request = require('ajax-request');
var foreach = require('foreach');
var client = new irc.Client(process.env.IRC_SERVER, process.env.IRC_NICK, {
channels: ['#home'],
password: process.env.IRC_PASS,
userName: process.env.IRC_NICK,
realName: process.env.IRC_NICK,
port: process.env.IRC_PORT,
localAddress: null,
debug: false,
showErrors: false,
autoRejoin: false,
autoConnect: true,
secure: false,
selfSigned: false,
certExpired: false,
floodProtection: true,
floodProtectionDelay: 1000,
sasl: false,
retryCount: 3,
retryDelay: 2000,
stripColors: true,
channelPrefixes: "&#",
messageSplit: 512,
encoding: ''
});
function respond(recipient, message) {
console.log("["+recipient +"] "+client.nick+": "+message);
client.say(recipient, message);
}
function handleMessage(replyTo, nick, message) {
// Pass the message to the botman backend.
// Do not trust the user as an authentication method.
request.post({
url: process.env.APP_URL + '/botman',
data: {"driver":"web", "userId": nick, "message":message},
headers: {}
},
function (err, res, body) {
if (err) {
console.log("==============================");
console.log("COULD NOT CONNECT TO BACKEND");
console.log(err);
console.log("==============================");
}
else {
data = JSON.parse(body);
foreach(data.messages, function (value, key, array) {
// TODO: Allow the backend to pass back commands
// If we have any messages to send back, do them now
if (value.text) {
respond(replyTo, value.text);
}
});
}
});
}
// Listen to public messages
client.addListener('message', function (from, to, message) {
console.log("["+to+"] "+from+": "+message);
handleMessage(to, from, message);
});
client.addListener('error', function(message) {
console.log('error: ', message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment