Skip to content

Commit

Permalink
Add new Button functions and modify AddNewElement and NewElement to a…
Browse files Browse the repository at this point in the history
…ccept buttons param
  • Loading branch information
mileusna committed May 31, 2016
1 parent 38a470e commit e1f738e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 31 deletions.
17 changes: 8 additions & 9 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Example:
func main() {
msng := &messenger.Messenger{
AccessToken: "YOUR_ACCESS_TOKEN_THAT_YOU_WILL_GENERATE_FOR_YOUR_PAGE_ON_FACEBOOK",
VerifyToken: "YOUR_SECRET_TOKEN_FOR_VERIFYING_WEBHOOK_PUT_THE_SAME_VALUE_HERE_AND_ON_FB",
PageID: "YOUR_PAGE_ID",
MessageReceived: messageReceived, // your function for handling received messages, defined below
}
Expand All @@ -44,19 +45,17 @@ Example:
msng.SendTextMessage(userID, "Hello there")
case "send me website":
// now lets send him some structured message with image, link and buttons
// if we don't need buttons, we can use AddNewElement
// now lets send him some structured message with image and link
gm := msng.NewGenericMessage(userID)
gm.AddNewElement("Title", "Subtitle", "http://mysite.com", "http://mysite.com/some-photo.jpeg")
gm.AddNewElement("Title", "Subtitle", "http://mysite.com", "http://mysite.com/some-photo.jpeg", nil)
// GenericMessage can contain up to 10 elements, they are represented as cards and can be scoreled horicontally in messenger
// So lets add one more element, this time with buttons
e := msng.NewElement("Site title", "Subtitle", "http://mysite.com", "http://mysite.com/some-photo.jpeg")
e.AddWebURLButton("Contact US", "http://mysite.com/contact")
e.AddPostbackButton("Ok", "THIS_DATA_YOU_WILL_RECEIVE_AS_POSTBACK_WHEN_USER_CLICK_THE_BUTTON")
gm.AddElement(e)
btn1 := msng.NewWebURLButton("Contact US", "http://mysite.com/contact")
btn2 := msng.NewPostbackButton("Ok", "THIS_DATA_YOU_WILL_RECEIVE_AS_POSTBACK_WHEN_USER_CLICK_THE_BUTTON")
gm.AddNewElement("Site title", "Subtitle", "http://mysite.com", "http://mysite.com/some-photo.jpeg", []messenger.Button{btn1, btn2})
// ok, message is ready, lets sent
// ok, message is ready, lets send
msng.SendMessage(gm)
default:
Expand All @@ -69,7 +68,7 @@ Example:
return // if there is an error, resp is empty struct, useless
}
log.Println("Message ID", resp.MessageID, "sent to user", resp.RecipientID)
// store resp.MessageID if you want to track delivery reports that will be sent later from google
// store resp.MessageID if you want to track delivery reports that will be sent later from Facebook
}
}
Expand Down
17 changes: 8 additions & 9 deletions examples/first/first.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
func main() {
msng := &messenger.Messenger{
AccessToken: "YOUR_ACCESS_TOKEN_THAT_YOU_WILL_GENERATE_FOR_YOUR_PAGE_ON_FACEBOOK",
VerifyToken: "YOUR_SECRET_TOKEN_FOR_VERIFYING_WEBHOOK_PUT_THE_SAME_VALUE_HERE_AND_ON_FB",
PageID: "YOUR_PAGE_ID",
MessageReceived: messageReceived, // your function for handling received messages, defined below
}
Expand All @@ -35,19 +36,17 @@ func messageReceived(msng *messenger.Messenger, userID int64, m messenger.Facebo
msng.SendTextMessage(userID, "Hello there")

case "send me website":
// now lets send him some structured message with image, link and buttons
// if we don't need buttons, we can use AddNewElement
// now lets send him some structured message with image and link
gm := msng.NewGenericMessage(userID)
gm.AddNewElement("Title", "Subtitle", "http://mysite.com", "http://mysite.com/some-photo.jpeg")
gm.AddNewElement("Title", "Subtitle", "http://mysite.com", "http://mysite.com/some-photo.jpeg", nil)

// GenericMessage can contain up to 10 elements, they are represented as cards and can be scoreled horicontally in messenger
// So lets add one more element, this time with buttons
e := msng.NewElement("Site title", "Subtitle", "http://mysite.com", "http://mysite.com/some-photo.jpeg")
e.AddWebURLButton("Contact US", "http://mysite.com/contact")
e.AddPostbackButton("Ok", "THIS_DATA_YOU_WILL_RECEIVE_AS_POSTBACK_WHEN_USER_CLICK_THE_BUTTON")
gm.AddElement(e)
btn1 := msng.NewWebURLButton("Contact US", "http://mysite.com/contact")
btn2 := msng.NewPostbackButton("Ok", "THIS_DATA_YOU_WILL_RECEIVE_AS_POSTBACK_WHEN_USER_CLICK_THE_BUTTON")
gm.AddNewElement("Site title", "Subtitle", "http://mysite.com", "http://mysite.com/some-photo.jpeg", []messenger.Button{btn1, btn2})

// ok, message is ready, lets sent
// ok, message is ready, lets send
msng.SendMessage(gm)

default:
Expand All @@ -60,7 +59,7 @@ func messageReceived(msng *messenger.Messenger, userID int64, m messenger.Facebo
return // if there is an error, resp is empty struct, useless
}
log.Println("Message ID", resp.MessageID, "sent to user", resp.RecipientID)
// store resp.MessageID if you want to track delivery reports that will be sent later from google
// store resp.MessageID if you want to track delivery reports that will be sent later from Facebook
}
}

