Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Button Templates #31

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Messenger Platform Go SDK
=====

[![Build Status](https://travis-ci.org/maciekmm/messenger-platform-go-sdk.svg?branch=master)](https://travis-ci.org/maciekmm/messenger-platform-go-sdk)
[![Build Status](https://travis-ci.org/maciekmm/messenger-platform-go-sdk.svg?branch=master)](https://travis-ci.org/maciekmm/messenger-platform-go-sdk)
[![Coverage Status](https://coveralls.io/repos/github/maciekmm/messenger-platform-go-sdk/badge.svg?branch=master)](https://coveralls.io/github/maciekmm/messenger-platform-go-sdk?branch=master)

A Go SDK for the [Facebook Messenger Platform](https://developers.facebook.com/docs/messenger-platform).
Expand All @@ -14,7 +14,7 @@ go get gopkg.in/maciekmm/messenger-platform-go-sdk.v4

## Usage

The main package has been named `messenger` for convenience.
The main package has been named `messenger` for convenience.

Your first step is to create `Messenger` instance.

Expand All @@ -37,7 +37,7 @@ messenger := &messenger.Messenger {
* `AccessToken` is required to send messages. You can find this token in your app developer dashboard under `Messenger` tab.
* `Debug` is used for setting debug mode type as described on https://developers.facebook.com/docs/graph-api/using-graph-api#debugging. Optional.

The next step is to hook up the handler to your HTTP server.
The next step is to hook up the handler to your HTTP server.

```go
//hook up
Expand All @@ -62,7 +62,7 @@ func MessageReceived(event messenger.Event, opts messenger.MessageOpts, msg mess

## Example

Check more examples in [examples folder.](https://github.com/maciekmm/messenger-platform-go-sdk/tree/master/examples)
Check more examples in [examples folder.](https://github.com/seenickcode/messenger-platform-go-sdk/tree/master/examples)

```go
var mess = &messenger.Messenger{
Expand Down
2 changes: 1 addition & 1 deletion attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
"testing"

"github.com/maciekmm/messenger-platform-go-sdk/template"
"github.com/seenickcode/messenger-platform-go-sdk/template"
)

func TestAttachmentUnmarshalling(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion examples/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"net/http"

"github.com/maciekmm/messenger-platform-go-sdk"
"github.com/seenickcode/messenger-platform-go-sdk"
)

var mess = &messenger.Messenger{
Expand Down
2 changes: 1 addition & 1 deletion examples/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"

"github.com/maciekmm/messenger-platform-go-sdk"
"github.com/seenickcode/messenger-platform-go-sdk"
)

func init() {
Expand Down
6 changes: 3 additions & 3 deletions examples/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"fmt"

"github.com/maciekmm/messenger-platform-go-sdk"
"github.com/maciekmm/messenger-platform-go-sdk/template"
"github.com/seenickcode/messenger-platform-go-sdk"
"github.com/seenickcode/messenger-platform-go-sdk/template"
)

func init() {
Expand All @@ -13,7 +13,7 @@ func init() {
mq.RecipientID(opts.Sender.ID)
mq.Template(template.GenericTemplate{Title: "abc",
Buttons: []template.Button{
template.Button{
{
Type: template.ButtonTypePostback,
Payload: "test",
Title: "abecadło",
Expand Down
14 changes: 11 additions & 3 deletions messagequery.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package messenger
import (
"errors"

"github.com/maciekmm/messenger-platform-go-sdk/template"
"github.com/seenickcode/messenger-platform-go-sdk/template"
)

type ContentType string
Expand Down Expand Up @@ -122,11 +122,19 @@ func (mq *MessageQuery) Template(tpl template.Template) error {

for _, v := range payload.Elements {
if v.Type() != tpl.Type() {
return errors.New("All templates have to have thesame type.")
return errors.New("All templates have to have the same type.")
}
}

payload.Elements = append(payload.Elements, tpl)
if t, ok := tpl.(template.ButtonTemplate); ok {
payload.ButtonsText += t.Text
for _, b := range t.Buttons {
payload.Buttons = append(payload.Buttons, b)
}
} else {
payload.Elements = append(payload.Elements, tpl)
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion messagequery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package messenger
import (
"testing"

"github.com/maciekmm/messenger-platform-go-sdk/template"
"github.com/seenickcode/messenger-platform-go-sdk/template"
)

func TestRandString(t *testing.T) {
Expand Down
60 changes: 60 additions & 0 deletions persistentmenu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package messenger

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)

type MenuItemType string

const (
MenuItemTypeWebURL MenuItemType = "web_url"
MenuItemTypePostback MenuItemType = "postback"
MenuItemTypeNested MenuItemType = "nested"
)

type MenuItem struct {
Type MenuItemType `json:"type"`
Title string `json:"title"`
URL string `json:"url,omitempty"`
Payload string `json:"payload,omitempty"`
CallToActions []MenuItem `json:"call_to_actions,omitempty"`
// TODO webview_height_ratio
// TODO messenger_extensions
// TODO fallback_url
}

type PersistentMenu struct {
Locale string `json:"locale"`
ComposerInputDisabled bool `json:"composer_input_disabled"`
CallToActions []MenuItem `json:"call_to_actions,omitempty"`
}

type PersistentMenuSettings struct {
PersistentMenu []PersistentMenu `json:"persistent_menu"`
}

func (m *Messenger) SetNestedPersistentMenu(set *PersistentMenuSettings) error {
body, err := json.Marshal(set)
if err != nil {
return err
}

resp, err := m.doRequest("POST", GraphAPI+"/v2.6/me/messenger_profile", bytes.NewReader(body))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Invalid status code %d", resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
result := &result{}
err = decoder.Decode(result)
if err != nil {
return err
}
return nil
}
1 change: 1 addition & 0 deletions template/buttons.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
ButtonTypePhoneNumber ButtonType = "phone_number"
ButtonTypeAccountLink ButtonType = "account_link"
ButtonTypeAccountUnlink ButtonType = "account_unlink"
ButtonTypeShare ButtonType = "element_share"
)

type Button struct {
Expand Down
26 changes: 15 additions & 11 deletions template/template.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package template

import (
"encoding/json"
"errors"
)
import "encoding/json"

type TemplateType string

Expand All @@ -13,20 +10,27 @@ type Template interface {
}

type Payload struct {
Elements []Template `json:"elements"`
Elements []Template `json:"elements"`
Buttons []Button `json:"buttons"` // used only for the Button Template
ButtonsText string `json:"text"` //
}

type rawPayload struct {
Type TemplateType `json:"template_type"`
Elements []Template `json:"elements"`
Type TemplateType `json:"template_type"`
Elements []Template `json:"elements,omitempty"`
Buttons []Button `json:"buttons,omitempty"` // used only for the Button Template
ButtonsText string `json:"text,omitempty"` //
}

func (p *Payload) MarshalJSON() ([]byte, error) {
rp := &rawPayload{}
if len(p.Elements) < 1 {
return []byte{}, errors.New("Elements slice cannot be empty")
}
rp.Elements = p.Elements
rp.Type = p.Elements[0].Type()
rp.Buttons = p.Buttons
if len(p.Elements) > 0 {
rp.Type = p.Elements[0].Type()
} else if len(p.Buttons) > 0 {
rp.Type = TemplateTypeButton
rp.ButtonsText = p.ButtonsText
}
return json.Marshal(rp)
}
4 changes: 2 additions & 2 deletions threadsettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"net/http"

"github.com/maciekmm/messenger-platform-go-sdk/template"
"github.com/seenickcode/messenger-platform-go-sdk/template"
)

type settingType string
Expand Down Expand Up @@ -106,7 +106,7 @@ func (m *Messenger) SetGetStartedButton(payload string) error {
result, err := m.changeThreadSettings(http.MethodPost, &threadSettings{
Type: settingTypeCallToActions,
State: threadStateNew,
CallToActions: []ctaPayload{ctaPayload{Payload: payload}},
CallToActions: []ctaPayload{{Payload: payload}},
})
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion threadsettings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package messenger
import (
"testing"

"github.com/maciekmm/messenger-platform-go-sdk/template"
"github.com/seenickcode/messenger-platform-go-sdk/template"
)

func TestSetGreetingText(t *testing.T) {
Expand Down