Skip to content

Commit

Permalink
Basic implementation is complete. Send the bot /test/ and it responds
Browse files Browse the repository at this point in the history
  • Loading branch information
ffejhog committed Jan 29, 2015
1 parent f9839a4 commit e560202
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 121 deletions.
54 changes: 0 additions & 54 deletions botfunctions.js~

This file was deleted.

36 changes: 5 additions & 31 deletions botfunctions.js → botrecieve.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,12 @@ connection.end();
//Using a text file for now(Good enough for the demo hehehe)
module.exports = {
stringlookup: function(ConverString){

/*
var LByLReader = require('line-by-line');
var lr = new LByLReader('database.txt');
//Insert code to look up database using string
lr.on('error', function (err) {
console.log(err);
});
lr.on('line', function (line) {
var inputedline = line.split(",");
if (inputedline[0] == ConverString){
return inputedline[1];
}
});
lr.on('end', function() {
return "";
});
*/
/*
if(ConverString == "test"){
return 'success';
var JSONStuff = JSON.parse(ConverString);
if(JSONStuff.text.toLowerCase() == "test"){
return "It Works!";
} else {
return 'failed';
return "";
}
*/
var JSONStuff = JSON.parse(ConverString);
return JSONStuff.txt;
}

}
};
34 changes: 34 additions & 0 deletions botsend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = {
botresponder: function(response){
var HTTPS = require('https');
var options, body, botReq;

options = {
hostname: "api.groupme.com",
path: '/v3/bots/post',
method: 'POST'
};

//Bot ID hardcoded for now, pls change later
body = {
"bot_id" : "ID HERE",
"text" : response
};

botReq = HTTPS.request(options, function(res){
if(res.statusCode == 202) {
//Post was successful, yay!
} else {
console.log('rejecting bad status code ' + res.statusCode);
}
});
botReq.on('error', function(err) {
console.log('Error posting mesasage ' + JSON.stringify(err));
});
botReq.on('timeout', function(err) {
console.log('timeout posting error' + JSON.stringify(err));
});
botReq.end(JSON.stringify(body));

}
};
21 changes: 12 additions & 9 deletions readpost.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var http = require('http');
var bottools = require('./botfunctions.js');

var botrecive = require('./botrecieve.js');
var botsend = require('./botsend.js');
var postHTML =
'<html><head><title>Post Example</title></head>' +
'<body>' +
Expand All @@ -12,16 +12,19 @@ var postHTML =
'</body></html>';

http.createServer(function (req, res) {
var postData = "";
req.on('data', function (chunk) {
postData += chunk;
var postData = "";
req.on('data', function (chunk) {
postData += chunk;
});
req.on('end' , function () {
var stringreturn = bottools.stringlookup(postData);
console.log('Data returned: ' + stringreturn);
req.on('end' , function () {
var stringreturn = botrecive.stringlookup(postData);
console.log('Data Sent: ' + stringreturn);
if(stringreturn != ""){
botsend.botresponder(stringreturn);
}
//console.log('POSTed: ' + postData);
res.writeHead(200);
//res.end(postHTML);
});
});
}).listen(8080);

27 changes: 0 additions & 27 deletions readpost.js~

This file was deleted.

0 comments on commit e560202

Please sign in to comment.