messenger

package
v0.0.0-...-d110d06 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2020 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ButtonTypeWebURL is type for web links
	ButtonTypeWebURL = ButtonType("web_url")

	//ButtonTypePostback is type for postback buttons that sends data back to webhook
	ButtonTypePostback = ButtonType("postback")

	// AttachmentTypeTemplate for template attachments
	AttachmentTypeTemplate = AttachmentType("template")

	// TemplateTypeGeneric for generic message templates
	TemplateTypeGeneric = TemplateType("generic")

	// NotificationTypeRegular for regular notification type
	NotificationTypeRegular = NotificationType("REGULAR")

	// NotificationTypeSilentPush for silent push
	NotificationTypeSilentPush = NotificationType("SILENT_PUSH")

	// NotificationTypeNoPush for no push
	NotificationTypeNoPush = NotificationType("NO_PUSH")
)

Variables

View Source
var TestURL = ""

TestURL to mock FB server, used for testing

Functions

This section is empty.

Types

type AttachmentType

type AttachmentType string

AttachmentType describes attachment type in GenericMessage

type Button

type Button struct {
	Type    ButtonType `json:"type"`
	URL     string     `json:"url,omitempty"`
	Title   string     `json:"title"`
	Payload string     `json:"payload,omitempty"`
}

Button on Generic Message template element

type ButtonType

type ButtonType string

ButtonType for buttons, it can be ButtonTypeWebURL or ButtonTypePostback

type Element

type Element struct {
	Title    string   `json:"title"`
	Subtitle string   `json:"subtitle,omitempty"`
	ItemURL  string   `json:"item_url,omitempty"`
	ImageURL string   `json:"image_url,omitempty"`
	Buttons  []Button `json:"buttons,omitempty"`
}

Element in Generic Message template attachment

func (*Element) AddPostbackButton

func (e *Element) AddPostbackButton(title, payload string)

AddPostbackButton creates and adds button that sends payload string back to webhook when pressed

func (*Element) AddWebURLButton

func (e *Element) AddWebURLButton(title, URL string)

AddWebURLButton creates and adds web link URL button to the element

type FacebookDelivery

type FacebookDelivery struct {
	Mids      []string `json:"mids"`
	Seq       int      `json:"seq"`
	Watermark int      `json:"watermark"`
}

FacebookDelivery struct for delivery reports received from Facebook server as part of FacebookRequest struct

type FacebookError

type FacebookError struct {
	Code      int    `json:"code"`
	FbtraceID string `json:"fbtrace_id"`
	Message   string `json:"message"`
	Type      string `json:"type"`
}

FacebookError received form Facebook server if sending messages failed

func (*FacebookError) Error

func (err *FacebookError) Error() error

Error returns Go error object constructed from FacebookError data

type FacebookMessage

type FacebookMessage struct {
	Mid  string `json:"mid"`
	Seq  int    `json:"seq"`
	Text string `json:"text"`
}

FacebookMessage struct for text messaged received from facebook server as part of FacebookRequest struct

type FacebookPostback

type FacebookPostback struct {
	Payload string `json:"payload"`
}

FacebookPostback struct for postbacks received from Facebook server as part of FacebookRequest struct

type FacebookRequest

type FacebookRequest struct {
	Entry []struct {
		ID        int64 `json:"id"`
		Messaging []struct {
			Recipient struct {
				ID          int64  `json:"id,string"`
				PhoneNumber string `json:"phone_number"`
				Name        struct {
					FirstName string `json:"first_name"`
					LastName  string `json:"last_name"`
				} `json:"name"`
			} `json:"recipient"`
			Sender struct {
				ID int64 `json:"id,string"`
			} `json:"sender"`
			Timestamp int               `json:"timestamp"`
			Message   *FacebookMessage  `json:"message,omitempty"`
			Delivery  *FacebookDelivery `json:"delivery"`
			Postback  *FacebookPostback `json:"postback"`
		} `json:"messaging"`
		Time int `json:"time"`
	} `json:"entry"`
	Object string `json:"object"`
}

FacebookRequest received from Facebook server on webhook, contains messages, delivery reports and/or postbacks

func DecodeRequest

func DecodeRequest(r *http.Request) (FacebookRequest, error)

DecodeRequest decodes http request from FB messagner to FacebookRequest struct DecodeRequest will close the Body reader Usually you don't have to use DecodeRequest if you setup events for specific types

