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 @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(Facebook.Graph, []) ] supervise(children, strategy: :one_for_one) end @type fields :: list @type access_token :: String.t @type response :: {:json, HashDict.t} | {:body, String.t} @type using_appsecret :: boolean @type reaction :: :reaction @doc """ If you want to use an appsecret proof, pass it into set_appsecret: ## Example iex> Facebook.setAppsecret("appsecret") See: https://developers.facebook.com/docs/graph-api/securing-requests """ def setAppsecret(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", "") See: https://developers.facebook.com/docs/graph-api/reference/user/ """ @spec me(fields :: String.t, access_token) :: response 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"], "") See: https://developers.facebook.com/docs/graph-api/reference/user/ """ @spec me(fields, access_token) :: response def me(fields, access_token) do fields = fields |> add_app_secret(access_token) Facebook.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: "") iex> # publish a link and message iex> Facebook.publish(:feed, "", [message: "") """ @spec publish(:feed, feed_id :: String.t, fields, access_token) :: response def publish(:feed, feed_id, fields, access_token) do params = fields ++ [access_token: access_token] Facebook.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, "", "", [], "") {:json, %{"id" => "..."}} iex> Facebook.publish(:video, "", "