Expand Down
43 changes: 30 additions & 13 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,37 +121,54 @@ func (msng Messenger) NewGenericMessage(userID int64) GenericMessage {
}

// AddNewElement adds element to Generic template message with defined title, subtitle, link url and image url
// Only title is mandatory, other params can be empty string
// Generic messages can have up to 10 elements which are scolled horizontaly in users messenger
func (m *GenericMessage) AddNewElement(title, subtitle, itemURL, imageURL string) {
m.AddElement(newElement(title, subtitle, itemURL, imageURL))
// Title param is mandatory. If not used set "" for other params and nil for buttons param
// Generic messages can have up to 10 elements which are scolled horizontaly in Facebook messenger
func (m *GenericMessage) AddNewElement(title, subtitle, itemURL, imageURL string, buttons []Button) {
m.AddElement(newElement(title, subtitle, itemURL, imageURL, buttons))
}

// AddElement adds element e to Generic Message
// Generic messages can have up to 10 elements which are scolled horizontaly in users messenger
// If element contain buttons, you can create element with NewElement, than add some buttons with
// AddWebURLButton and AddPostbackButton and add it to message using this method
// Generic messages can have up to 10 elements which are scolled horizontaly in Facebook messenger
func (m *GenericMessage) AddElement(e Element) {
m.Message.Attachment.Payload.Elements = append(m.Message.Attachment.Payload.Elements, e)
}

// NewElement creates new element with defined title, subtitle, link url and image url
// Only title is mandatory, other params can be empty string
// Title param is mandatory. If not used set "" for other params and nil for buttons param
// Instead of calling this function you can also initialize Element struct, depends what you prefere
func (msng Messenger) NewElement(title, subtitle, itemURL, imageURL string) Element {
return newElement(title, subtitle, itemURL, imageURL)
func (msng Messenger) NewElement(title, subtitle, itemURL, imageURL string, buttons []Button) Element {
return newElement(title, subtitle, itemURL, imageURL, buttons)
}

func newElement(title, subtitle, itemURL, imageURL string) Element {
func newElement(title, subtitle, itemURL, imageURL string, buttons []Button) Element {
return Element{
Title: title,
Subtitle: subtitle,
ItemURL: itemURL,
ImageURL: imageURL,
Buttons: buttons,
}
}

// AddWebURLButton adds web link URL button to the element
// NewWebURLButton creates new web url button
func (msng Messenger) NewWebURLButton(title, URL string) Button {
return Button{
Type: ButtonTypeWebURL,
Title: title,
URL: URL,
}
}

// NewPostbackButton creates new postback button that sends payload string back to webhook when pressed
func (msng Messenger) NewPostbackButton(title, payload string) Button {
return Button{
Type: ButtonTypePostback,
Title: title,
Payload: payload,
}
}

// AddWebURLButton creates and adds web link URL button to the element
func (e *Element) AddWebURLButton(title, URL string) {
b := Button{
Type: ButtonTypeWebURL,
Expand All @@ -161,7 +178,7 @@ func (e *Element) AddWebURLButton(title, URL string) {
e.Buttons = append(e.Buttons, b)
}

// AddPostbackButton adds button that sends payload string back to webhook when pressed
// AddPostbackButton creates and adds button that sends payload string back to webhook when pressed
func (e *Element) AddPostbackButton(title, payload string) {
b := Button{
Type: ButtonTypePostback,
Expand Down

0 comments on commit e1f738e

Please sign in to comment.