70 lines
2.0 KiB
JavaScript
70 lines
2.0 KiB
JavaScript
|
var Promise = require('promise');
|
||
|
var BasePlugin = require('./base.js');
|
||
|
var logger = require('winston');
|
||
|
|
||
|
class Twitter extends BasePlugin {
|
||
|
constructor(config) {
|
||
|
super(config);
|
||
|
this.name = 'Twitter';
|
||
|
this.config = config;
|
||
|
}
|
||
|
|
||
|
test(input) {
|
||
|
// Kernel version
|
||
|
var res = null;
|
||
|
if (res = input.match(/http(s)?:\/\/(www\.)?((twitter\.com\/([a-zA-Z0-9_-]*)\/status\/([a-zA-Z0-9_-]*)))/)) {
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
|
||
|
}
|
||
|
|
||
|
getTweet(statusId) {
|
||
|
var Twitter = require('twitter');
|
||
|
var client = new Twitter({
|
||
|
consumer_key: this.config.keys.twitter.consumerKey,
|
||
|
consumer_secret: this.config.keys.twitter.consumerSecret,
|
||
|
access_token_key: '',
|
||
|
access_token_secret: ''
|
||
|
});
|
||
|
var promise = new Promise(function(resolve, reject) {
|
||
|
client.get('statuses/show/' + statusId, function(error, tweet, response) {
|
||
|
if(!error) {
|
||
|
var user = tweet.user.screen_name;
|
||
|
var time = tweet.created_at;
|
||
|
var dateTime = new Date(time);
|
||
|
var dateTimeLocale = dateTime.toLocaleDateString('fi-FI') + ' ' + dateTime.getHours() + '.' + dateTime.getMinutes();
|
||
|
var str = '@' + user + ': "' + tweet.text + '" ('+ dateTimeLocale +')';
|
||
|
str = str.replace(/(?:\r\n|\r|\n)/g, ' ');
|
||
|
|
||
|
resolve(str);
|
||
|
} else {
|
||
|
reject();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
});
|
||
|
return promise;
|
||
|
}
|
||
|
|
||
|
result(input) {
|
||
|
var that = this;
|
||
|
var resultPromise = new Promise(function(resultResolve, resultReject) {
|
||
|
var res = null;
|
||
|
if (res = input.match(/http(s)?:\/\/(www\.)?((twitter\.com\/([a-zA-Z0-9_-]*)\/status\/([a-zA-Z0-9_-]*)))/)) {
|
||
|
var status = res[6];
|
||
|
that.getTweet(status).then(function(tweet) {
|
||
|
resultResolve('Twiitti: ' + tweet);
|
||
|
}, function(error) {
|
||
|
resultReject('Twiitti: Jokin virhe tapahtui');
|
||
|
});
|
||
|
} else {
|
||
|
resultReject('Twiitti: Jokin virhe tapahtui');
|
||
|
}
|
||
|
});
|
||
|
return resultPromise;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = Twitter;
|