2016-06-15 18:00:59 +02:00
|
|
|
|
local facebook = {}
|
|
|
|
|
|
|
|
|
|
function facebook:init(config)
|
|
|
|
|
if not cred_data.fb_access_token then
|
2016-08-01 21:07:27 +02:00
|
|
|
|
print('Fehlender Key: fb_access_token.')
|
|
|
|
|
print('facebook.lua wird nicht aktiviert.')
|
2016-06-15 18:00:59 +02:00
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
facebook.triggers = {
|
2016-08-15 23:14:28 +02:00
|
|
|
|
"facebook.com/([A-Za-z0-9-._-]+)/(posts)/(%d+)",
|
|
|
|
|
"facebook.com/(permalink).php%?(story_fbid)=(%d+)&id=(%d+)",
|
|
|
|
|
"facebook.com/(photo).php%?fbid=(%d+)",
|
|
|
|
|
"facebook.com/([A-Za-z0-9-._-]+)/(photos)/a.(%d+[%d%.]*)/(%d+)",
|
|
|
|
|
"facebook.com/(video).php%?v=(%d+)",
|
|
|
|
|
"facebook.com/([A-Za-z0-9-._-]+)/(videos)/(%d+[%d%.]*)",
|
|
|
|
|
"facebook.com/([A-Za-z0-9-._-]+)/?$"
|
2016-06-15 18:00:59 +02:00
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local BASE_URL = 'https://graph.facebook.com/v2.5'
|
|
|
|
|
local fb_access_token = cred_data.fb_access_token
|
|
|
|
|
|
|
|
|
|
local makeOurDate = function(dateString)
|
2016-08-15 23:14:28 +02:00
|
|
|
|
local pattern = "(%d+)%/(%d+)%/(%d+)"
|
2016-06-15 18:00:59 +02:00
|
|
|
|
local month, day, year = dateString:match(pattern)
|
|
|
|
|
return day..'.'..month..'.'..year
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function facebook:get_fb_id(name)
|
|
|
|
|
local url = BASE_URL..'/'..name..'?access_token='..fb_access_token..'&locale=de_DE'
|
|
|
|
|
local res,code = https.request(url)
|
|
|
|
|
if code ~= 200 then return nil end
|
|
|
|
|
local data = json.decode(res)
|
|
|
|
|
return data.id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function facebook:fb_post (id, story_id)
|
|
|
|
|
local url = BASE_URL..'/'..id..'_'..story_id..'?access_token='..fb_access_token..'&locale=de_DE&fields=from,name,story,message,link'
|
|
|
|
|
local res,code = https.request(url)
|
|
|
|
|
if code ~= 200 then return nil end
|
|
|
|
|
local data = json.decode(res)
|
|
|
|
|
|
|
|
|
|
local from = data.from.name
|
|
|
|
|
local message = data.message
|
2016-08-15 23:14:28 +02:00
|
|
|
|
if not message then return nil end
|
2016-06-15 18:00:59 +02:00
|
|
|
|
local name = data.name
|
|
|
|
|
if data.link then
|
2016-08-04 13:44:56 +02:00
|
|
|
|
link = '\n<a href="'..data.link..'">'..data.name..'</a>'
|
2016-06-15 18:00:59 +02:00
|
|
|
|
else
|
2016-08-15 23:14:28 +02:00
|
|
|
|
link = ""
|
2016-06-15 18:00:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if data.story then
|
|
|
|
|
story = ' ('..data.story..')'
|
|
|
|
|
else
|
2016-08-15 23:14:28 +02:00
|
|
|
|
story = ""
|
2016-06-15 18:00:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
2016-08-04 13:44:56 +02:00
|
|
|
|
local text = '<b>'..from..'</b>'..story..':\n'..message..'\n'..link
|
2016-06-15 18:00:59 +02:00
|
|
|
|
return text
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function facebook:send_facebook_photo(photo_id, receiver)
|
|
|
|
|
local url = BASE_URL..'/'..photo_id..'?access_token='..fb_access_token..'&locale=de_DE&fields=images,from,name'
|
|
|
|
|
local res,code = https.request(url)
|
|
|
|
|
if code ~= 200 then return nil end
|
|
|
|
|
local data = json.decode(res)
|
|
|
|
|
|
2016-08-04 13:44:56 +02:00
|
|
|
|
local from = '<b>'..data.from.name..'</b>'
|
2017-05-24 21:28:05 +02:00
|
|
|
|
local image_url = data.images[1].source
|
2016-06-15 18:00:59 +02:00
|
|
|
|
if data.name then
|
2017-05-24 21:28:05 +02:00
|
|
|
|
text = from..'<a href="'..image_url..'"> </a>hat ein Bild gepostet:\n'..data.name
|
2016-06-15 18:00:59 +02:00
|
|
|
|
else
|
2017-05-24 21:28:05 +02:00
|
|
|
|
text = from..'<a href="'..image_url..'"> </a>hat ein Bild gepostet:'
|
2016-06-15 18:00:59 +02:00
|
|
|
|
end
|
2017-05-24 21:28:05 +02:00
|
|
|
|
|
|
|
|
|
return text
|
2016-06-15 18:00:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function facebook:send_facebook_video(video_id)
|
|
|
|
|
local url = BASE_URL..'/'..video_id..'?access_token='..fb_access_token..'&locale=de_DE&fields=description,from,source,title'
|
|
|
|
|
local res,code = https.request(url)
|
|
|
|
|
if code ~= 200 then return nil end
|
|
|
|
|
local data = json.decode(res)
|
|
|
|
|
|
2016-08-04 13:44:56 +02:00
|
|
|
|
local from = '<b>'..data.from.name..'</b>'
|
2016-07-22 21:53:41 +02:00
|
|
|
|
local description = data.description or ''
|
2016-06-15 18:00:59 +02:00
|
|
|
|
local source = data.source
|
2016-07-17 13:22:27 +02:00
|
|
|
|
return from..' hat ein Video gepostet:\n'..description, source, data.title
|
2016-06-15 18:00:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function facebook:facebook_info(name)
|
|
|
|
|
local url = BASE_URL..'/'..name..'?access_token='..fb_access_token..'&locale=de_DE&fields=about,name,birthday,category,founded,general_info,is_verified'
|
|
|
|
|
local res,code = https.request(url)
|
|
|
|
|
if code ~= 200 then return nil end
|
|
|
|
|
local data = json.decode(res)
|
|
|
|
|
|
|
|
|
|
local name = data.name
|
|
|
|
|
if data.is_verified then
|
|
|
|
|
name = name..' ✅'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local category = data.category
|
|
|
|
|
|
|
|
|
|
if data.about then
|
|
|
|
|
about = '\n'..data.about
|
|
|
|
|
else
|
2016-08-15 23:14:28 +02:00
|
|
|
|
about = ""
|
2016-06-15 18:00:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if data.general_info then
|
|
|
|
|
general_info = '\n'..data.general_info
|
|
|
|
|
else
|
2016-08-15 23:14:28 +02:00
|
|
|
|
general_info = ""
|
2016-06-15 18:00:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if data.birthday and data.founded then
|
|
|
|
|
birth = '\nGeburtstag: '..makeOurDate(data.birthday)
|
|
|
|
|
elseif data.birthday and not data.founded then
|
|
|
|
|
birth = '\nGeburtstag: '..makeOurDate(data.birthday)
|
|
|
|
|
elseif data.founded and not data.birthday then
|
|
|
|
|
birth = '\nGegründet: '..data.founded
|
|
|
|
|
else
|
2016-08-15 23:14:28 +02:00
|
|
|
|
birth = ""
|
2016-06-15 18:00:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
2016-08-04 13:44:56 +02:00
|
|
|
|
local text = '<b>'..name..'</b> ('..category..')<i>'..about..'</i>'..general_info..birth
|
2016-06-15 18:00:59 +02:00
|
|
|
|
return text
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function facebook:action(msg, config, matches)
|
|
|
|
|
if matches[1] == 'permalink' or matches[2] == 'posts' then
|
|
|
|
|
story_id = matches[3]
|
|
|
|
|
if not matches[4] then
|
|
|
|
|
id = facebook:get_fb_id(matches[1])
|
|
|
|
|
else
|
|
|
|
|
id = matches[4]
|
|
|
|
|
end
|
2016-08-24 15:38:29 +02:00
|
|
|
|
utilities.send_reply(msg, facebook:fb_post(id, story_id), 'HTML')
|
2016-06-15 18:00:59 +02:00
|
|
|
|
return
|
|
|
|
|
elseif matches[1] == 'photo' or matches[2] == 'photos' then
|
|
|
|
|
if not matches[4] then
|
|
|
|
|
photo_id = matches[2]
|
|
|
|
|
else
|
|
|
|
|
photo_id = matches[4]
|
|
|
|
|
end
|
2017-05-24 21:28:05 +02:00
|
|
|
|
local text = facebook:send_facebook_photo(photo_id, receiver)
|
|
|
|
|
if not text then return end
|
2016-08-24 15:38:29 +02:00
|
|
|
|
utilities.send_typing(msg.chat.id, 'upload_photo')
|
2017-05-24 21:28:05 +02:00
|
|
|
|
utilities.send_message(msg.chat.id, text, false, msg.message_id, 'HTML')
|
2016-06-15 18:00:59 +02:00
|
|
|
|
return
|
|
|
|
|
elseif matches[1] == 'video' or matches[2] == 'videos' then
|
|
|
|
|
if not matches[3] then
|
|
|
|
|
video_id = matches[2]
|
|
|
|
|
else
|
|
|
|
|
video_id = matches[3]
|
|
|
|
|
end
|
2016-07-17 13:22:27 +02:00
|
|
|
|
local output, video_url, title = facebook:send_facebook_video(video_id)
|
|
|
|
|
if not title then
|
|
|
|
|
title = 'Video aufrufen'
|
|
|
|
|
else
|
|
|
|
|
title = 'VIDEO: '..title
|
|
|
|
|
end
|
|
|
|
|
if not video_url then return end
|
2016-08-24 15:38:29 +02:00
|
|
|
|
utilities.send_reply(msg, output, 'HTML', '{"inline_keyboard":[[{"text":"'..utilities.md_escape(title)..'","url":"'..video_url..'"}]]}')
|
2016-06-15 18:00:59 +02:00
|
|
|
|
return
|
|
|
|
|
else
|
2016-08-24 15:38:29 +02:00
|
|
|
|
utilities.send_reply(msg, facebook:facebook_info(matches[1]), 'HTML')
|
2016-06-15 18:00:59 +02:00
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return facebook
|