Last active
August 29, 2015 14:15
-
-
Save chitoku-k/4731a93ee5eb058c9ebf to your computer and use it in GitHub Desktop.
悲報ダメです
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Twitter = require("twitter"); | |
var followers = []; | |
var client = new Twitter({ | |
consumer_key: process.env.TWITTER_CONSUMER_KEY, | |
consumer_secret: process.env.TWITTER_CONSUMER_SECRET, | |
access_token_key: process.env.TWITTER_ACCESS_TOKEN, | |
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET, | |
}); | |
client.stream("user", {}, function (stream) { | |
stream.on("data", function (data) { | |
if (data.text !== undefined && !data.retweeted_status) { | |
Process(data); | |
} | |
}); | |
stream.on("error", function (error) { | |
console.log(error); | |
}); | |
}); | |
var callbacks = [{ | |
regex: /(ひとつ|一つ|悲報|報告|気づいたこと)(いいですか|いきます)/, | |
text: function (username, match) { return username + " " + match[1] + "ダメです。"; } | |
}, { | |
regex: /(進捗)どうですか/, | |
text: function (username, match) { return username + "の" + match[1] + "ダメです。"; } | |
}, { | |
regex: /(?:なん|何)でも(?:し|す)/, | |
text: function (username, match) { return username + " ん?"; } | |
}, { | |
regex: /屋上ある?んだけど.?(?:焼いてい?かない[??])/, | |
text: function (username, match) { return username + " あぁ^~いいっすね~"; } | |
} | |
]; | |
function Process(data) { | |
var match; | |
for (var i = 0; i < callbacks.length; i++) { | |
if ((match = data.text.match(callbacks[i].regex)) !== null) { | |
CreateReply(data, callbacks[i].text("@" + data.user.screen_name, match)); | |
} | |
} | |
} | |
function CreateReply(data, text) { | |
if (followers.indexOf(data.user.id_str) === -1) { | |
client.get("friendships/show", { target_id: data.user.id_str }, function (error, obj, response) { | |
if (error) { | |
console.log(error); | |
} | |
if (obj.relationship.target.following) { | |
followers[followers.length] = data.user.id_str; | |
Send(data, text); | |
} | |
}) | |
} else { | |
Send(data, text); | |
} | |
} | |
function Send(data, text) { | |
console.log(text); | |
client.post("statuses/update", { | |
status: text, | |
in_reply_to_status_id: data.id_str | |
}, function () { }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment