Commit 10e142e6 authored by Sit Melai's avatar Sit Melai

Initial Commit for Public Repository

parents
*~
config.json
node_modules/
{
"name": "rc-bot",
"version": "1.0.0",
"description": "Bot translating Ryzom IG channel into another IG channel.",
"main": "trad-bot.js",
"dependencies": {
"ddp": "^0.12.1",
"ddp-login": "^1.0.10",
"request": "^2.83.0",
"yandex-translate": "^2.1.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "ssh://git@git.khaganat.net:3543/SIELA1915/Ryzom-Talk-Bot.git"
},
"author": "SIELA1915",
"license": "ISC",
"keywords": []
}
#!/usr/bin/env node
"use strict";
/**
* Translates a channel into another one
*
* Based on work by John Szaszvari <jszaszvari@gmail.com>
* Git Repo: https://github.com/jszaszvari/rocketchat-ddp-listener
*
*/
var fs = require("fs");
var config = JSON.parse(fs.readFileSync("./" + process.argv[2], 'UTF-8'));
var translate = require("yandex-translate")(config["yandexKey"]);
var DDP = require("ddp");
var login = require("ddp-login");
var request = require("request");
var ddpClient = new DDP({
host: config["server"],
maintainCollections: true,
useSockJs: true,
autoReconnect: false
});
function connect() {
console.log(ddpClient._buildWsUrl());
request.post({url: config["iframeLogin"], form: {"password": config["password"], "username": config["username"]}}, function (error, response, body) {
var RCToken = "";
if (!error && response.statusCode == 200) {
RCToken = process.env['METEOR_TOKEN'] = body.slice(body.lastIndexOf("loginToken") + 13, body.indexOf("\"", body.lastIndexOf("loginToken")+14));
}
ddpClient.connect(function(err) {
if (err) throw err;
login(ddpClient, {
env: "METEOR_TOKEN",
method: "token",
token: RCToken,
plaintext: false,
retry: 5
}, function(error, userInfo) {
if (error) {
// Something went wrong...
console.log(error);
} else {
// We are now logged in, with userInfo.token as our session auth token.
var token = userInfo.token;
console.log("Authentication Sucessful.\n");
// Subscribe to a message stream from a channel or group
console.log("Attempting to subscribe to the Group/Channel now.\n");
ddpClient.call("rooms/get", [{"$date": 0}], function(err, result) {
var subID = result.find(function(value, index, array) {
if (value["name"] == config["subChannel"]) {
return true;
}
return false;
})["_id"];
var postID = result.find(function(value, index, array) {
if (value["name"] == config["postChannel"]) {
return true;
}
return false;
})["_id"];
ddpClient.subscribe("stream-room-messages", [subID, false], function() {
console.log("Subscription Complete.\n");
ddpClient.call("sendMessage", [{"rid": subID, "msg": "I've got no other choice than follow the orders... (Bot is back up)"}]);
ddpClient.call("sendMessage", [{"rid": postID, "msg": "I've got no other choice than follow the orders... (Bot is back up)"}]);
});
if (config["return"] == true) {
ddpClient.subscribe("stream-room-messages", [postID, false], function() {
console.log("Subscription Complete.\n");
});
}
ddpClient.on("message", function(msg) {
var msgJson = JSON.parse(msg);
if (msgJson["msg"] == "changed") {
var origUser = msgJson["fields"]["args"][0]["u"]["username"];
if (origUser.toLowerCase() != config["username"].toLowerCase()) {
var origContent = msgJson["fields"]["args"][0]["msg"];
var targetRoom = (msgJson["fields"]["args"][0]["rid"]==subID?postID:subID);
var modified = msgJson["fields"]["args"][0]["editedAt"] != undefined;
if (config["translate"] == true) {
translate.translate(origContent, {to: config["langTo"], from: config["langFrom"]}, function(err, res) {
ddpClient.call("sendMessage", [{"rid": targetRoom, "msg": (modified?"EDIT: ":"") + origUser + ": " + res.text}]);
});
} else {
ddpClient.call("sendMessage", [{"rid": targetRoom, "msg": (modified?"EDIT: ":"") + origUser + ": " + origContent}]);
}
}
}
// ddpClient.call("sendMessage", [{"rid": post, "msg": msg}]);
});
});
}
});
});
});
}
connect();
ddpClient.on('socket-close', function(code, message) {
console.log("Close: %s %s", code, message);
setTimeout(connect(), 10000);
});
ddpClient.on('socket-error', function(error) {
console.log("Error: %j", error);
setTimeout(connect(), 10000);
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment