From 7dec8714738365f5da654bad7336cefb10fc6f54 Mon Sep 17 00:00:00 2001 From: Albert Le Batteux Date: Thu, 15 Nov 2018 13:56:11 +0100 Subject: [PATCH] feat: EnableChatExtension --- messenger.go | 34 ++++++++++++++++++++++++++++++++++ settings.go | 9 +++++++++ 2 files changed, 43 insertions(+) diff --git a/messenger.go b/messenger.go index 61a0b04..112e4e4 100644 --- a/messenger.go +++ b/messenger.go @@ -20,6 +20,11 @@ const ( ProfileFields = "first_name,last_name,profile_pic,locale,timezone,gender" // SendSettingsURL is API endpoint for saving settings. SendSettingsURL = "https://graph.facebook.com/v2.6/me/thread_settings" + + // MessengerProfileURL is the API endoint where yout bot set properties that define various aspects of the following Messenger Platform features. + // Used in the form https://graph.facebook.com/v2.6/me/messenger_profile?access_token= + // https://developers.facebook.com/docs/messenger-platform/reference/messenger-profile-api/ + MessengerProfileURL = "https://graph.facebook.com/v2.6/me/messenger_profile" ) // Options are the settings used when creating a Messenger client. @@ -435,6 +440,35 @@ func (m *Messenger) Attachment(to Recipient, dataType AttachmentType, url string return response.Attachment(dataType, url, messagingType, tags...) } +// EnableChatExtension set the homepage url required for a chat extension. +func (m *Messenger) EnableChatExtension(homeURL HomeURL) error { + wrap := map[string]interface{}{ + "home_url": homeURL, + } + data, err := json.Marshal(wrap) + if err != nil { + return err + } + + req, err := http.NewRequest("POST", MessengerProfileURL, bytes.NewBuffer(data)) + if err != nil { + return err + } + + req.Header.Set("Content-Type", "application/json") + req.URL.RawQuery = "access_token=" + m.token + + client := &http.Client{} + + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + return checkFacebookError(resp.Body) +} + // classify determines what type of message a webhook event is. func (m *Messenger) classify(info MessageInfo, e Entry) Action { if info.Message != nil { diff --git a/settings.go b/settings.go index 97cd583..43ac9c5 100644 --- a/settings.go +++ b/settings.go @@ -44,3 +44,12 @@ type CallToActionsItem struct { WebviewHeightRatio string `json:"webview_height_ratio,omitempty"` MessengerExtension bool `json:"messenger_extensions,omitempty"` } + +// HomeURL is the settings for EnableChatExtension +// https://developers.facebook.com/docs/messenger-platform/reference/messenger-profile-api/home-url +type HomeURL struct { + URL string `json:"url,omitempty"` + WebviewHeightRatio string `json:"webview_height_ratio,omitempty"` + WebviewShareButton string `json:"webview_share_button,omitempty"` + InTest bool `json:"in_test,omitempty"` +}