ex_line_bot_sdk v0.0.5 ExLineBotSdk.LineClient behaviour View Source

ExLineBotSdk.LineClient provides client’s functionalities to connect Line messaging api endpoint.

To setup a line’s client library in your project add use ExLineBotSdk.LineClient and set module attribute channel_access_token in your module like the code example shown below.

defmodule MyApp.LineClient do
  @channel_access_token "[Your Line Access Token Here]"

  use ExLineBotSdk.Client
end

Usages

Verifying Line Requests

In order to use this library to verify the line requests that came in. Line requests must passed through plug PlugSetRequestRawData so that the raw request is saved in conn.private[:raw_request_data] since we need raw Line’s raw request data to verify that they are actually came from Line.

To see how we can configure PlugSetRequestRawData checkout it usage documentation in https://hexdocs.pm/plug_set_request_raw_data/PlugSetRequestRawData.html#module-usage-instruction

Below code show an example of how to configure PlugSetRequestRawData to set conn.private[:raw_request_data] when there is x-line-signature in request headers in an phoenix app.

defmodule MyAppWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :my_app

  alias MyAppWeb.SetRawData

  ...

  plug(PlugSetRequestRawData, %{check: %{check: &SetRawData.check/1}) # Make sure that it is before Plug.Parsers plug

  plug(
    Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Poison
  )
  ...
end

defmodule MyAppWeb.SetRawData do
  alias Plug.Conn

  def check(%Conn{} = conn) do
    conn.req_headers
    |> Enum.any?(fn {header, _content} -> header == "x-line-signature" end)
  end
end

Once PlugSetRequestRawData is properly configured like above code sample you can use ExLineBotSdk.LineClient.verify/1 to check the validity of Line webhook requests.

Link to this section Summary

Callbacks

Verify if a Line’s webhook post request is actually coming from Line

Link to this section Callbacks

Link to this callback channel_access_token() View Source
channel_access_token() :: String.t()
Link to this callback verify(%) View Source
verify(%Plug.Conn{
  adapter: term(),
  assigns: term(),
  before_send: term(),
  body_params: term(),
  cookies: term(),
  halted: term(),
  host: term(),
  method: term(),
  owner: term(),
  params: term(),
  path_info: term(),
  path_params: term(),
  port: term(),
  private: term(),
  query_params: term(),
  query_string: term(),
  remote_ip: term(),
  req_cookies: term(),
  req_headers: term(),
  request_path: term(),
  resp_body: term(),
  resp_cookies: term(),
  resp_headers: term(),
  scheme: term(),
  script_name: term(),
  secret_key_base: term(),
  state: term(),
  status: term()
}) ::
  {:ok,
   %Plug.Conn{
     adapter: term(),
     assigns: term(),
     before_send: term(),
     body_params: term(),
     cookies: term(),
     halted: term(),
     host: term(),
     method: term(),
     owner: term(),
     params: term(),
     path_info: term(),
     path_params: term(),
     port: term(),
     private: term(),
     query_params: term(),
     query_string: term(),
     remote_ip: term(),
     req_cookies: term(),
     req_headers: term(),
     request_path: term(),
     resp_body: term(),
     resp_cookies: term(),
     resp_headers: term(),
     scheme: term(),
     script_name: term(),
     secret_key_base: term(),
     state: term(),
     status: term()
   }}
  | {:error, String.t()}

Verify if a Line’s webhook post request is actually coming from Line.

Example

  ExLineBotSdk.LineClient.veriy(conn)
  {:ok, conn} When Line request is verified else return {:error, "Line Request is not verified."}