Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

some bug fixing, changed Graph API version to the earlier, handling instagram requests, added additional datas to Profile struct #72

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/paked/messenger

go 1.16

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
16 changes: 8 additions & 8 deletions messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import (
const (
// ProfileURL is the API endpoint used for retrieving profiles.
// Used in the form: https://graph.facebook.com/v2.6/<USER_ID>?fields=<PROFILE_FIELDS>&access_token=<PAGE_ACCESS_TOKEN>
ProfileURL = "https://graph.facebook.com/v2.6/"
ProfileURL = "https://graph.facebook.com/v14.0/"
// SendSettingsURL is API endpoint for saving settings.
SendSettingsURL = "https://graph.facebook.com/v2.6/me/thread_settings"
SendSettingsURL = "https://graph.facebook.com/v11.0/me/thread_settings"

// MessengerProfileURL is the API endpoint where you set properties that define various aspects of the following Messenger Platform features.
// Used in the form https://graph.facebook.com/v2.6/me/messenger_profile?access_token=<PAGE_ACCESS_TOKEN>
// https://developers.facebook.com/docs/messenger-platform/reference/messenger-profile-api/
MessengerProfileURL = "https://graph.facebook.com/v2.6/me/messenger_profile"
MessengerProfileURL = "https://graph.facebook.com/v11.0/me/messenger_profile"
)

// Options are the settings used when creating a Messenger client.
Expand Down Expand Up @@ -288,8 +288,8 @@ func (m *Messenger) handle(w http.ResponseWriter, r *http.Request) {
return
}

if rec.Object != "page" {
fmt.Println("Object is not page, undefined behaviour. Got", rec.Object)
if rec.Object != "page" && rec.Object != "instagram" {
fmt.Println("Object is not page or instagram, undefined behaviour. Got", rec.Object)
respond(w, http.StatusUnprocessableEntity)
return
}
Expand Down Expand Up @@ -426,7 +426,7 @@ func (m *Messenger) Response(to int64) *Response {
}

// Send will send a textual message to a user. This user must have previously initiated a conversation with the bot.
func (m *Messenger) Send(to Recipient, message string, messagingType MessagingType, tags ...string) error {
func (m *Messenger) Send(to Recipient, message string, messagingType MessagingType, tags ...TagType) error {
return m.SendWithReplies(to, message, nil, messagingType, tags...)
}

Expand All @@ -440,13 +440,13 @@ func (m *Messenger) SendGeneralMessage(to Recipient, elements *[]StructuredMessa
}

// SendWithReplies sends a textual message to a user, but gives them the option of numerous quick response options.
func (m *Messenger) SendWithReplies(to Recipient, message string, replies []QuickReply, messagingType MessagingType, tags ...string) error {
func (m *Messenger) SendWithReplies(to Recipient, message string, replies []QuickReply, messagingType MessagingType, tags ...TagType) error {
response := &Response{
token: m.token,
to: to,
}

return response.TextWithReplies(message, replies, messagingType, tags...)
return response.TextWithReplies(message, replies, messagingType, NotificationRegularType, tags...)
}

// Attachment sends an image, sound, video or a regular file to a given recipient.
Expand Down
9 changes: 9 additions & 0 deletions profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ type Profile struct {
Locale string `json:"locale"`
Timezone float64 `json:"timezone"`
Gender string `json:"gender"`

// instagram user profile
Username string `json:"username,omitempty"`
IsPrivate bool `json:"is_private,omitempty"`
//FollowCount int32 `json:"follow_count,omitempty"`
FollowedByCount int32 `json:"follower_count,omitempty"` // by the documentation followed_by_count
IsVerifiedUser bool `json:"is_verified_user"`
IsUserFollowBusiness bool `json:"is_user_follow_business"`
IsBusinessFollowUser bool `json:"is_business_follow_user"`
}
63 changes: 46 additions & 17 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ import (
// AttachmentType is attachment type.
type AttachmentType string
type MessagingType string
type NotificationType string
type TopElementStyle string
type ImageAspectRatio string
type SenderAction string
type TagType string

const (
// SendMessageURL is API endpoint for sending messages.
SendMessageURL = "https://graph.facebook.com/v2.11/me/messages"
SendMessageURL = "https://graph.facebook.com/v11.0/me/messages"
// ThreadControlURL is the API endpoint for passing thread control.
ThreadControlURL = "https://graph.facebook.com/v2.6/me/pass_thread_control"
ThreadControlURL = "https://graph.facebook.com/v11.0/me/pass_thread_control"
// InboxPageID is managed by facebook for secondary pass to inbox features: https://developers.facebook.com/docs/messenger-platform/handover-protocol/pass-thread-control
InboxPageID = 263902037430900

Expand All @@ -48,6 +51,13 @@ const (
// NonPromotionalSubscriptionType is NON_PROMOTIONAL_SUBSCRIPTION messaging type
NonPromotionalSubscriptionType MessagingType = "NON_PROMOTIONAL_SUBSCRIPTION"

// NotificationNoPushType is NO_PUSH notification type. No notification
NotificationNoPushType NotificationType = "NO_PUSH"
// NotificationRegularType is REGULAR notification type (default). Sound or vibration when a message is received by a person
NotificationRegularType NotificationType = "REGULAR"
// NotificationSilentPushType is SILENT_PUSH notification type. On-screen notification only
NotificationSilentPushType NotificationType = "SILENT_PUSH"

// TopElementStyle is compact.
CompactTopElementStyle TopElementStyle = "compact"
// TopElementStyle is large.
Expand All @@ -57,6 +67,21 @@ const (
HorizontalImageAspectRatio ImageAspectRatio = "horizontal"
// ImageAspectRatio is square.
SquareImageAspectRatio ImageAspectRatio = "square"

SenderActionMarkSeen SenderAction = "mark_seen"
SenderActionTypingOn SenderAction = "typing_on"
SenderActionTypingOff SenderAction = "typing_off"

// TagAccountUpdateType Tags the message you are sending to your customer as a non-recurring update to their application or account. Not available for Instagram Messaging API
TagAccountUpdateType TagType = "ACCOUNT_UPDATE"
// TagConfirmedEventUpdateType Tags the message you are sending to your customer as a reminder fo an upcoming event or an update for an event in procgres for which the customer is registered. Not available for Instagram Messaging API
TagConfirmedEventUpdateType TagType = "CONFIRMED_EVENT_UPDATE"
// TagCustomerFeedbackType Tags the message you are sending to your customer as a Customer Feedback Survey. Customer feedback messages must be sent within 7 days of the customer's last message. Not available for Instagram Messaging API
TagCustomerFeedbackType TagType = "CUSTOMER_FEEDBACK"
// TagHumanAgentType When this tag is added to a message to a customer, it allows a human agent to respond to a person's message. Messages can be sent within 7 days of the person's. Human agent support is for issues that cannot be resolved within the standard 24 hour messaging window
TagHumanAgentType TagType = "HUMAN_AGENT"
// TagPostPurchaseUpdateType Tags the message you are sending to your customer as an update for a recent purchase made by the customer. Not available for Instagram Messaging API.
TagPostPurchaseUpdateType TagType = "POST_PURCHASE_UPDATE"
)

// QueryResponse is the response sent back by Facebook when setting up things
Expand Down Expand Up @@ -107,15 +132,16 @@ func (r *Response) SetToken(token string) {
}

// Text sends a textual message.
func (r *Response) Text(message string, messagingType MessagingType, tags ...string) error {
return r.TextWithReplies(message, nil, messagingType, tags...)
func (r *Response) Text(message string, messagingType MessagingType, notificationType NotificationType, tags ...TagType) error {
return r.TextWithReplies(message, nil, messagingType, notificationType, tags...)
}

// TextWithReplies sends a textual message with some replies
// messagingType should be one of the following: "RESPONSE","UPDATE","MESSAGE_TAG","NON_PROMOTIONAL_SUBSCRIPTION"
// notificationType should be one of the following: "NO_PUSH","REGULAR" (default),"SILENT_PUSH"
// only supply tags when messagingType == "MESSAGE_TAG" (see https://developers.facebook.com/docs/messenger-platform/send-messages#messaging_types for more)
func (r *Response) TextWithReplies(message string, replies []QuickReply, messagingType MessagingType, tags ...string) error {
var tag string
func (r *Response) TextWithReplies(message string, replies []QuickReply, messagingType MessagingType, notificationType NotificationType, tags ...TagType) error {
var tag TagType
if len(tags) > 0 {
tag = tags[0]
}
Expand All @@ -128,14 +154,15 @@ func (r *Response) TextWithReplies(message string, replies []QuickReply, messagi
Attachment: nil,
QuickReplies: replies,
},
Tag: tag,
Tag: tag,
NotificationType: notificationType,
}
return r.DispatchMessage(&m)
}

// AttachmentWithReplies sends a attachment message with some replies
func (r *Response) AttachmentWithReplies(attachment *StructuredMessageAttachment, replies []QuickReply, messagingType MessagingType, tags ...string) error {
var tag string
func (r *Response) AttachmentWithReplies(attachment *StructuredMessageAttachment, replies []QuickReply, messagingType MessagingType, notificationType NotificationType, tags ...TagType) error {
var tag TagType
if len(tags) > 0 {
tag = tags[0]
}
Expand All @@ -147,7 +174,8 @@ func (r *Response) AttachmentWithReplies(attachment *StructuredMessageAttachment
Attachment: attachment,
QuickReplies: replies,
},
Tag: tag,
Tag: tag,
NotificationType: notificationType,
}
return r.DispatchMessage(&m)
}
Expand Down Expand Up @@ -327,7 +355,7 @@ func (r *Response) ListTemplate(elements *[]StructuredMessageElement, messagingT
}

// SenderAction sends a info about sender action
func (r *Response) SenderAction(action string) error {
func (r *Response) SenderAction(action SenderAction) error {
m := SendSenderAction{
Recipient: r.to,
SenderAction: action,
Expand Down Expand Up @@ -394,10 +422,11 @@ func (r *Response) PassThreadToInbox() error {

// SendMessage is the information sent in an API request to Facebook.
type SendMessage struct {
MessagingType MessagingType `json:"messaging_type"`
Recipient Recipient `json:"recipient"`
Message MessageData `json:"message"`
Tag string `json:"tag,omitempty"`
MessagingType MessagingType `json:"messaging_type"`
Recipient Recipient `json:"recipient"`
Message MessageData `json:"message"`
Tag TagType `json:"tag,omitempty"`
NotificationType NotificationType `json:"notification_type,omitempty"`
}

// MessageData is a message consisting of text or an attachment, with an additional selection of optional quick replies.
Expand Down Expand Up @@ -479,6 +508,6 @@ type StructuredMessageButton struct {

// SendSenderAction is the information about sender action
type SendSenderAction struct {
Recipient Recipient `json:"recipient"`
SenderAction string `json:"sender_action"`
Recipient Recipient `json:"recipient"`
SenderAction SenderAction `json:"sender_action"`
}