2016-06-11 14:46:41 +02:00
|
|
|
local twitter = {}
|
|
|
|
|
|
|
|
function twitter:init(config)
|
|
|
|
if not cred_data.tw_consumer_key then
|
2016-08-15 23:14:28 +02:00
|
|
|
print('Fehlender Key: tw_consumer_key.')
|
|
|
|
print('twitter.lua wird nicht aktiviert.')
|
2016-06-11 14:46:41 +02:00
|
|
|
return
|
|
|
|
elseif not cred_data.tw_consumer_secret then
|
2016-08-15 23:14:28 +02:00
|
|
|
print('Fehlender Key: tw_consumer_secret.')
|
|
|
|
print('twitter.lua wird nicht aktiviert.')
|
2016-06-11 14:46:41 +02:00
|
|
|
return
|
|
|
|
elseif not cred_data.tw_access_token then
|
2016-08-15 23:14:28 +02:00
|
|
|
print('Fehlender Key: tw_access_token.')
|
|
|
|
print('twitter.lua wird nicht aktiviert.')
|
2016-06-11 14:46:41 +02:00
|
|
|
return
|
|
|
|
elseif not cred_data.tw_access_token_secret then
|
2016-08-15 23:14:28 +02:00
|
|
|
print('Fehlender Key: tw_access_token_secret.')
|
|
|
|
print('twitter.lua wird nicht aktiviert.')
|
2016-06-11 14:46:41 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
twitter.triggers = {
|
2016-07-17 13:47:15 +02:00
|
|
|
'twitter.com/[^/]+/statuse?s?/([0-9]+)',
|
|
|
|
'twitter.com/statuse?s?/([0-9]+)'
|
2016-06-11 14:46:41 +02:00
|
|
|
}
|
|
|
|
twitter.doc = [[*Twitter-Link*: Postet Tweet]]
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
2016-10-02 18:02:30 +02:00
|
|
|
function get_tweet(response)
|
2016-06-11 14:46:41 +02:00
|
|
|
local full_name = response.user.name
|
|
|
|
local user_name = response.user.screen_name
|
|
|
|
if response.user.verified then
|
|
|
|
verified = ' ✅'
|
|
|
|
else
|
|
|
|
verified = ''
|
|
|
|
end
|
2016-08-04 13:44:56 +02:00
|
|
|
local header = '<b>Tweet von '..full_name..'</b> (<a href="https://twitter.com/'..user_name..'">@' ..user_name..'</a>'..verified..'):'
|
2016-09-25 20:42:45 +02:00
|
|
|
local text = response.full_text
|
2017-09-12 19:12:06 +02:00
|
|
|
local tweet_link = '\n<a href= "https://twitter.com/'..user_name..'/status/'..response.id_str..'">Tweet aufrufen</a>'
|
2016-06-11 14:46:41 +02:00
|
|
|
|
|
|
|
-- favorites & retweets
|
|
|
|
if response.retweet_count == 0 then
|
|
|
|
retweets = ""
|
|
|
|
else
|
|
|
|
retweets = response.retweet_count..'x retweeted'
|
|
|
|
end
|
|
|
|
if response.favorite_count == 0 then
|
|
|
|
favorites = ""
|
|
|
|
else
|
|
|
|
favorites = response.favorite_count..'x favorisiert'
|
|
|
|
end
|
|
|
|
if retweets == "" and favorites ~= "" then
|
2016-10-12 19:26:05 +02:00
|
|
|
footer = '\n<i>'..favorites..'</i>'
|
2016-06-11 14:46:41 +02:00
|
|
|
elseif retweets ~= "" and favorites == "" then
|
2016-10-12 19:26:05 +02:00
|
|
|
footer = '\n<i>'..retweets..'</i>'
|
2016-06-11 14:46:41 +02:00
|
|
|
elseif retweets ~= "" and favorites ~= "" then
|
2016-10-12 19:26:05 +02:00
|
|
|
footer = '\n<i>'..retweets..' - '..favorites..'</i>'
|
2016-06-11 14:46:41 +02:00
|
|
|
else
|
|
|
|
footer = ""
|
|
|
|
end
|
|
|
|
|
|
|
|
-- replace short URLs
|
|
|
|
if response.entities.urls then
|
|
|
|
for k, v in pairs(response.entities.urls) do
|
|
|
|
local short = v.url
|
|
|
|
local long = v.expanded_url
|
2016-08-01 21:07:27 +02:00
|
|
|
local long = long:gsub('%%', '%%%%')
|
2016-06-11 14:46:41 +02:00
|
|
|
text = text:gsub(short, long)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- remove images
|
|
|
|
local images = {}
|
|
|
|
local videos = {}
|
|
|
|
if response.entities.media and response.extended_entities.media then
|
|
|
|
for k, v in pairs(response.extended_entities.media) do
|
|
|
|
local url = v.url
|
|
|
|
if v.video_info then
|
2016-10-09 22:36:43 +02:00
|
|
|
for i in pairs(v.video_info.variants) do
|
|
|
|
if v.video_info.variants[i].content_type == 'video/mp4' then -- first mp4 is usually the highest res
|
|
|
|
local vid = v.video_info.variants[i].url
|
|
|
|
videos[#videos+1] = vid
|
|
|
|
break;
|
|
|
|
end
|
|
|
|
end
|
2016-09-21 00:33:58 +02:00
|
|
|
else
|
|
|
|
images[#images+1] = v.media_url_https
|
|
|
|
end
|
|
|
|
text = text:gsub(url, '')
|
2016-06-11 14:46:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- quoted tweet
|
|
|
|
if response.quoted_status then
|
2016-09-25 20:42:45 +02:00
|
|
|
local quoted_text = response.quoted_status.full_text
|
2016-06-11 14:46:41 +02:00
|
|
|
local quoted_name = response.quoted_status.user.name
|
|
|
|
local quoted_screen_name = response.quoted_status.user.screen_name
|
|
|
|
if response.quoted_status.user.verified then
|
|
|
|
quoted_verified = ' ✅'
|
|
|
|
else
|
|
|
|
quoted_verified = ''
|
|
|
|
end
|
2016-09-25 20:42:45 +02:00
|
|
|
|
|
|
|
-- replace short URLs for quoted tweets
|
|
|
|
if response.quoted_status.entities.urls then
|
|
|
|
for k, v in pairs(response.quoted_status.entities.urls) do
|
|
|
|
local short = v.url
|
|
|
|
local long = v.expanded_url
|
|
|
|
local long = long:gsub('%%', '%%%%')
|
|
|
|
quoted_text = quoted_text:gsub(short, long)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- same for media
|
|
|
|
if response.quoted_status.entities.media then
|
|
|
|
for k, v in pairs(response.quoted_status.entities.media) do
|
|
|
|
local short = v.url
|
|
|
|
local long = v.media_url_https
|
|
|
|
local long = long:gsub('%%', '%%%%')
|
|
|
|
quoted_text = quoted_text:gsub(short, long)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-04 13:44:56 +02:00
|
|
|
quote = '<b>Als Antwort auf '..quoted_name..'</b> (<a href="https://twitter.com/'..quoted_screen_name..'">@' ..quoted_screen_name..'</a>'..quoted_verified..'):\n'..quoted_text
|
2016-06-11 14:46:41 +02:00
|
|
|
text = text..'\n\n'..quote..'\n'
|
|
|
|
end
|
|
|
|
|
2016-10-12 19:26:05 +02:00
|
|
|
return header.."\n"..utilities.trim(text)..footer..tweet_link, images, videos
|
2016-10-02 18:02:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function twitter:action(msg, config, matches)
|
|
|
|
utilities.send_typing(msg.chat.id, 'typing')
|
|
|
|
if not matches[2] then
|
|
|
|
id = matches[1]
|
|
|
|
else
|
|
|
|
id = matches[2]
|
|
|
|
end
|
|
|
|
|
|
|
|
local twitter_url = "https://api.twitter.com/1.1/statuses/show/" .. id.. ".json"
|
|
|
|
local get_params = {tweet_mode = 'extended'}
|
|
|
|
local response_code, response_headers, response_status_line, response_body = client:PerformRequest("GET", twitter_url, get_params)
|
|
|
|
if response_code ~= 200 then
|
2016-10-03 14:44:30 +02:00
|
|
|
utilities.send_reply(msg, 'Twitter nicht erreichbar, Tweet existiert nicht oder User ist privat.')
|
2016-10-02 18:02:30 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local response = json.decode(response_body)
|
|
|
|
|
|
|
|
local text, images, videos = get_tweet(response)
|
|
|
|
|
2016-06-11 14:46:41 +02:00
|
|
|
-- send the parts
|
2016-10-02 18:02:30 +02:00
|
|
|
utilities.send_reply(msg, text, 'HTML')
|
2016-06-11 14:46:41 +02:00
|
|
|
for k, v in pairs(images) do
|
2016-10-03 21:56:32 +02:00
|
|
|
utilities.send_photo(msg.chat.id, v, nil, msg.message_id)
|
2016-06-11 14:46:41 +02:00
|
|
|
end
|
|
|
|
for k, v in pairs(videos) do
|
2016-10-09 22:36:43 +02:00
|
|
|
local file = download_to_file(v)
|
|
|
|
utilities.send_video(msg.chat.id, file, nil, msg.message_id)
|
2016-06-11 14:46:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-03 14:44:30 +02:00
|
|
|
return twitter
|