Skip to content

Instantly share code, notes, and snippets.

@bryanjhsu bryanjhsu/liarbot.js Secret
Last active Mar 1, 2017

Embed
What would you like to do?
var twitterAPI = require('node-twitter-api');
var consumerKey = "";
var consumerSecret = "";
var accessToken = "";
var tokenSecret = "";
var myScreenName = "ShoobyBot"
var twitter = new twitterAPI({
consumerKey: consumerKey,
consumerSecret: consumerSecret});
twitter.getStream("user", {}, accessToken, tokenSecret, onData);
function onData(error, streamEvent) {
if (streamEvent.hasOwnProperty('event')) {
var sourceHandle = streamEvent['source']['screen_name'];
var userId = streamEvent['source']['id_str'];
console.log("source: " + streamEvent['source']);
console.log(userId);
// a new follower!
if (streamEvent['event'] == 'follow') {
twitter.friendships(
"create",
{"user_id": userId},
accessToken,
tokenSecret,
function (err, data, resp) { console.log(err); }
);
twitter.direct_messages(
'new',
{
"screen_name": sourceHandle,
"text": "Tell me a secret. I promise I won't tell anyone :)"
},
accessToken,
tokenSecret,
function (err, data, resp) { console.log(err); }
);
}
}
if (streamEvent.hasOwnProperty('direct_message')) {
var dmText = streamEvent['direct_message']['text'];
var senderName = streamEvent['direct_message']['sender']['screen_name'];
// streaming API sends us our own direct messages! skip if we're
// the sender.
if (senderName == myScreenName) {
return;
}
var outgoingText;
// send a response!
twitter.statuses(
"update",
{"status": dmText},
accessToken,
tokenSecret,
function (err, data, resp) { console.log(err); }
);
twitter.direct_messages(
'new',
{
"screen_name": senderName,
"text": "Tell me another secret. I swear I won't tell anyone this time :)"
},
accessToken,
tokenSecret,
function (err, data, resp) { console.log(err); }
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.