     |> put_status(200)
     |> text("ok")
   end
+
+  defp podcast_json(conn, podcast) do
+    %{
+      title: podcast.title,
+      image_url: podcast.image_url,
+      subtitle: podcast.description,
+      default_action: %{
+        type: "web_url",
+        url: "https://20ba4076.ngrok.io" <> podcast_frontend_path(conn, :show, podcast),
+        messenger_extensions: true,
+        webview_height_ratio: "tall",
+        fallback_url: "https://20ba4076.ngrok.io" <> podcast_frontend_path(conn, :show, podcast)
+      },
+      buttons: [
+        %{
+          type: "web_url",
+          url: "https://20ba4076.ngrok.io" <> podcast_frontend_path(conn, :show, podcast),
+          title: "In Panoptikum"
+        },
+        %{
+          type: "web_url",
+          url: "https://20ba4076.ngrok.io" <> podcast_frontend_path(conn, :show, podcast),
+          title: "View Website"
+        }
+      ]
+    }
+  end
 end


  def message(conn, %{"entry" => [%{"messaging" => [%{"message" => %{"text" => message}, "sender" => %{"id" => sender_id}}]}]}) do
    sqlfrag = "%" <> message <> "%"
    podcast_ids = from(p in Pan.Podcast, where: ilike(p.title, ^sqlfrag),
                                      select: p.id)
                |> Repo.all()

    users_also_liking = from(l in Pan.Like, where: l.podcast_id in ^podcast_ids and
                                               is_nil(l.chapter_id) and
                                               is_nil(l.episode_id),
                                        select: l.enjoyer_id)
                        |> Repo.all()
                        |> Enum.uniq
    also_liked = from(l in Pan.Like, join: p in assoc(l, :podcast),
                                 where: l.enjoyer_id in ^users_also_liking and
                                        is_nil(l.chapter_id) and
                                        is_nil(l.episode_id) and
                                        not(l.podcast_id in ^podcast_ids),
                                 group_by: p.id,
                                 select: [count(l.podcast_id), p.id, p.title],
                                 order_by: [desc: count(l.podcast_id)],
                                 limit: 10)
                 |> Repo.all()
    reply = case also_liked do
      [[_, _, title] | _] ->
        title
      [] ->
        "No matches found"
    end

    data = %{
      recipient: %{
        id: sender_id
      },
      message: %{
        text: reply
      }
    }
    |> Poison.encode!

      access_token: Application.get_env(:pan, :bot)[:fb_access_token]
    }
    |> URI.encode_query()

    body = %{
      setting_type: "domain_whitelisting",
      whitelisted_domains: ["https://20ba4076.ngrok.io"],
      domain_action_type: "add"
    }
    |> Poison.encode!
    "https://graph.facebook.com/v2.6/me/thread_settings?#{params}"
    |> HTTPotion.post([body: body,  headers: ["Content-Type": "application/json"]])

    "https://graph.facebook.com/v2.6/me/messages?#{params}"
    |> HTTPotion.post([body: data,  headers: ["Content-Type": "application/json"]])
