Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building a Slack app with Phoenix

Wu Qing
January 20, 2022

Building a Slack app with Phoenix

Presented at Elixir Australia December 2021 Meetup
https://www.meetup.com/elixir-sydney/events/gztkjsyccqbtb/

Recording video available at https://www.youtube.com/watch?v=O_61JddRvq0

Wu Qing

January 20, 2022
Tweet

More Decks by Wu Qing

Other Decks in Programming

Transcript

  1. Sample request from Slack %{ "api_app_id" => "A02DMAGNJ21", "channel_id" =>

    "C02FFNUTGBS", "channel_name" => "priv-test-video", "command" => "/meet", "is_enterprise_install" => "false", "response_url" => "https://hooks.slack.com/commands/T033NA***/2763195051441/9Euw22LTMOHkjqK8cfgE4wVI", "team_domain" => "telnyx", "team_id" => "T033NA***", "text" => "", "token" => "RSVU4xGkEEwBYIZrM9GJGTcN", "trigger_id" => "2752826282196.3124344411.690a4adfbdb8bf7f4a86dba3529445c0", "user_id" => "U02DKE08LUA", "user_name" => "qingwu" }
  2. Sample request from Slack %{ "api_app_id" => "A02DMAGNJ21", "channel_id" =>

    "C02FFNUTGBS", "channel_name" => "priv-test-video", "command" => "/meet", "is_enterprise_install" => "false", "response_url" => "https://hooks.slack.com/commands/T033NA***/2763195051441/9Euw22LTMOHkjqK8cfgE4wVI", "team_domain" => "telnyx", "team_id" => "T033NA***", "text" => "", "token" => "RSVU4xGkEEwBYIZrM9GJGTcN", "trigger_id" => "2752826282196.3124344411.690a4adfbdb8bf7f4a86dba3529445c0", "user_id" => "U02DKE08LUA", "user_name" => "qingwu" }
  3. Handle participants join/leave Iteration 1: periodical task • get participants

    in room from Video API • get participants in Slack call
  4. Handle participants join/leave Iteration 1: periodical task • get participants

    in room from Video API • get participants in Slack call • compare and update Slack call
  5. Handle participants join/leave Iteration 2: improved periodical task • get

    participants in room from Video API • keep track of participants in GenServer
  6. Handle participants join/leave Iteration 2: improved periodical task • get

    participants in room from Video API • keep track of participants in GenServer • compare and update both GenServer and Slack call
  7. Handle participants join/leave Iteration 2: improved periodical task • get

    participants in room from Video API • keep track of participants in GenServer • compare and update both GenServer and Slack call
  8. Finish calls What • delete room in Video API •

    end call in Slack • remove room/call in GenServer state
  9. Finish calls When • no one in the call (after

    someone joined) • Idle for 24 hours
  10. Get Request body with Phoenix defmodule CacheBodyReader do def read_body(conn,

    opts) do {:ok, body, conn} = Plug.Conn.read_body(conn, opts) conn = put_in(conn.assigns[:raw_body], body) {:ok, body, conn} end end
  11. Get Request body with Phoenix # endpoint.ex plug Plug.Parsers, parsers:

    [:urlencoded, :json], pass: ["text/*"], body_reader: {CacheBodyReader, :read_body, []}, json_decoder: Phoenix.json_library()
  12. Compute Signature basestring = "v0:#{timestamp}:#{raw_body}" hash = :hmac |> :crypto.mac(:sha256,

    signing_secret, basestring) |> Base.encode16(case: :lower) signature = "v1=" <> hash
  13. Compute Signature basestring = "v0:#{timestamp}:#{raw_body}" hash = :hmac |> :crypto.mac(:sha256,

    signing_secret, basestring) |> Base.encode16(case: :lower) signature = "v1=" <> hash
  14. Compute Signature basestring = "v0:#{timestamp}:#{raw_body}" hash = :hmac |> :crypto.mac(:sha256,

    signing_secret, basestring) |> Base.encode16(case: :lower) signature = "v0=" <> hash
  15. References • Slack Slash Commands • Slack Block kit •

    Slack Calls API • Telnyx Video API documentation