defmodule Exsms do use HTTPoison.Base require Logger def send(service, payload) do case check_service(service) do true -> process_response_body(service, payload) false -> {:error, "Service Not Found"} end end def process_response_body(service, body) do config = serviceconfig(service) headers = auth_headers(service, config) pre_payload = build_payload(config, service, body) payload = pre_payload |> Poison.encode!() url = build_url(config, service, pre_payload) request(config[:type], url, payload, headers) end def build_url(config, service, params) do case service do service when service in [:textlocal, :msg91_otp] -> config[:url] <> URI.encode_query(params) _ -> config[:url] end end def build_payload(config, service, payload) do case service do :textlocal -> %{ apikey: config[:api], numbers: payload[:to], message: payload[:message], sender: payload[:sender] } :mailjet -> %{ To: payload[:to], Text: payload[:message], From: payload[:sender] } :sendinblue -> %{ recipient: payload[:to], content: payload[:message], sender: payload[:sender] } :msg91 -> %{ sender: payload[:sender], route: payload[:route], country: payload[:country], sms: [ %{ message: payload[:message], to: [ payload[:to] ] } ] } :msg91_otp -> %{ authkey: config[:api], sender: payload[:sender], mobile: payload[:to], message: payload[:message], otp_length: payload[:length], otp: payload[:otp], otp_expiry: payload[:otp_expiry], email: payload[:email] } end end def auth_headers(service, config) do case service do :sendinblue -> [{"api-key", config[:api]}, {"Content-Type", "application/json"}] :mailjet -> [{"Authorization", "Bearer " <> config[:api]}, {"Content-Type", "application/json"}] :msg91 -> [{"authkey", config[:api]}, {"Content-Type", "application/json"}] _ -> [] end end def serviceconfig(service) do case service do :sendinblue -> %{ url: "https://api.sendinblue.com/v3/transactionalSMS/sms", type: :post, api: Application.get_env(:exsms, :sendinblue)[:api] } :mailjet -> %{ url: "https://api.mailjet.com/v4/sms-send", type: :post, api: Application.get_env(:exsms, :mailjet)[:api] } :textlocal -> %{ url: "https://api.textlocal.in/send/?", type: :post, api: Application.get_env(:exsms, :textlocal)[:api] } :msg91 -> %{ url: "https://api.msg91.com/api/v2/sendsms", type: :post, api: Application.get_env(:exsms, :msg91)[:api] } :msg91_otp -> %{ url: "https://control.msg91.com/api/sendotp.php?", type: :post, api: Application.get_env(:exsms, :msg91)[:api] } _ -> "Service Not Found" end end defp check_service(service) do case service do service when service in [:sendinblue, :mailjet, :textlocal, :msg91, :msg91_otp] -> true _ -> false end end def request(method, url, body, headers) do url = String.to_charlist(url) Logger.debug("exsms - Method - #{inspect(method)}") Logger.debug("exsms - Url - #{inspect(url)}") Logger.debug("exsms - Body - #{inspect(body)}") Logger.debug("exsms - Headers - #{inspect(headers)}") HTTPoison.request(method, url, body, headers) |> normalise_response end defp normalise_response(response) do Logger.debug("exsms - #{inspect(response)}") case response do {:ok, %HTTPoison.Response{body: body}} -> {:ok, body} {:error, %HTTPoison.Error{reason: reason}} -> {:error, reason} end end end