type FacebookResponse

type FacebookResponse struct {
	MessageID   string `json:"message_id"`
	RecipientID int64  `json:"recipient_id,string"`
}

FacebookResponse received from Facebook server after sending the message

type GenericMessage

type GenericMessage struct {
	Message          genericMessageContent `json:"message"`
	Recipient        recipient             `json:"recipient"`
	NotificationType NotificationType      `json:"notification_type,omitempty"`
}

GenericMessage struct used for sending structural messages to messenger (messages with images, links, and buttons)

func (*GenericMessage) AddElement

func (m *GenericMessage) AddElement(e Element)

AddElement adds element e to Generic Message Generic messages can have up to 10 elements which are scolled horizontaly in Facebook messenger

func (*GenericMessage) AddNewElement

func (m *GenericMessage) AddNewElement(title, subtitle, itemURL, imageURL string, buttons []Button)

AddNewElement adds element to Generic template message with defined title, subtitle, link url and image url 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

type Message

type Message interface {
	// contains filtered or unexported methods
}

Message interface that represents all type of messages that we can send to Facebook Messenger

type Messenger

type Messenger struct {
	AccessToken string
	VerifyToken string
	PageID      string

	// MessageReceived event fires when message from Facebook received
	MessageReceived func(msng *Messenger, user UserInfo, m FacebookMessage)

	// DeliveryReceived event fires when delivery report from Facebook received
	// Omit (nil) if you don't want to manage this events
	DeliveryReceived func(msng *Messenger, user UserInfo, d FacebookDelivery)

	// PostbackReceived event fires when postback received from Facebook server
	// Omit (nil) if you don't use postbacks and you don't want to manage this events
	PostbackReceived func(msng *Messenger, user UserInfo, p FacebookPostback)
	// contains filtered or unexported fields
}

Messenger struct

func New

func New(accessToken, pageID string) Messenger

New creates new messenger instance

func (Messenger) NewElement

func (msng Messenger) NewElement(title, subtitle, itemURL, imageURL string, buttons []Button) Element

NewElement creates new element with defined title, subtitle, link url and image url 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 (Messenger) NewGenericMessage

func (msng Messenger) NewGenericMessage(userID int64) GenericMessage

NewGenericMessage creates new Generic Template message for userID Generic template messages are used for structured messages with images, links, buttons and postbacks

func (Messenger) NewImageMessage

func (msng Messenger) NewImageMessage(userID int64, imageURL string) GenericMessage

func (Messenger) NewPostbackButton

func (msng Messenger) NewPostbackButton(title, payload string) Button

NewPostbackButton creates new postback button that sends payload string back to webhook when pressed

func (Messenger) NewTextMessage

func (msng Messenger) NewTextMessage(userID int64, text string) TextMessage

NewTextMessage creates new text message for userID This function is here for convenient reason, you will probably use shorthand version SentTextMessage which sends message immediatly

func (Messenger) NewWebURLButton

func (msng Messenger) NewWebURLButton(title, URL string) Button

NewWebURLButton creates new web url button

func (*Messenger) SendMessage

func (msng *Messenger) SendMessage(m Message) (FacebookResponse, error)

SendMessage sends chat message

func (Messenger) SendTextMessage

func (msng Messenger) SendTextMessage(receiverID int64, text string) (FacebookResponse, error)

SendTextMessage sends text messate to receiverID it is shorthand instead of crating new text message and then sending it

func (*Messenger) ServeHTTP

func (msng *Messenger) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is HTTP handler for Messenger so it could be directly used as http.Handler

func (Messenger) VerifyWebhook

func (msng Messenger) VerifyWebhook(w http.ResponseWriter, r *http.Request)

VerifyWebhook verifies your webhook by checking VerifyToken and sending challange back to Facebook

type NotificationType

type NotificationType string

NotificationType for sent messages

type TemplateType

type TemplateType string

TemplateType of template in GenericMessage

type TextMessage

type TextMessage struct {
	Message          textMessageContent `json:"message"`
	Recipient        recipient          `json:"recipient"`
	NotificationType NotificationType   `json:"notification_type,omitempty"`
}

TextMessage struct used for sending text messages to messenger

type UserInfo

type UserInfo struct {
	ID         int64
	Name       string
	Timezone   float64
	Locale     string
	ProfilePic string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL