Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added functionality for FB messenger API #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/facebook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ defmodule Facebook do
Facebook.Graph.post(:video, "/#{page_id}/videos", payload, params, [])
end

@spec send_message(payload :: String.t, access_token) :: response
def send_message(payload, access_token) do
params = [access_token: access_token]
Facebook.Graph.post :message, "me/messages", payload, params, []
end

defp media_payload(file_path) do
{:multipart, [{:file, file_path, {"form-data", [filename: Path.basename(file_path)]}, []}]}
end
Expand Down
31 changes: 20 additions & 11 deletions lib/facebook/graph.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule Facebook.Graph do
@type path :: String.t
@type response :: {:json, HashDict.t} | {:body, String.t}
@type options :: list
@type headers :: list
@type params :: list
@type method :: :get | :post | :put | :head
@type url :: String.t
Expand Down Expand Up @@ -58,20 +59,29 @@ defmodule Facebook.Graph do
end

@doc """
HTTP POST using path, body, params and options
HTTP POST for video api using path, body, params and options
"""
@spec post(path, payload, params, options) :: response
def post(path, payload, params, options) do
url = :hackney_url.make_url(Config.graph_url, path, params)
@spec post(:video, path, payload, params, options) :: response
def post(:video, path, payload, params, options) do
url = :hackney_url.make_url(Config.graph_video_url, path, params)
request(:post, url, payload, options)
end

@doc """
HTTP POST for video api using path, body, params and options
HTTP POST for messenger api using path, body, params and options
"""
@spec post(:video, path, payload, params, options) :: response
def post(:video, path, payload, params, options) do
url = :hackney_url.make_url(Config.graph_video_url, path, params)
def post(:message, path, payload, params, options) do
headers = ["Content-type": "application/json"]
url = :hackney_url.make_url(Config.graph_url, path, params)
request(:post, url, payload, options, headers)
end

@doc """
HTTP POST using path, body, params and options
"""
@spec post(path, payload, params, options) :: response
def post(path, payload, params, options) do
url = :hackney_url.make_url(Config.graph_url, path, params)
request(:post, url, payload, options)
end

Expand All @@ -86,9 +96,8 @@ defmodule Facebook.Graph do
@doc """
HTTP generic request (GET, POST, etc) using a full URL, payload and options
"""
@spec request(method, url, payload, options) :: response
def request(method, url, payload, options) do
headers = []
@spec request(method, url, payload, options, headers) :: response
def request(method, url, payload, options, headers \\ []) do
Logger.debug fn ->
"[#{method}] #{url} #{inspect headers} #{inspect payload}"
end
Expand Down