Skip to content

Instantly share code, notes, and snippets.

@MeerKatDev
Created November 24, 2019 12:19
Show Gist options
  • Save MeerKatDev/d5a154cec21e0b9284284001ed81b4b0 to your computer and use it in GitHub Desktop.
Save MeerKatDev/d5a154cec21e0b9284284001ed81b4b0 to your computer and use it in GitHub Desktop.
Elixir - send SMS with Twilio through Hackney
text = "sms text"
opts = [follow_redirect: true, basic_auth: {@account_sid, @auth_token}]
base_url = "https://api.twilio.com/2010-04-01/Accounts/#{@account_sid}/Messages.json"
payload = "Body=#{text}&From=#{@number_from}&To=#{number_to}"
headers = ["Content-Type": "application/x-www-form-urlencoded"]
case :hackney.request(:post, base_url, headers, payload, opts) do
{:ok, 201, _hs, _ref} ->
json(conn, %{message: "Message sent successfully!"})
{:error, reason} ->
send_resp(conn, 500, Jason.encode!(%{message: reason}))
{:ok, code, _hs, ref} ->
{:ok, body} = :hackney.body(ref)
send_resp(conn, code, body)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment