1
0
mirror of synced 2024-11-25 06:16:07 +03:00

Fix styling

This commit is contained in:
Harrison 2016-05-04 18:09:34 +10:00
parent 09dbfd4b26
commit 036b286d6b
5 changed files with 73 additions and 71 deletions

View File

@ -11,6 +11,6 @@ const (
// DeliveryAction means that the event was a previous recipient reading their respective // DeliveryAction means that the event was a previous recipient reading their respective
// messages. // messages.
DeliveryAction DeliveryAction
// PostBackAction represents post call back // PostBackAction represents post call back
PostBackAction PostBackAction
) )

View File

@ -34,13 +34,13 @@ type Delivery struct {
// PostBack represents postback callback // PostBack represents postback callback
type PostBack struct { type PostBack struct {
// Sender is who the message was sent from. // Sender is who the message was sent from.
Sender Sender `json:"-"` Sender Sender `json:"-"`
// Recipient is who the message was sent to. // Recipient is who the message was sent to.
Recipient Recipient `json:"-"` Recipient Recipient `json:"-"`
// Time is when the message was sent. // Time is when the message was sent.
Time time.Time `json:"-"` Time time.Time `json:"-"`
// PostBack ID // PostBack ID
Payload string `json:"payload"` Payload string `json:"payload"`
} }

View File

@ -23,7 +23,7 @@ type Options struct {
VerifyToken string VerifyToken string
// Token is the access token of the Facebook page to send messages from. // Token is the access token of the Facebook page to send messages from.
Token string 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 WebhookURL string
} }
@ -41,7 +41,7 @@ type Messenger struct {
mux *http.ServeMux mux *http.ServeMux
messageHandlers []MessageHandler messageHandlers []MessageHandler
deliveryHandlers []DeliveryHandler deliveryHandlers []DeliveryHandler
postBackHandlers []PostBackHandler postBackHandlers []PostBackHandler
token string token string
verifyHandler func(http.ResponseWriter, *http.Request) verifyHandler func(http.ResponseWriter, *http.Request)
} }
@ -143,26 +143,26 @@ func (m *Messenger) dispatch(r Receive) {
} }
switch a { switch a {
case TextAction: case TextAction:
for _, f := range m.messageHandlers { for _, f := range m.messageHandlers {
message := *info.Message message := *info.Message
message.Sender = info.Sender message.Sender = info.Sender
message.Recipient = info.Recipient message.Recipient = info.Recipient
message.Time = time.Unix(info.Timestamp, 0) message.Time = time.Unix(info.Timestamp, 0)
f(message, resp) f(message, resp)
} }
case DeliveryAction: case DeliveryAction:
for _, f := range m.deliveryHandlers { for _, f := range m.deliveryHandlers {
f(*info.Delivery, resp) f(*info.Delivery, resp)
} }
case PostBackAction: case PostBackAction:
for _, f := range m.postBackHandlers { for _, f := range m.postBackHandlers {
message := *info.PostBack message := *info.PostBack
message.Sender = info.Sender message.Sender = info.Sender
message.Recipient = info.Recipient message.Recipient = info.Recipient
message.Time = time.Unix(info.Timestamp, 0) message.Time = time.Unix(info.Timestamp, 0)
f(message, resp) f(message, resp)
} }
} }
} }
} }

View File

@ -33,7 +33,7 @@ type MessageInfo struct {
// Delivery is the contents of a message if it is a DeliveryAction. // Delivery is the contents of a message if it is a DeliveryAction.
// Nil if it is not a DeliveryAction. // Nil if it is not a DeliveryAction.
Delivery *Delivery `json:"delivery"` Delivery *Delivery `json:"delivery"`
PostBack *PostBack `json:"postback"` PostBack *PostBack `json:"postback"`
} }

View File

