-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterhello.js
99 lines (95 loc) · 2.49 KB
/
twitterhello.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const fs = require ("fs");
const utils = require ("daveutils");
const davetwitter = require ("davetwitter");
var config = {
httpPort: process.env.PORT || 1491,
myDomain: "twitterhello.scripting.com",
flLogToConsole: true,
flAllowAccessFromAnywhere: true,
flPostEnabled: true,
pathServerHomePageSource: "index.html",
twitterConsumerKey: undefined, //provided in config file
twitterConsumerSecret: undefined
};
function readConfig (f, theConfig, callback) {
fs.readFile (f, function (err, jsontext) {
if (err) {
console.log ("readConfig: err.message == " + err.message);
}
else {
try {
var jstruct = JSON.parse (jsontext);
for (var x in jstruct) {
theConfig [x] = jstruct [x];
}
}
catch (err) {
console.log ("readConfig: err.message == " + err.message);
}
}
callback ();
});
}
function handleHttpRequest (theRequest) {
var params = theRequest.params;
const token = params.oauth_token;
const secret = params.oauth_token_secret;
function returnError (jstruct) {
theRequest.httpReturn (500, "application/json", utils.jsonStringify (jstruct));
}
function returnData (jstruct) {
if (jstruct === undefined) {
jstruct = {};
}
theRequest.httpReturn (200, "application/json", utils.jsonStringify (jstruct));
}
function httpReturn (err, jstruct) {
if (err) {
returnError (err);
}
else {
returnData (jstruct);
}
}
function returnHtml (htmltext) {
theRequest.httpReturn (200, "text/html", htmltext);
}
function returnServerHomePage () {
if (config.pathServerHomePageSource !== undefined) {
fs.readFile (config.pathServerHomePageSource, function (err, homePageSource) {
if (err) {
console.log ("returnServerHomePage: err.message == " + err.message + ", f == " + config.pathServerHomePageSource);
}
else {
returnHtml (homePageSource);
}
});
}
else {
request (config.urlServerHomePageSource, function (err, response, templatetext) {
if (!err && response.statusCode == 200) {
servePage (templatetext);
}
});
}
}
switch (theRequest.lowermethod) {
case "post":
break;
case "get":
switch (theRequest.lowerpath) {
case "/":
returnServerHomePage ();
return (true);
case "/sendtweet":
davetwitter.sendTweet (token, secret, params.message, undefined, httpReturn);
return (true);
}
break;
}
return (false);
}
readConfig ("config.json", config, function (err) {
config.httpRequestCallback = handleHttpRequest;
davetwitter.start (config);
});