module Web.FBMessenger.API.Bot.Responses
(
MessageResponse (..)
, SendErrorCode (..)
, SendErrorObject (..)
, SubscriptionResponse (..)
, UserProfileResponse (..)
, WelcomeMessageResponse (..)
, errorInfo
, extractSendError
) where
import Data.Aeson
import Data.Text (Text)
import GHC.Generics
import Servant.Client (ServantError(..))
import Web.FBMessenger.API.Bot.JsonExt
data SubscriptionResponse = SubscriptionResponse{ subscription_success :: Bool } deriving (Eq, Show, Generic)
instance ToJSON SubscriptionResponse where
toJSON = toJsonDrop 13
instance FromJSON SubscriptionResponse where
parseJSON = parseJsonDrop 13
data MessageResponse = MessageResponse
{ message_response_recipient_id :: Text
, message_response_message_id :: Text
} deriving (Eq, Show, Generic)
instance ToJSON MessageResponse where
toJSON = toJsonDrop 17
instance FromJSON MessageResponse where
parseJSON = parseJsonDrop 17
data UserProfileResponse = UserProfileResponse
{ usr_first_name :: Text
, usr_last_name :: Text
, usr_profile_pic :: Text
, usr_locale :: Text
, usr_timezone :: Int
, usr_gender :: Text
} deriving (Eq, Show, Generic)
instance ToJSON UserProfileResponse where
toJSON = toJsonDrop 4
instance FromJSON UserProfileResponse where
parseJSON = parseJsonDrop 4
data WelcomeMessageResponse = WelcomeMessageResponse { wmr_result :: Text } deriving (Eq, Show, Generic)
instance ToJSON WelcomeMessageResponse where
toJSON = toJsonDrop 4
instance FromJSON WelcomeMessageResponse where
parseJSON = parseJsonDrop 4
data SendErrorObject = SendErrorObject { eoMessage :: Text, eoType :: Text, eoCode :: SendErrorCode, eoErrorData :: Text, eoFbtraceId :: Text } deriving (Eq, Show, Generic)
instance ToJSON SendErrorObject where
toJSON SendErrorObject{..} = object [ "error" .= e ]
where e = object [ "message" .= eoMessage
, "type" .= eoType
, "code" .= eoCode
, "error_data" .= eoErrorData
, "fbtrace_id" .= eoFbtraceId ]
instance FromJSON SendErrorObject where
parseJSON = withObject "send error" $ \o ->
let e = o .: "error"
e' field = e >>= (.: field)
in SendErrorObject <$>
e' "message" <*>
e' "type" <*>
e' "code" <*>
e' "error_data" <*>
e' "fbtrace_id"
data SendErrorCode = InternalServerError | UnauthorizedApplication | NoMatchingUserFound | RateLimitError deriving (Eq, Show)
instance ToJSON SendErrorCode where
toJSON InternalServerError = Number 2
toJSON UnauthorizedApplication = Number 10
toJSON NoMatchingUserFound = Number 100
toJSON RateLimitError = Number 613
instance FromJSON SendErrorCode where
parseJSON (Number 2) = pure InternalServerError
parseJSON (Number 10) = pure UnauthorizedApplication
parseJSON (Number 100) = pure NoMatchingUserFound
parseJSON (Number 613) = pure RateLimitError
parseJSON _ = fail "Unable to parse SendErrorCode"
errorInfo :: SendErrorObject -> (SendErrorCode, Text)
errorInfo err = (ecode, edata)
where edata = eoErrorData err
ecode = eoCode err
extractSendError :: ServantError -> Maybe SendErrorObject
extractSendError FailureResponse{ responseStatus = _, responseContentType = _
, responseBody = body
} = do
eo <- decode body :: Maybe SendErrorObject
return eo
extractSendError _ = Nothing