2019-08-13 19:37:22 +03:00
|
|
|
var Promise = require('promise')
|
|
|
|
var BasePlugin = require('./base.js')
|
2017-03-17 21:09:39 +02:00
|
|
|
|
|
|
|
class Twitter extends BasePlugin {
|
2019-08-13 19:37:22 +03:00
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
|
|
|
this.name = 'Twitter'
|
|
|
|
this.config = config
|
|
|
|
}
|
2017-03-17 21:09:39 +02:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
test (input) {
|
2017-03-17 21:09:39 +02:00
|
|
|
// Kernel version
|
2019-08-13 19:37:22 +03:00
|
|
|
if (input.match(/http(s)?:\/\/(www\.)?((twitter\.com\/([a-zA-Z0-9_-]*)\/status\/([a-zA-Z0-9_-]*)))/)) {
|
|
|
|
return true
|
2017-03-17 21:09:39 +02:00
|
|
|
}
|
2019-08-13 19:37:22 +03:00
|
|
|
return false
|
2017-03-17 21:09:39 +02:00
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
getTweet (statusId) {
|
|
|
|
var Twitter = require('twitter')
|
2017-03-17 21:09:39 +02:00
|
|
|
var client = new Twitter({
|
|
|
|
consumer_key: this.config.keys.twitter.consumerKey,
|
|
|
|
consumer_secret: this.config.keys.twitter.consumerSecret,
|
|
|
|
access_token_key: '',
|
|
|
|
access_token_secret: ''
|
2019-08-13 19:37:22 +03:00
|
|
|
})
|
|
|
|
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)
|
2017-03-17 21:09:39 +02:00
|
|
|
} else {
|
2019-08-13 19:37:22 +03:00
|
|
|
reject(new Error())
|
2017-03-17 21:09:39 +02:00
|
|
|
}
|
2019-08-13 19:37:22 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
return promise
|
2017-03-17 21:09:39 +02:00
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
result (input) {
|
|
|
|
var that = this
|
|
|
|
var resultPromise = new Promise(function (resolve, reject) {
|
|
|
|
var res = input.match(/http(s)?:\/\/(www\.)?((twitter\.com\/([a-zA-Z0-9_-]*)\/status\/([a-zA-Z0-9_-]*)))/)
|
|
|
|
if (res !== null) {
|
|
|
|
var status = res[6]
|
|
|
|
that.getTweet(status).then(function (tweet) {
|
|
|
|
resolve('Twiitti: ' + tweet)
|
|
|
|
}, function () {
|
|
|
|
reject(new Error('Twiitti: Jokin virhe tapahtui'))
|
|
|
|
})
|
2017-03-17 21:09:39 +02:00
|
|
|
} else {
|
2019-08-13 19:37:22 +03:00
|
|
|
reject(new Error('Twiitti: Jokin virhe tapahtui'))
|
2017-03-17 21:09:39 +02:00
|
|
|
}
|
2019-08-13 19:37:22 +03:00
|
|
|
})
|
|
|
|
return resultPromise
|
2017-03-17 21:09:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
module.exports = Twitter
|