Powered by AppSignal & Oban Pro

What happened this week

what_happened_this_week.livemd

What happened this week

Mix.install([
  :slack,
  :kino
])

Program

slack_access_token = ''
defmodule SlackHelper.HTML do
  use Kino.JS

  def new(html) do
    Kino.JS.new(__MODULE__, html)
  end

  def render_attachment(message) do
    case message["attachments"] do
      [first] ->
        "<a href=\"#{first["original_url"]}\">#{first["fallback"]}</a>"
    end
  end

  def render_message(message) do
    """
    <li>
      <div>
        <span>#{cleanup_text(message["text"])}</span>
      </div>
      <div>
        #{render_attachment(message)}
      </div>
    </li>
    """
  end

  def cleanup_text(message) do
    String.replace(message, "<#C144UA49G|podcast>", "")
  end

  def wrap(inner) do
    """
    <ul>
      #{inner}
    </ul>
    """
  end

  asset "main.js" do
    """
    export function init(ctx, html) {
      ctx.importCSS("main.css");

      ctx.root.innerHTML = html;
    }
    """
  end

  asset "main.css" do
    """
    ul {
      list-style-type: none;
    }
    li {
      margin: 10px;
    }
    a:link:not(#exclude), a:visited {
      text-decoration: none;
      color: blue;
      cursor: pointer;
    }
    """
  end
end
[podcast_channel = %{id: channel_id}] =
  Slack.Web.Conversations.list(%{token: slack_access_token})
  |> Map.get("channels")
  |> Enum.map(fn channel -> %{name: channel["name"], id: channel["id"]} end)
  |> Enum.filter(fn channel -> Regex.match?(~r/development/, channel.name) end)
podcast_hashtag_regex = ~r/<#C144UA49G|podcast>/

Slack.Web.Conversations.history(channel_id, %{token: slack_access_token})
|> Map.get("messages")
|> Enum.filter(fn message -> Regex.match?(podcast_hashtag_regex, message["text"]) end)
|> Enum.map(&SlackHelper.HTML.render_message/1)
|> Enum.join()
|> SlackHelper.HTML.wrap()
|> SlackHelper.HTML.new()