This repository has been archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.go
141 lines (126 loc) · 3.39 KB
/
send.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package fbot
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
// URL to send messages to;
// is relative to the API URL.
const sendMessageURL = "%s/me/messages?access_token=%s&appsecret_proof=%s"
// Send a text message with a set of quick reply buttons to a user.
func (c Client) Send(id int64, message string, replies []Reply) error {
return c.send(id, struct {
Text string `json:"text"`
QuickReplies []quickReply `json:"quick_replies,omitempty"`
}{
Text: message,
QuickReplies: parseReplies(replies),
})
}
// SendWithButtons sends a message with a set of buttons and quick replies to a user.
func (c Client) SendWithButtons(id int64, message string, replies []Reply, buttons []Button) error {
return c.send(id, struct {
Attachment buttonAttachment `json:"attachment"`
QuickReplies []quickReply `json:"quick_replies,omitempty"`
}{
Attachment: buttonAttachment{
Type: "template",
Payload: buttonPayload{
Type: "button",
Text: message,
Buttons: buttons,
},
},
QuickReplies: parseReplies(replies),
})
}
func (c Client) send(id int64, message interface{}) error {
m := struct {
Recipient recipient `json:"recipient"`
Message interface{} `json:"message"`
}{
Recipient: recipient{ID: id},
Message: message,
}
data, err := json.Marshal(m)
if err != nil {
return fmt.Errorf("failed to get json of %#v: %v", m, err)
}
url := fmt.Sprintf(sendMessageURL, c.api, c.token, c.secretProof)
resp, err := http.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
return fmt.Errorf("failed to post \"%s\" to %#v: %v", data, url, err)
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode == 200 {
return nil
}
return checkError(resp.Body)
}
func parseReplies(replies []Reply) []quickReply {
var qs []quickReply
for _, r := range replies {
qs = append(qs, quickReply{
ContentType: "text",
Title: r.Text,
Payload: r.Payload,
})
}
return qs
}
// PayloadButton returns a button that posts a payload back to the bot.
func PayloadButton(text, payload string) Button {
return button{
Type: "postback",
Title: text,
Payload: payload,
}
}
// URLButton returns a button that opens a URL in a full-screen webview.
func URLButton(text, url string) Button {
return button{
Type: "web_url",
Title: text,
URL: url,
ShareButton: "hide",
Extensions: true,
}
}
// LinkButton returns a button that opens a URL in the browser.
func LinkButton(text, url string) Button {
return button{
Type: "web_url",
Title: text,
URL: url,
ShareButton: "hide",
Extensions: false,
}
}
type buttonAttachment struct {
Type string `json:"type"`
Payload buttonPayload `json:"payload"`
}
type buttonPayload struct {
Type string `json:"template_type"`
Text string `json:"text"`
Buttons []Button `json:"buttons"`
}
type button struct {
Type string `json:"type"`
Title string `json:"title"`
Payload string `json:"payload,omitempty"`
URL string `json:"url,omitempty"`
ShareButton string `json:"webview_share_button,omitempty"`
Extensions bool `json:"messenger_extensions,omitempty"`
}
type recipient struct {
ID int64 `json:"id,string"`
}
type quickReply struct {
ContentType string `json:"content_type,omitempty"`
Title string `json:"title,omitempty"`
Payload string `json:"payload"`
}