1
0
mirror of synced 2024-11-22 04:46:05 +03:00

Merge pull request #47 from albttx/enable-chat-extension

feat: EnableChatExtension
This commit is contained in:
Harrison Shoebridge 2018-11-23 13:51:17 +11:00 committed by GitHub
commit 8a57afcd5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,66 @@
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/paked/messenger"
)
var (
serverURL = flag.String("serverURL", "", "The server (webview) URL, must be https (required)")
verifyToken = flag.String("verify-token", "mad-skrilla", "The token used to verify facebook (required)")
verify = flag.Bool("should-verify", false, "Whether or not the app should verify itself")
pageToken = flag.String("page-token", "not skrilla", "The token that is used to verify the page on facebook")
appSecret = flag.String("app-secret", "", "The app secret from the facebook developer portal (required)")
host = flag.String("host", "localhost", "The host used to serve the messenger bot")
port = flag.Int("port", 8080, "The port used to serve the messenger bot")
)
func main() {
flag.Parse()
if *verifyToken == "" || *appSecret == "" || *pageToken == "" {
fmt.Printf("missing arguments\n\n")
flag.Usage()
os.Exit(-1)
}
client := messenger.New(messenger.Options{
Verify: *verify,
AppSecret: *appSecret,
VerifyToken: *verifyToken,
Token: *pageToken,
})
err := client.EnableChatExtension(messenger.HomeURL{
URL: *serverURL,
WebviewHeightRatio: "tall",
WebviewShareButton: "show",
InTest: true,
})
if err != nil {
fmt.Println("Failed to EnableChatExtension, err=", err)
}
// Setup a handler to be triggered when a message is received
client.HandleMessage(func(m messenger.Message, r *messenger.Response) {
fmt.Printf("%v (Sent, %v)\n", m.Text, m.Time.Format(time.UnixDate))
p, err := client.ProfileByID(m.Sender.ID)
if err != nil {
fmt.Println("Something went wrong!", err)
}
r.Text(fmt.Sprintf("Hello, %v!", p.FirstName), messenger.ResponseType)
})
addr := fmt.Sprintf("%s:%d", *host, *port)
log.Println("Serving messenger bot on", addr)
log.Fatal(http.ListenAndServe(addr, client.Handler()))
}

View File

@ -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=<PAGE_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 {

View File

@ -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"`
}