-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
-- DO NOT USE WITHOUT PERMISSION

do

local BASE_URL = 'https://graph.facebook.com'

function get_fbphoto_data (fbphoto_code)
  local access_token = cred_data.fb_access_token
  local url = BASE_URL..'/'..fbphoto_code..'?access_token='..access_token..'&locale=de_DE'
  local res,code  = https.request(url)
  if code ~= 200 then return "HTTP-FEHLER" end
  local data = json:decode(res)
  return data
end

function send_fbphoto_data(data, receiver)
  local from = data.from.name
  local name = data.name
  if name then
    text = from..' hat ein Bild gepostet:\n'..name
  else
    text = from..' hat ein Bild gepostet'
  end
  local image_url = data.source
  local cb_extra = {
    receiver=receiver,
    url=image_url
  }
  send_msg(receiver, text, send_photo_from_url_callback, cb_extra)
end

function run(msg, matches)
  local fbphoto_code = matches[3]
  local data = get_fbphoto_data(fbphoto_code)
  local receiver = get_receiver(msg)
  send_fbphoto_data(data, receiver)
end

return {
  description = "Sendet Facebook-Bilder-Post", 
  usage = "URL zu öffentlichem Facebook-Bild",
  patterns = {"facebook.com/([A-Za-z0-9-._-]+)/photos/([A-Za-z0-9-._-]+)/([A-Za-z0-9-._-]+)"},
  run = run 
}
end