-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsend_message.go
40 lines (33 loc) · 925 Bytes
/
send_message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package instabot
import (
"bytes"
"context"
"encoding/json"
"io"
)
func encodeSendMessageJSON(w io.Writer, recipeint string, message Message) error {
enc := json.NewEncoder(w)
return enc.Encode(&struct {
Recipient *Recipient `json:"recipient"`
Message Message `json:"message"`
}{
Recipient: &Recipient{
ID: recipeint,
},
Message: message,
})
}
// SendMessage sends message by calling instagram api.
// https://developers.facebook.com/docs/messenger-platform/instagram/features/send-message#send-api
func (c *Client) SendMessage(ctx context.Context, recipient string, message Message) (*SendMessageResponse, error) {
var buf bytes.Buffer
if err := encodeSendMessageJSON(&buf, recipient, message); err != nil {
return nil, err
}
res, err := c.post(ctx, APIEndpointSendMessage, &buf)
if err != nil {
return nil, err
}
defer res.Body.Close()
return decodeToSendMessageResponse(res)
}