defmodule Facebook do use Application use Supervisor @moduledoc """ Provides API wrappers for the Facebook Graph API See: https://developers.facebook.com/docs/graph-api """ alias Facebook.Config alias Facebook.Graph @doc "Start hook" def start(_type, _args) do start_link([]) end @doc "Supervisor start" def start_link(_) do Supervisor.start_link(__MODULE__, []) end def init(_) do children = [ worker(Graph, []) ] supervise(children, strategy: :one_for_one) end @type using_appsecret :: boolean @type react_type :: atom @type scope :: atom | String.t @type reaction :: :reaction @type object_id :: String.t @type page_id :: String.t @type file_path :: String.t @type access_token :: String.t @type limit :: number @type fields :: list @type resp :: {:ok, Map.t} | {:error, Map.t} @type num_resp :: {:ok, number} | {:error, Map.t} @doc """ If you want to use an appsecret proof, pass it into set_appsecret: ## Example iex> Facebook.set_appsecret("appsecret") See: https://developers.facebook.com/docs/graph-api/securing-requests """ def set_appsecret(appsecret) do Config.appsecret(appsecret) end @doc """ Basic user infos of the logged in user (specified by the access_token). ## Example iex> Facebook.me("id,first_name", "") {:ok, %{"first_name" => "...", "id" => "..."}} See: https://developers.facebook.com/docs/graph-api/reference/user/ """ @spec me(fields :: String.t, access_token) :: resp def me(fields, access_token) when is_binary(fields) do me([fields: fields], access_token) end @doc """ Basic user infos of the logged in user (specified by the access_token). ## Example iex> Facebook.me([fields: "id,first_name"], "") {:ok, %{"first_name" => "...", "id" => "..."}} See: https://developers.facebook.com/docs/graph-api/reference/user/ """ @spec me(fields, access_token) :: resp def me(fields, access_token) do fields = fields |> add_app_secret(access_token) Graph.get("/me", fields ++ [access_token: access_token]) end @doc """ Publish to a feed. Author (user or page) is determined from the supplied token. The `feed_id` is the id for the user or page feed to publish to. Apps need both `manage_pages` and `publish_pages` to be able to publish as a Page. The `publish_actions` permission is required to publish as an individual. See Facebook's publishing documentation for more info: * https://developers.facebook.com/docs/pages/publishing * https://developers.facebook.com/docs/pages/publishing#personal_post * https://developers.facebook.com/docs/facebook-login/permissions#reference-publish_pages ## Examples iex> # publish a message iex> Facebook.publish(:feed, "", [message: "") {:ok, %{"id" => "{page_id}_{post_id}"}} iex> # publish a link and message iex> Facebook.publish(:feed, "", [message: "") {:ok, %{"id" => "{page_id}_{post_id}"}} """ @spec publish(:feed, feed_id :: String.t, fields, access_token) :: resp def publish(:feed, feed_id, fields, access_token) do params = fields ++ [access_token: access_token] Graph.post("/#{feed_id}/feed", params, []) end @doc """ Publish media to a feed. Author (user or page) is determined from the supplied token. The `feed_id` is the id for the user or page feed to publish to. Same :feed publishing permissions apply. ## Example iex> Facebook.publish(:photo, "", "", [], "") {:ok, %{"id" => photo_id, "post_id" => "{page_id}_{post_id}"} iex> Facebook.publish(:video, "", "