diff --git a/actions.go b/actions.go index 3b29a8a..9143573 100644 --- a/actions.go +++ b/actions.go @@ -11,6 +11,6 @@ const ( // DeliveryAction means that the event was a previous recipient reading their respective // messages. DeliveryAction - // PostBackAction represents post call back + // PostBackAction represents post call back PostBackAction ) diff --git a/message.go b/message.go index 1756c7a..f1d8027 100644 --- a/message.go +++ b/message.go @@ -34,13 +34,13 @@ type Delivery struct { // PostBack represents postback callback type PostBack struct { - // Sender is who the message was sent from. + // Sender is who the message was sent from. Sender Sender `json:"-"` // Recipient is who the message was sent to. Recipient Recipient `json:"-"` // Time is when the message was sent. Time time.Time `json:"-"` - // PostBack ID + // PostBack ID Payload string `json:"payload"` } diff --git a/messenger.go b/messenger.go index bf8c98c..d87e183 100644 --- a/messenger.go +++ b/messenger.go @@ -23,7 +23,7 @@ type Options struct { VerifyToken string // Token is the access token of the Facebook page to send messages from. Token string - // WebhookURL is where the Messenger client should listen for webhook events. + // WebhookURL is where the Messenger client should listen for webhook events. WebhookURL string } @@ -41,7 +41,7 @@ type Messenger struct { mux *http.ServeMux messageHandlers []MessageHandler deliveryHandlers []DeliveryHandler - postBackHandlers []PostBackHandler + postBackHandlers []PostBackHandler token string verifyHandler func(http.ResponseWriter, *http.Request) } @@ -143,26 +143,26 @@ func (m *Messenger) dispatch(r Receive) { } switch a { - case TextAction: - for _, f := range m.messageHandlers { - message := *info.Message - message.Sender = info.Sender - message.Recipient = info.Recipient - message.Time = time.Unix(info.Timestamp, 0) - f(message, resp) - } - case DeliveryAction: - for _, f := range m.deliveryHandlers { - f(*info.Delivery, resp) - } - case PostBackAction: - for _, f := range m.postBackHandlers { - message := *info.PostBack - message.Sender = info.Sender - message.Recipient = info.Recipient - message.Time = time.Unix(info.Timestamp, 0) - f(message, resp) - } + case TextAction: + for _, f := range m.messageHandlers { + message := *info.Message + message.Sender = info.Sender + message.Recipient = info.Recipient + message.Time = time.Unix(info.Timestamp, 0) + f(message, resp) + } + case DeliveryAction: + for _, f := range m.deliveryHandlers { + f(*info.Delivery, resp) + } + case PostBackAction: + for _, f := range m.postBackHandlers { + message := *info.PostBack + message.Sender = info.Sender + message.Recipient = info.Recipient + message.Time = time.Unix(info.Timestamp, 0) + f(message, resp) + } } } } diff --git a/receiving.go b/receiving.go index 52040af..7b22ab2 100644 --- a/receiving.go +++ b/receiving.go @@ -33,7 +33,7 @@ type MessageInfo struct { // Delivery is the contents of a message if it is a DeliveryAction. // Nil if it is not a DeliveryAction. Delivery *Delivery `json:"delivery"` - + PostBack *PostBack `json:"postback"` } diff --git a/response.go b/response.go index 1d90bf9..6ca29ac 100644 --- a/response.go +++ b/response.go @@ -97,22 +97,23 @@ func (r *Response) Image(im image.Image) error { return nil } +// ButtonTemplate sends a message with the main contents being button elements func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButton) error { - m := SendStructuredMessage { + m := SendStructuredMessage{ Recipient: r.to, - Message: StructuredMessageData { - Attachment: StructuredMessageAttachment { - Type: "template", - Payload: StructuredMessagePayload { - TemplateType: "button", - Text: text, - Buttons: buttons, - Elements: nil, - }, - }, + Message: StructuredMessageData{ + Attachment: StructuredMessageAttachment{ + Type: "template", + Payload: StructuredMessagePayload{ + TemplateType: "button", + Text: text, + Buttons: buttons, + Elements: nil, + }, + }, }, } - + data, err := json.Marshal(m) if err != nil { return nil @@ -130,25 +131,26 @@ func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButto resp, err := client.Do(req) defer resp.Body.Close() - + return err } +// GenericTemplate is a message which allows for structural elements to be sent func (r *Response) GenericTemplate(text string, elements *[]StructuredMessageElement) error { - m := SendStructuredMessage { + m := SendStructuredMessage{ Recipient: r.to, - Message: StructuredMessageData { - Attachment: StructuredMessageAttachment { - Type: "template", - Payload: StructuredMessagePayload { - TemplateType: "generic", - Buttons: nil, - Elements: elements, - }, - }, + Message: StructuredMessageData{ + Attachment: StructuredMessageAttachment{ + Type: "template", + Payload: StructuredMessagePayload{ + TemplateType: "generic", + Buttons: nil, + Elements: elements, + }, + }, }, } - + data, err := json.Marshal(m) if err != nil { return nil @@ -166,7 +168,7 @@ func (r *Response) GenericTemplate(text string, elements *[]StructuredMessageEle resp, err := client.Do(req) defer resp.Body.Close() - + return err } @@ -181,45 +183,45 @@ type MessageData struct { Text string `json:"text,omitempty"` } -// SendStructuredMessage is a structured message template +// SendStructuredMessage is a structured message template. type SendStructuredMessage struct { - Recipient Recipient `json:"recipient"` + Recipient Recipient `json:"recipient"` Message StructuredMessageData `json:"message"` } +// StructuredMessageData is an attachment sent with a structured message. type StructuredMessageData struct { Attachment StructuredMessageAttachment `json:"attachment"` } +// StructuredMessageAttachment is the attachment of a structured message. type StructuredMessageAttachment struct { - // Template allways + // Type must be template Type string `json:"type"` // Payload is the information for the file which was sent in the attachment. Payload StructuredMessagePayload `json:"payload"` } +// StructuredMessagePayload is the actual payload of an attachment type StructuredMessagePayload struct { - // button, generic, receipt - TemplateType string `json:"template_type"` - Text string `json:"text,omitempty"` - Elements *[]StructuredMessageElement `json:"elements,omitempty"` - Buttons *[]StructuredMessageButton `json:"buttons,omitempty"` + // TemplateType must be button, generic or receipt + TemplateType string `json:"template_type"` + Text string `json:"text,omitempty"` + Elements *[]StructuredMessageElement `json:"elements,omitempty"` + Buttons *[]StructuredMessageButton `json:"buttons,omitempty"` } -// StructuredMessageElement - Generic Template +// StructuredMessageElement is a response containing structural elements type StructuredMessageElement struct { - Title string `json:"title"` - ImageURL string `json:"image_url"` - Subtitle string `json:"subtitle"` - Buttons []StructuredMessageButton `json:"buttons"` + Title string `json:"title"` + ImageURL string `json:"image_url"` + Subtitle string `json:"subtitle"` + Buttons []StructuredMessageButton `json:"buttons"` } -// StructuredMessageButton - Button Template +// StructuredMessageButton is a response containing buttons type StructuredMessageButton struct { - Type string `json:"type"` - URL string `json:"url"` - Title string `json:"title"` + Type string `json:"type"` + URL string `json:"url"` + Title string `json:"title"` } - - -