@ -97,22 +97,23 @@ func (r *Response) Image(im image.Image) error {
return nil return nil
} }
// ButtonTemplate sends a message with the main contents being button elements
func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButton) error { func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButton) error {
m := SendStructuredMessage { m := SendStructuredMessage{
Recipient: r.to, Recipient: r.to,
Message: StructuredMessageData { Message: StructuredMessageData{
Attachment: StructuredMessageAttachment { Attachment: StructuredMessageAttachment{
Type: "template", Type: "template",
Payload: StructuredMessagePayload { Payload: StructuredMessagePayload{
TemplateType: "button", TemplateType: "button",
Text: text, Text: text,
Buttons: buttons, Buttons: buttons,
Elements: nil, Elements: nil,
}, },
}, },
}, },
} }
data, err := json.Marshal(m) data, err := json.Marshal(m)
if err != nil { if err != nil {
return nil return nil
@ -130,25 +131,26 @@ func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButto
resp, err := client.Do(req) resp, err := client.Do(req)
defer resp.Body.Close() defer resp.Body.Close()
return err return err
} }
// GenericTemplate is a message which allows for structural elements to be sent
func (r *Response) GenericTemplate(text string, elements *[]StructuredMessageElement) error { func (r *Response) GenericTemplate(text string, elements *[]StructuredMessageElement) error {
m := SendStructuredMessage { m := SendStructuredMessage{
Recipient: r.to, Recipient: r.to,
Message: StructuredMessageData { Message: StructuredMessageData{
Attachment: StructuredMessageAttachment { Attachment: StructuredMessageAttachment{
Type: "template", Type: "template",
Payload: StructuredMessagePayload { Payload: StructuredMessagePayload{
TemplateType: "generic", TemplateType: "generic",
Buttons: nil, Buttons: nil,
Elements: elements, Elements: elements,
}, },
}, },
}, },
} }
data, err := json.Marshal(m) data, err := json.Marshal(m)
if err != nil { if err != nil {
return nil return nil
@ -166,7 +168,7 @@ func (r *Response) GenericTemplate(text string, elements *[]StructuredMessageEle
resp, err := client.Do(req) resp, err := client.Do(req)
defer resp.Body.Close() defer resp.Body.Close()
return err return err
} }
@ -181,45 +183,45 @@ type MessageData struct {
Text string `json:"text,omitempty"` Text string `json:"text,omitempty"`
} }
// SendStructuredMessage is a structured message template // SendStructuredMessage is a structured message template.
type SendStructuredMessage struct { type SendStructuredMessage struct {
Recipient Recipient `json:"recipient"` Recipient Recipient `json:"recipient"`
Message StructuredMessageData `json:"message"` Message StructuredMessageData `json:"message"`
} }
// StructuredMessageData is an attachment sent with a structured message.
type StructuredMessageData struct { type StructuredMessageData struct {
Attachment StructuredMessageAttachment `json:"attachment"` Attachment StructuredMessageAttachment `json:"attachment"`
} }
// StructuredMessageAttachment is the attachment of a structured message.
type StructuredMessageAttachment struct { type StructuredMessageAttachment struct {
// Template allways // Type must be template
Type string `json:"type"` Type string `json:"type"`
// Payload is the information for the file which was sent in the attachment. // Payload is the information for the file which was sent in the attachment.
Payload StructuredMessagePayload `json:"payload"` Payload StructuredMessagePayload `json:"payload"`
} }
// StructuredMessagePayload is the actual payload of an attachment
type StructuredMessagePayload struct { type StructuredMessagePayload struct {
// button, generic, receipt // TemplateType must be button, generic or receipt
TemplateType string `json:"template_type"` TemplateType string `json:"template_type"`
Text string `json:"text,omitempty"` Text string `json:"text,omitempty"`
Elements *[]StructuredMessageElement `json:"elements,omitempty"` Elements *[]StructuredMessageElement `json:"elements,omitempty"`
Buttons *[]StructuredMessageButton `json:"buttons,omitempty"` Buttons *[]StructuredMessageButton `json:"buttons,omitempty"`
} }
// StructuredMessageElement - Generic Template // StructuredMessageElement is a response containing structural elements
type StructuredMessageElement struct { type StructuredMessageElement struct {
Title string `json:"title"` Title string `json:"title"`
ImageURL string `json:"image_url"` ImageURL string `json:"image_url"`
Subtitle string `json:"subtitle"` Subtitle string `json:"subtitle"`
Buttons []StructuredMessageButton `json:"buttons"` Buttons []StructuredMessageButton `json:"buttons"`
} }
// StructuredMessageButton - Button Template // StructuredMessageButton is a response containing buttons
type StructuredMessageButton struct { type StructuredMessageButton struct {
Type string `json:"type"` Type string `json:"type"`
URL string `json:"url"` URL string `json:"url"`
Title string `json:"title"` Title string `json:"title"`
} }