-
Notifications
You must be signed in to change notification settings - Fork 27
/
message.go
60 lines (54 loc) · 1.25 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package messenger
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
type MessageResponse struct {
RecipientID string `json:"recipient_id"`
MessageID string `json:"message_id"`
AttachmentID string `json:"attachment_id,omitempty"`
}
type rawMessage struct {
Recipient
MessageQuery
}
func (m *Messenger) sendCustomMessage(i interface{}) ([]byte, error) {
byt, err := json.Marshal(i)
if err != nil {
return nil, err
}
resp, err := m.doRequest("POST", GraphAPI+"/v2.6/me/messages", bytes.NewReader(byt))
if err != nil {
return nil, err
}
defer resp.Body.Close()
read, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
er := new(rawError)
json.Unmarshal(read, er)
return nil, er.Error
}
return read, err
}
func (m *Messenger) SendMessage(mq MessageQuery) (*MessageResponse, error) {
b, err := m.sendCustomMessage(mq)
if err != nil {
return nil, err
}
response := &MessageResponse{}
err = json.Unmarshal(b, response)
return response, err
}
func (m *Messenger) SendSimpleMessage(recipient string, message string) (*MessageResponse, error) {
return m.SendMessage(MessageQuery{
Recipient: Recipient{
ID: recipient,
},
Message: SendMessage{
Text: message,
},
MessagingType: MessagingTypeRegular,
})
}