defmodule MessengerBot.Service.UserProfile do @moduledoc """ Facebook MessengerBot 'UserProfile' API """ alias MessengerBot.Client alias MessengerBot.Payload.{UserProfileRequest, UserProfileResponse} alias MessengerBot.Config @base_url "https://graph.facebook.com/v2.6" @user_profile %UserProfileResponse{} @doc """ Fetch user profile from facebook api """ @spec fetch(String.t, UserProfileRequest.t) :: UserProfileResponse.t def fetch(page_id, %UserProfileRequest{} = request) do token = Config.page_token(page_id) url = url_for(request, token) case Client.get(url) do {:error, _} -> @user_profile response -> attrs = :jiffy.decode(response.body, [:return_maps]) %UserProfileResponse{ first_name: attrs["first_name"], last_name: attrs["last_name"], profile_pic: attrs["profile_pic"], locale: attrs["locale"], timezone: attrs["timezone"], gender: attrs["gender"], last_ad_referral: attrs["last_ad_referral"] } end end defp url_for(request, token) do "#{@base_url}/#{request.id}?fields=#{request.fields}&access_token=#{token}" end end