2016-08-15 23:14:28 +02:00
|
|
|
|
local twitter_user = {}
|
2016-07-17 13:22:27 +02:00
|
|
|
|
|
2016-08-04 20:37:12 +02:00
|
|
|
|
require "./miku/encoding"
|
|
|
|
|
|
2016-07-17 13:22:27 +02:00
|
|
|
|
function twitter_user:init(config)
|
|
|
|
|
if not cred_data.tw_consumer_key then
|
2016-08-01 21:07:27 +02:00
|
|
|
|
print('Fehlender Key: tw_consumer_key.')
|
|
|
|
|
print('twitter_user.lua wird nicht aktiviert.')
|
2016-07-17 13:22:27 +02:00
|
|
|
|
return
|
|
|
|
|
elseif not cred_data.tw_consumer_secret then
|
2016-08-01 21:07:27 +02:00
|
|
|
|
print('Fehlender Key: tw_consumer_secret.')
|
|
|
|
|
print('twitter_user.lua wird nicht aktiviert.')
|
2016-07-17 13:22:27 +02:00
|
|
|
|
return
|
|
|
|
|
elseif not cred_data.tw_access_token then
|
2016-08-01 21:07:27 +02:00
|
|
|
|
print('Fehlender Key: tw_access_token.')
|
|
|
|
|
print('twitter_user.lua wird nicht aktiviert.')
|
2016-07-17 13:22:27 +02:00
|
|
|
|
return
|
|
|
|
|
elseif not cred_data.tw_access_token_secret then
|
2016-08-01 21:07:27 +02:00
|
|
|
|
print('Fehlender Key: tw_access_token_secret.')
|
|
|
|
|
print('twitter_user.lua wird nicht aktiviert.')
|
2016-07-17 13:22:27 +02:00
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
twitter_user.triggers = {
|
|
|
|
|
"twitter.com/([A-Za-z0-9-_-.-_-]+)$"
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local consumer_key = cred_data.tw_consumer_key
|
|
|
|
|
local consumer_secret = cred_data.tw_consumer_secret
|
|
|
|
|
local access_token = cred_data.tw_access_token
|
|
|
|
|
local access_token_secret = cred_data.tw_access_token_secret
|
|
|
|
|
|
|
|
|
|
local client = OAuth.new(consumer_key, consumer_secret, {
|
|
|
|
|
RequestToken = "https://api.twitter.com/oauth/request_token",
|
|
|
|
|
AuthorizeUser = {"https://api.twitter.com/oauth/authorize", method = "GET"},
|
|
|
|
|
AccessToken = "https://api.twitter.com/oauth/access_token"
|
|
|
|
|
}, {
|
|
|
|
|
OAuthToken = access_token,
|
|
|
|
|
OAuthTokenSecret = access_token_secret
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
function twitter_user:resolve_url(url)
|
|
|
|
|
local response_body = {}
|
|
|
|
|
local request_constructor = {
|
|
|
|
|
url = url,
|
|
|
|
|
method = "HEAD",
|
|
|
|
|
sink = ltn12.sink.table(response_body),
|
|
|
|
|
headers = {},
|
|
|
|
|
redirect = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
local ok, response_code, response_headers, response_status_line = http.request(request_constructor)
|
|
|
|
|
if ok and response_headers.location then
|
|
|
|
|
return response_headers.location
|
|
|
|
|
else
|
|
|
|
|
return url
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
|
function twitter_user:action(msg, config, matches)
|
2016-07-17 13:22:27 +02:00
|
|
|
|
local twitter_url = "https://api.twitter.com/1.1/users/show/"..matches[1]..".json"
|
|
|
|
|
local response_code, response_headers, response_status_line, response_body = client:PerformRequest("GET", twitter_url)
|
|
|
|
|
local response = json.decode(response_body)
|
|
|
|
|
if response_code ~= 200 then return end
|
|
|
|
|
|
|
|
|
|
local full_name = response.name
|
|
|
|
|
local user_name = response.screen_name
|
|
|
|
|
if response.verified then
|
|
|
|
|
user_name = user_name..' ✅'
|
|
|
|
|
end
|
|
|
|
|
if response.protected then
|
|
|
|
|
user_name = user_name..' 🔒'
|
|
|
|
|
end
|
|
|
|
|
local header = full_name.. " (@" ..user_name.. ")\n"
|
|
|
|
|
|
2016-08-16 13:19:24 +02:00
|
|
|
|
local description = unescape(response.description)
|
2016-07-17 13:22:27 +02:00
|
|
|
|
if response.location then
|
|
|
|
|
location = response.location
|
|
|
|
|
else
|
|
|
|
|
location = ''
|
|
|
|
|
end
|
|
|
|
|
if response.url and response.location ~= '' then
|
|
|
|
|
url = ' | '..twitter_user:resolve_url(response.url)..'\n'
|
|
|
|
|
elseif response.url and response.location == '' then
|
|
|
|
|
url = twitter_user:resolve_url(response.url)..'\n'
|
|
|
|
|
else
|
|
|
|
|
url = '\n'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local body = description..'\n'..location..url
|
|
|
|
|
|
|
|
|
|
local favorites = comma_value(response.favourites_count)
|
|
|
|
|
local follower = comma_value(response.followers_count)
|
|
|
|
|
local following = comma_value(response.friends_count)
|
|
|
|
|
local statuses = comma_value(response.statuses_count)
|
|
|
|
|
local footer = statuses..' Tweets, '..follower..' Follower, '..following..' folge ich, '..favorites..' Tweets favorisiert'
|
|
|
|
|
|
|
|
|
|
local pic_url = string.gsub(response.profile_image_url_https, "normal", "400x400")
|
2016-08-24 15:38:29 +02:00
|
|
|
|
utilities.send_typing(msg.chat.id, 'upload_photo')
|
2016-07-17 13:22:27 +02:00
|
|
|
|
local file = download_to_file(pic_url)
|
|
|
|
|
|
|
|
|
|
local text = header..body..footer
|
|
|
|
|
if string.len(text) > 199 then -- can only send captions with < 200 characters
|
2016-08-24 15:38:29 +02:00
|
|
|
|
utilities.send_photo(msg.chat.id, file, nil, msg.message_id)
|
|
|
|
|
utilities.send_reply(msg, text)
|
2016-07-17 13:22:27 +02:00
|
|
|
|
return
|
|
|
|
|
else
|
2016-08-24 15:38:29 +02:00
|
|
|
|
utilities.send_photo(msg.chat.id, file, text, msg.message_id)
|
2016-07-17 13:22:27 +02:00
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
|
return twitter_user
|