Skip to content

Commit

Permalink
Merge 35240f1 into 81bae32
Browse files Browse the repository at this point in the history
  • Loading branch information
FritzFlorian committed Apr 7, 2017
2 parents 81bae32 + 35240f1 commit b8eefd1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 12 deletions.
47 changes: 41 additions & 6 deletions lib/message_sender.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,68 @@ defmodule FacebookMessenger.Sender do
def send(recepient, message) do
res = manager.post(
url: url,
body: json_payload(recepient, message)
body: text_payload(recepient, message) |> to_json
)
Logger.info("response from FB #{inspect(res)}")
res
end

@doc """
sends an image message to the recipient
* :recepient - the recepient to send the message to
* :image_url - the url of the image to be sent
"""
@spec send_image(String.t, String.t) :: HTTPotion.Response.t
def send_image(recepient, image_url) do
res = manager.post(
url: url,
body: image_payload(recepient, image_url) |> to_json
)
Logger.info("response fro FB #{inspect(res)}")
res
end

@doc """
creates a payload to send to facebook
* :recepient - the recepient to send the message to
* :message - the message to send
"""
def payload(recepient, message) do
def text_payload(recepient, message) do
%{
recipient: %{id: recepient},
message: %{text: message}
}
end

@doc """
creates a json payload to send to facebook
creates a payload for an image message to send to facebook
* :recepient - the recepient to send the message to
* :message - the message to send
* :image_url - the url of the image to be sent
"""
def image_payload(recepient, image_url) do
%{
recipient: %{id: recepient},
message: %{
attachment: %{
type: "image",
payload: %{
url: image_url
}
}
}
}
end

@doc """
converts a map to json using poison
* :map - the map to be converted to json
"""
def json_payload(recepient, message) do
payload(recepient, message)
def to_json(map) do
map
|> Poison.encode
|> elem(1)
end
Expand Down
27 changes: 21 additions & 6 deletions test/message_sender_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,33 @@ defmodule TestBotOne.MessageSenderTest do
assert FacebookMessenger.Sender.url == "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_TOKEN"
end

test "creates a correct payload" do
assert FacebookMessenger.Sender.payload(1055439761215256, "Hello") ==
test "creates a correct image payload" do
assert FacebookMessenger.Sender.image_payload(1055439761215256, "sample.com/some_image.png") ==
%{
recipient: %{id: 1055439761215256},
message: %{
attachment: %{type: "image", payload: %{url: "sample.com/some_image.png"}}
}
}
end

test "creates a correct text payload" do
assert FacebookMessenger.Sender.text_payload(1055439761215256, "Hello") ==
%{message: %{text: "Hello"}, recipient: %{id: 1055439761215256}}
end

test "creates a correct payload in json" do
assert FacebookMessenger.Sender.json_payload(1055439761215256, "Hello") ==
"{\"recipient\":{\"id\":1055439761215256},\"message\":{\"text\":\"Hello\"}}"
test "converts hash to json"do
assert FacebookMessenger.Sender.to_json(%{test_key: "test_value"}) ==
"{\"test_key\":\"test_value\"}"
end

test "sends correct message" do
test "sends correct text message" do
FacebookMessenger.Sender.send(1055439761215256, "Hello")
assert_received %{body: "{\"recipient\":{\"id\":1055439761215256},\"message\":{\"text\":\"Hello\"}}", url: "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_TOKEN"}
end

test "sends correct image message" do
FacebookMessenger.Sender.send_image(1055439761215256, "sample.com/some_image.png")
assert_received %{body: "{\"recipient\":{\"id\":1055439761215256},\"message\":{\"attachment\":{\"type\":\"image\",\"payload\":{\"url\":\"sample.com/some_image.png\"}}}}", url: "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_TOKEN"}
end
end

0 comments on commit b8eefd1

Please sign in to comment.