From e4dd9d29702d2a91cfafb6c55c051dc4f3047072 Mon Sep 17 00:00:00 2001 From: Pranas Kiziela Date: Mon, 14 Oct 2019 12:59:51 +0300 Subject: [PATCH 1/9] Add constants for sender actions --- response.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/response.go b/response.go index 473797d..6f285e2 100644 --- a/response.go +++ b/response.go @@ -21,6 +21,7 @@ type AttachmentType string type MessagingType string type TopElementStyle string type ImageAspectRatio string +type SenderAction string const ( // SendMessageURL is API endpoint for sending messages. @@ -57,6 +58,10 @@ const ( HorizontalImageAspectRatio ImageAspectRatio = "horizontal" // ImageAspectRatio is square. SquareImageAspectRatio ImageAspectRatio = "square" + + SenderActionMarkSeen SenderAction = "mark_seen" + SenderActionTypingOn SenderAction = "typing_on" + SenderActionTypingOff SenderAction = "typing_off" ) // QueryResponse is the response sent back by Facebook when setting up things @@ -326,7 +331,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, @@ -478,6 +483,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"` } From 0ffc913822458b3bcde03b9e6b7f78ebb8302909 Mon Sep 17 00:00:00 2001 From: simus Date: Fri, 9 Jul 2021 17:14:09 +0500 Subject: [PATCH 2/9] changing GraphAPI version to the latest, merging pull request related to Actions --- go.mod | 2 ++ messenger.go | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 3f7ff6c..601762f 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/messenger.go b/messenger.go index c2628af..9c01515 100644 --- a/messenger.go +++ b/messenger.go @@ -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/?fields=&access_token= - ProfileURL = "https://graph.facebook.com/v2.6/" + ProfileURL = "https://graph.facebook.com/v11.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= // 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. From cc80ff2577346072f4b88f490b01bb6c4a38af18 Mon Sep 17 00:00:00 2001 From: simus Date: Thu, 30 Jun 2022 10:04:17 +0500 Subject: [PATCH 3/9] changing GraphAPI version to the latest, merging pull request related to Actions --- messenger.go | 4 ++-- response.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/messenger.go b/messenger.go index 9c01515..25deed2 100644 --- a/messenger.go +++ b/messenger.go @@ -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 } diff --git a/response.go b/response.go index 98cca80..015494b 100644 --- a/response.go +++ b/response.go @@ -25,9 +25,9 @@ type SenderAction 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 From 4f279cdecec1df176f35c9f46587a16275e664da Mon Sep 17 00:00:00 2001 From: simus Date: Thu, 30 Jun 2022 13:40:12 +0500 Subject: [PATCH 4/9] added additional fields for instagram users, to Profile struct --- profile.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/profile.go b/profile.go index de3bbbc..34e80fb 100644 --- a/profile.go +++ b/profile.go @@ -9,4 +9,10 @@ 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:"followed_by_count,omitempty"` } From f64aafe8da4fd965e0ced988a2d29656dd1781f1 Mon Sep 17 00:00:00 2001 From: simus Date: Fri, 1 Jul 2022 14:17:35 +0500 Subject: [PATCH 5/9] changed Profile struct for instagram --- messenger.go | 2 +- profile.go | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/messenger.go b/messenger.go index 25deed2..b45b085 100644 --- a/messenger.go +++ b/messenger.go @@ -17,7 +17,7 @@ import ( const ( // ProfileURL is the API endpoint used for retrieving profiles. // Used in the form: https://graph.facebook.com/v2.6/?fields=&access_token= - ProfileURL = "https://graph.facebook.com/v11.0/" + ProfileURL = "https://graph.facebook.com/v14.0/" // SendSettingsURL is API endpoint for saving settings. SendSettingsURL = "https://graph.facebook.com/v11.0/me/thread_settings" diff --git a/profile.go b/profile.go index 34e80fb..fef6d27 100644 --- a/profile.go +++ b/profile.go @@ -11,8 +11,11 @@ type Profile struct { 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:"followed_by_count,omitempty"` + 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"` } From 2722fd225374786a18beb33da9c856a40cac3acd Mon Sep 17 00:00:00 2001 From: simus Date: Fri, 30 Dec 2022 16:16:19 +0500 Subject: [PATCH 6/9] handling notification_type of the messages --- response.go | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/response.go b/response.go index 015494b..72bcfd7 100644 --- a/response.go +++ b/response.go @@ -19,6 +19,7 @@ import ( // AttachmentType is attachment type. type AttachmentType string type MessagingType string +type NotificationType string type TopElementStyle string type ImageAspectRatio string type SenderAction string @@ -49,6 +50,13 @@ const ( // NonPromotionalSubscriptionType is NON_PROMOTIONAL_SUBSCRIPTION messaging type NonPromotionalSubscriptionType MessagingType = "NON_PROMOTIONAL_SUBSCRIPTION" + // NotificationNoPushType is NO_PUSH notification type + NotificationNoPushType NotificationType = "NO_PUSH" + // NotificationRegularType is REGULAR notification type (default) + NotificationRegularType NotificationType = "REGULAR" + // NotificationSilentPushType is SILENT_PUSH notification type + NotificationSilentPushType NotificationType = "SILENT_PUSH" + // TopElementStyle is compact. CompactTopElementStyle TopElementStyle = "compact" // TopElementStyle is large. @@ -112,14 +120,15 @@ 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 ...string) 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 { +func (r *Response) TextWithReplies(message string, replies []QuickReply, messagingType MessagingType, notificationType NotificationType, tags ...string) error { var tag string if len(tags) > 0 { tag = tags[0] @@ -133,13 +142,14 @@ 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 { +func (r *Response) AttachmentWithReplies(attachment *StructuredMessageAttachment, replies []QuickReply, messagingType MessagingType, notificationType NotificationType, tags ...string) error { var tag string if len(tags) > 0 { tag = tags[0] @@ -152,7 +162,8 @@ func (r *Response) AttachmentWithReplies(attachment *StructuredMessageAttachment Attachment: attachment, QuickReplies: replies, }, - Tag: tag, + Tag: tag, + NotificationType: notificationType, } return r.DispatchMessage(&m) } @@ -399,10 +410,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 string `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. From 183e6895c6323df76d8ca7dad2f5d1df5874e070 Mon Sep 17 00:00:00 2001 From: simus Date: Fri, 30 Dec 2022 16:21:00 +0500 Subject: [PATCH 7/9] added some official docstrings to the NotificationTypes --- response.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/response.go b/response.go index 72bcfd7..7864089 100644 --- a/response.go +++ b/response.go @@ -50,11 +50,11 @@ const ( // NonPromotionalSubscriptionType is NON_PROMOTIONAL_SUBSCRIPTION messaging type NonPromotionalSubscriptionType MessagingType = "NON_PROMOTIONAL_SUBSCRIPTION" - // NotificationNoPushType is NO_PUSH notification type + // NotificationNoPushType is NO_PUSH notification type. No notification NotificationNoPushType NotificationType = "NO_PUSH" - // NotificationRegularType is REGULAR notification type (default) + // 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 + // NotificationSilentPushType is SILENT_PUSH notification type. On-screen notification only NotificationSilentPushType NotificationType = "SILENT_PUSH" // TopElementStyle is compact. From 7d230dfdf2ab3fa0d79a6b94bb5c3f5d1fc5ff38 Mon Sep 17 00:00:00 2001 From: simus Date: Fri, 30 Dec 2022 16:30:42 +0500 Subject: [PATCH 8/9] added TagType enum for MESSAGE_TAG messaging type --- response.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/response.go b/response.go index 7864089..33ed456 100644 --- a/response.go +++ b/response.go @@ -23,6 +23,7 @@ type NotificationType string type TopElementStyle string type ImageAspectRatio string type SenderAction string +type TagType string const ( // SendMessageURL is API endpoint for sending messages. @@ -70,6 +71,17 @@ const ( 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 @@ -120,7 +132,7 @@ func (r *Response) SetToken(token string) { } // Text sends a textual message. -func (r *Response) Text(message string, messagingType MessagingType, notificationType NotificationType, tags ...string) error { +func (r *Response) Text(message string, messagingType MessagingType, notificationType NotificationType, tags ...TagType) error { return r.TextWithReplies(message, nil, messagingType, notificationType, tags...) } @@ -128,8 +140,8 @@ func (r *Response) Text(message string, messagingType MessagingType, notificatio // 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, notificationType NotificationType, 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] } @@ -149,8 +161,8 @@ func (r *Response) TextWithReplies(message string, replies []QuickReply, messagi } // AttachmentWithReplies sends a attachment message with some replies -func (r *Response) AttachmentWithReplies(attachment *StructuredMessageAttachment, replies []QuickReply, messagingType MessagingType, notificationType NotificationType, 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] } @@ -413,7 +425,7 @@ type SendMessage struct { MessagingType MessagingType `json:"messaging_type"` Recipient Recipient `json:"recipient"` Message MessageData `json:"message"` - Tag string `json:"tag,omitempty"` + Tag TagType `json:"tag,omitempty"` NotificationType NotificationType `json:"notification_type,omitempty"` } From 757993fbc619b80ce991cce0c00a607018eb4ae7 Mon Sep 17 00:00:00 2001 From: simus Date: Fri, 30 Dec 2022 16:42:59 +0500 Subject: [PATCH 9/9] bugfix with new types --- messenger.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/messenger.go b/messenger.go index b45b085..a2ccc10 100644 --- a/messenger.go +++ b/messenger.go @@ -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...) } @@ -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.