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..a2ccc10 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/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= // 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. @@ -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 } @@ -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. diff --git a/profile.go b/profile.go index de3bbbc..fef6d27 100644 --- a/profile.go +++ b/profile.go @@ -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"` } diff --git a/response.go b/response.go index 97b0113..33ed456 100644 --- a/response.go +++ b/response.go @@ -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 @@ -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. @@ -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 @@ -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] } @@ -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] } @@ -147,7 +174,8 @@ func (r *Response) AttachmentWithReplies(attachment *StructuredMessageAttachment Attachment: attachment, QuickReplies: replies, }, - Tag: tag, + Tag: tag, + NotificationType: notificationType, } return r.DispatchMessage(&m) } @@ -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, @@ -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. @@ -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"` }