From bc21717ee1d92915980bf42760097dd302685ffe Mon Sep 17 00:00:00 2001 From: Arran Ubels Date: Thu, 20 Jul 2017 09:45:57 +1000 Subject: [PATCH 1/6] Bare minimum to work --- messenger.go | 44 ++++++++++++++++++++++++++++++++++++++------ send.go | 2 +- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/messenger.go b/messenger.go index a0a99e6..98cd185 100644 --- a/messenger.go +++ b/messenger.go @@ -14,6 +14,7 @@ import ( "net/http" "os" "path/filepath" + "net/textproto" ) // This defines a bot @@ -42,7 +43,7 @@ func NewBotAPI(token string, vtoken string, secret string) *BotAPI { // It takes Request struct encoded into a buffer of json bytes // The APIResponse contains the error from FB if any // Should NOT be directly used, Use Send / SendFile -func (bot *BotAPI) MakeRequest(b *bytes.Buffer) (APIResponse, error) { +func (bot *BotAPI) MakeRequest(b *bytes.Buffer) (APIResponse, error) { uri := fmt.Sprintf(APIEndpoint, bot.Token) req, _ := http.NewRequest("POST", uri, b) @@ -66,6 +67,31 @@ func (bot *BotAPI) MakeRequest(b *bytes.Buffer) (APIResponse, error) { return rsp, nil } +func (bot *BotAPI) MakeRequestMultipart(contentType string, body io.Reader) (APIResponse, error) { + //uri := "http://localhost:1234/asdf" + uri := fmt.Sprintf(APIEndpoint, bot.Token) + + req, _ := http.NewRequest("POST", uri, body) + req.Header.Set("Content-Type", contentType) + resp, err := bot.Client.Do(req) + if err != nil { + return APIResponse{}, err + } + defer resp.Body.Close() + + var rsp APIResponse + dec := json.NewDecoder(resp.Body) + err = dec.Decode(&rsp) + if err != nil { + return APIResponse{}, nil + } + + if resp.StatusCode != 200 { + return rsp, errors.New(http.StatusText(resp.StatusCode)) + } + return rsp, nil +} + // This function helps send messages to users // It takes Message / GenericTemplate / ButtonTemplate / ReceiptTemplate and // sends it to the user @@ -168,11 +194,16 @@ func (bot *BotAPI) SendFile(u User, path string) (APIResponse, error) { body := &bytes.Buffer{} writer := multipart.NewWriter(body) - part, err := writer.CreateFormFile("filedata", filepath.Base(path)) + //part, err := writer.CreateFormFile("filedata", filepath.Base(path)) + hdr := textproto.MIMEHeader{ + "Content-Disposition": []string{"name=\"filedata\"; filename=\""+filepath.Base(path)+"\"", }, + "Content-Type": []string{ "image/png", }, + } + part, err := writer.CreatePart(hdr) if err != nil { return APIResponse{}, err } - _, err = io.Copy(part, file) + _, err = io.Copy (part, file) usr, _ := json.Marshal(u) _ = writer.WriteField("recipient", string(usr)) @@ -180,12 +211,13 @@ func (bot *BotAPI) SendFile(u User, path string) (APIResponse, error) { im, _ := json.Marshal(img) _ = writer.WriteField("message", string(im)) - err = writer.Close() - if err != nil { + contentType := writer.FormDataContentType() + + if err := writer.Close(); err != nil { return APIResponse{}, err } - return bot.MakeRequest(body) + return bot.MakeRequestMultipart(contentType, body) } //This function verifies the message diff --git a/send.go b/send.go index 6b8964d..8cd40ea 100644 --- a/send.go +++ b/send.go @@ -56,7 +56,7 @@ type AttachmentPayload interface{} // Used as payload for type image/audio/video/file type FilePayload struct { - URL string `json:"url"` + URL string `json:"url,omitempty"` } type TemplateBase struct { From 87486f4b3793659ba23bdc23577341c4eab08253 Mon Sep 17 00:00:00 2001 From: Arran Ubels Date: Thu, 20 Jul 2017 09:56:55 +1000 Subject: [PATCH 2/6] Streaming too --- messenger.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/messenger.go b/messenger.go index 98cd185..e5b0bf6 100644 --- a/messenger.go +++ b/messenger.go @@ -15,6 +15,7 @@ import ( "os" "path/filepath" "net/textproto" + "mime" ) // This defines a bot @@ -192,18 +193,30 @@ func (bot *BotAPI) SendFile(u User, path string) (APIResponse, error) { } defer file.Close() + ftypes, err := mime.ExtensionsByType(path) + if err != nil { + return APIResponse{}, err + } + if len(ftypes) == 0 { + return APIResponse{}, err + } + + return bot.SendStream(u, filepath.Base(path), ftypes[0], file) +} + +func (bot *BotAPI) SendStream(u User, filename, contentType string, r io.Reader) (APIResponse, error) { body := &bytes.Buffer{} writer := multipart.NewWriter(body) //part, err := writer.CreateFormFile("filedata", filepath.Base(path)) hdr := textproto.MIMEHeader{ - "Content-Disposition": []string{"name=\"filedata\"; filename=\""+filepath.Base(path)+"\"", }, - "Content-Type": []string{ "image/png", }, + "Content-Disposition": []string{"name=\"filedata\"; filename=\""+filename+"\"", }, + "Content-Type": []string{ contentType, }, } part, err := writer.CreatePart(hdr) if err != nil { return APIResponse{}, err } - _, err = io.Copy (part, file) + _, err = io.Copy (part, r) usr, _ := json.Marshal(u) _ = writer.WriteField("recipient", string(usr)) @@ -211,13 +224,13 @@ func (bot *BotAPI) SendFile(u User, path string) (APIResponse, error) { im, _ := json.Marshal(img) _ = writer.WriteField("message", string(im)) - contentType := writer.FormDataContentType() + contentType2 := writer.FormDataContentType() if err := writer.Close(); err != nil { return APIResponse{}, err } - return bot.MakeRequestMultipart(contentType, body) + return bot.MakeRequestMultipart(contentType2, body) } //This function verifies the message From 009a3996da7eb90e6537ad42bfe4661344601949 Mon Sep 17 00:00:00 2001 From: Arran Ubels Date: Wed, 21 Mar 2018 11:26:08 +1100 Subject: [PATCH 3/6] Required changes --- helpers.go | 2 +- messenger.go | 1 + receive.go | 6 +++--- send.go | 1 + 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/helpers.go b/helpers.go index 1e2720c..c392fb3 100644 --- a/helpers.go +++ b/helpers.go @@ -3,7 +3,7 @@ package mbotapi import "github.com/satori/go.uuid" // Creates a User from ID(int64) -func NewUserFromID(id int64) User { +func NewUserFromID(id string) User { return User{ ID: id, } diff --git a/messenger.go b/messenger.go index e5b0bf6..f62a1c4 100644 --- a/messenger.go +++ b/messenger.go @@ -120,6 +120,7 @@ func (bot *BotAPI) Send(u User, c interface{}, notif string) (APIResponse, error Recipient: u, Message: c.(Message), NotifType: n, + MessagingType: "RESPONSE", } case GenericTemplate: diff --git a/receive.go b/receive.go index a8ed131..01ad8bc 100644 --- a/receive.go +++ b/receive.go @@ -10,7 +10,7 @@ type Response struct { // This defines an Entry in the payload by webhook type Entry struct { - PageID int64 `json:"id"` + PageID string `json:"id"` Time int64 `json:"time"` Messaging []Callback `json:"messaging"` } @@ -53,12 +53,12 @@ func (c Callback) IsDelivery() bool { // This defines an user // One of the fields will be set to identify the user type User struct { - ID int64 `json:"id,omitempty,string"` + ID string `json:"id,omitempty"` PhoneNumber string `json:"phone_number,omitempty"` } type Page struct { - ID int64 `json:"id,string"` + ID string `json:"id"` } // Ref contains the `data-ref` set for message optin for the bot diff --git a/send.go b/send.go index 8cd40ea..7b40b29 100644 --- a/send.go +++ b/send.go @@ -27,6 +27,7 @@ type Request struct { Message Message `json:"message,omitempty"` Action Action `json:"sender_action,omitempty"` NotifType string `json:"notification_type"` + MessagingType string `json:"messaging_type,omitempty"` } type Action string From 4f1610933c68a42708a51cdb6bcf497b1f18cee7 Mon Sep 17 00:00:00 2001 From: arran ubels Date: Mon, 9 Jul 2018 00:01:38 +1000 Subject: [PATCH 4/6] Needed --- helpers.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/helpers.go b/helpers.go index c392fb3..4f759ef 100644 --- a/helpers.go +++ b/helpers.go @@ -164,12 +164,14 @@ func NewButtonTemplate(text string) ButtonTemplate { // Creates an empty Receipt Template func NewReceiptTemplate(rname string) ReceiptTemplate { + uuid4, err := uuid.NewV4() + if err != nil {} return ReceiptTemplate{ TemplateBase: TemplateBase{ Type: "receipt", }, RecipientName: rname, - ID: uuid.NewV4().String(), + ID: uuid4.String(), Currency: "USD", PaymentMethod: "", Items: []OrderItem{}, From ba078a4b3b80bc02726262c2cf4a38cab2c21455 Mon Sep 17 00:00:00 2001 From: Arran Ubels Date: Sun, 24 Feb 2019 17:51:41 +1100 Subject: [PATCH 5/6] Update helpers.go --- helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers.go b/helpers.go index 4f759ef..f6073e9 100644 --- a/helpers.go +++ b/helpers.go @@ -164,7 +164,7 @@ func NewButtonTemplate(text string) ButtonTemplate { // Creates an empty Receipt Template func NewReceiptTemplate(rname string) ReceiptTemplate { - uuid4, err := uuid.NewV4() + uuid4 := uuid.NewV4() if err != nil {} return ReceiptTemplate{ TemplateBase: TemplateBase{ From 40ea7e0440d2fda713bc0115350e241fb53a3613 Mon Sep 17 00:00:00 2001 From: Arran Ubels Date: Sun, 24 Feb 2019 17:52:39 +1100 Subject: [PATCH 6/6] Update helpers.go --- helpers.go | 1 - 1 file changed, 1 deletion(-) diff --git a/helpers.go b/helpers.go index f6073e9..4945d75 100644 --- a/helpers.go +++ b/helpers.go @@ -165,7 +165,6 @@ func NewButtonTemplate(text string) ButtonTemplate { // Creates an empty Receipt Template func NewReceiptTemplate(rname string) ReceiptTemplate { uuid4 := uuid.NewV4() - if err != nil {} return ReceiptTemplate{ TemplateBase: TemplateBase{ Type: "receipt",