forked from dghubble/go-twitter
/
direct_messages.go
281 lines (248 loc) · 10.6 KB
/
direct_messages.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package twitter
import (
"net/http"
"time"
"github.com/dghubble/sling"
)
// DirectMessageEvents lists Direct Message events.
type DirectMessageEvents struct {
Events []DirectMessageEvent `json:"events"`
NextCursor string `json:"next_cursor"`
}
// DirectMessageEvent is a single Direct Message sent or received.
type DirectMessageEvent struct {
CreatedAt string `json:"created_timestamp,omitempty"`
ID string `json:"id,omitempty"`
Type string `json:"type"`
Message *DirectMessageEventMessage `json:"message_create"`
}
// DirectMessageEventMessage contains message contents, along with sender and
// target recipient.
type DirectMessageEventMessage struct {
SenderID string `json:"sender_id,omitempty"`
Target *DirectMessageTarget `json:"target"`
Data *DirectMessageData `json:"message_data"`
}
// DirectMessageTarget specifies the recipient of a Direct Message event.
type DirectMessageTarget struct {
RecipientID string `json:"recipient_id"`
}
// DirectMessageData is the message data contained in a Direct Message event.
type DirectMessageData struct {
Text string `json:"text"`
Entities *Entities `json:"entitites,omitempty"`
Attachment *DirectMessageDataAttachment `json:"attachment,omitempty"`
QuickReply *DirectMessageQuickReply `json:"quick_reply,omitempty"`
CTAs []DirectMessageCTA `json:"ctas,omitempty"`
}
// DirectMessageDataAttachment contains message data attachments for a Direct
// Message event.
type DirectMessageDataAttachment struct {
Type string `json:"type"`
Media MediaEntity `json:"media"`
}
// DirectMessageQuickReply contains quick reply data for a Direct Message
// event.
type DirectMessageQuickReply struct {
Type string `json:"type"`
Options []DirectMessageQuickReplyOption `json:"options"`
}
// DirectMessageQuickReplyOption represents Option object for
// a Direct Message's Quick Reply.
type DirectMessageQuickReplyOption struct {
Label string `json:"label"`
Description string `json:"description,omitempty"`
Metadata string `json:"metadata,omitempty"`
}
// DirectMessageCTA contains CTA data for a Direct Message event.
type DirectMessageCTA struct {
Type string `json:"type"`
Label string `json:"label"`
URL string `json:"url"`
}
// DirectMessageService provides methods for accessing Twitter direct message
// API endpoints.
type DirectMessageService struct {
baseSling *sling.Sling
sling *sling.Sling
}
// newDirectMessageService returns a new DirectMessageService.
func newDirectMessageService(sling *sling.Sling) *DirectMessageService {
return &DirectMessageService{
baseSling: sling.New(),
sling: sling.Path("direct_messages/"),
}
}
// DirectMessageEventsNewParams are the parameters for
// DirectMessageService.EventsNew
type DirectMessageEventsNewParams struct {
Event *DirectMessageEvent `json:"event"`
}
// EventsNew publishes a new Direct Message event and returns the event.
// Requires a user auth context with DM scope.
// https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event
func (s *DirectMessageService) EventsNew(params *DirectMessageEventsNewParams) (*DirectMessageEvent, *http.Response, error) {
// Twitter API wraps the event response
wrap := &struct {
Event *DirectMessageEvent `json:"event"`
}{}
apiError := new(APIError)
resp, err := s.sling.New().Post("events/new.json").BodyJSON(params).Receive(wrap, apiError)
return wrap.Event, resp, relevantError(err, *apiError)
}
// DirectMessageEventsShowParams are the parameters for
// DirectMessageService.EventsShow
type DirectMessageEventsShowParams struct {
ID string `url:"id,omitempty"`
}
// EventsShow returns a single Direct Message event by id.
// Requires a user auth context with DM scope.
// https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/get-event
func (s *DirectMessageService) EventsShow(id string, params *DirectMessageEventsShowParams) (*DirectMessageEvent, *http.Response, error) {
if params == nil {
params = &DirectMessageEventsShowParams{}
}
params.ID = id
// Twitter API wraps the event response
wrap := &struct {
Event *DirectMessageEvent `json:"event"`
}{}
apiError := new(APIError)
resp, err := s.sling.New().Get("events/show.json").QueryStruct(params).Receive(wrap, apiError)
return wrap.Event, resp, relevantError(err, *apiError)
}
// DirectMessageEventsListParams are the parameters for
// DirectMessageService.EventsList
type DirectMessageEventsListParams struct {
Cursor string `url:"cursor,omitempty"`
Count int `url:"count,omitempty"`
}
// EventsList returns Direct Message events (both sent and received) within
// the last 30 days in reverse chronological order.
// Requires a user auth context with DM scope.
// https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/list-events
func (s *DirectMessageService) EventsList(params *DirectMessageEventsListParams) (*DirectMessageEvents, *http.Response, error) {
events := new(DirectMessageEvents)
apiError := new(APIError)
resp, err := s.sling.New().Get("events/list.json").QueryStruct(params).Receive(events, apiError)
return events, resp, relevantError(err, *apiError)
}
// EventsDestroy deletes the Direct Message event by id.
// Requires a user auth context with DM scope.
// https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/delete-message-event
func (s *DirectMessageService) EventsDestroy(id string) (*http.Response, error) {
params := struct {
ID string `url:"id,omitempty"`
}{id}
apiError := new(APIError)
resp, err := s.sling.New().Delete("events/destroy.json").QueryStruct(params).Receive(nil, apiError)
return resp, relevantError(err, *apiError)
}
// DEPRECATED
// DirectMessage is a direct message to a single recipient (DEPRECATED).
type DirectMessage struct {
CreatedAt string `json:"created_at"`
Entities *Entities `json:"entities"`
ID int64 `json:"id"`
IDStr string `json:"id_str"`
Recipient *User `json:"recipient"`
RecipientID int64 `json:"recipient_id"`
RecipientScreenName string `json:"recipient_screen_name"`
Sender *User `json:"sender"`
SenderID int64 `json:"sender_id"`
SenderScreenName string `json:"sender_screen_name"`
Text string `json:"text"`
}
// CreatedAtTime returns the time a Direct Message was created (DEPRECATED).
func (d DirectMessage) CreatedAtTime() (time.Time, error) {
return time.Parse(time.RubyDate, d.CreatedAt)
}
// directMessageShowParams are the parameters for DirectMessageService.Show
type directMessageShowParams struct {
ID int64 `url:"id,omitempty"`
}
// Show returns the requested Direct Message (DEPRECATED).
// Requires a user auth context with DM scope.
// https://dev.twitter.com/rest/reference/get/direct_messages/show
func (s *DirectMessageService) Show(id int64) (*DirectMessage, *http.Response, error) {
params := &directMessageShowParams{ID: id}
dm := new(DirectMessage)
apiError := new(APIError)
resp, err := s.sling.New().Get("show.json").QueryStruct(params).Receive(dm, apiError)
return dm, resp, relevantError(err, *apiError)
}
// DirectMessageGetParams are the parameters for DirectMessageService.Get
// (DEPRECATED).
type DirectMessageGetParams struct {
SinceID int64 `url:"since_id,omitempty"`
MaxID int64 `url:"max_id,omitempty"`
Count int `url:"count,omitempty"`
IncludeEntities *bool `url:"include_entities,omitempty"`
SkipStatus *bool `url:"skip_status,omitempty"`
}
// Get returns recent Direct Messages received by the authenticated user
// (DEPRECATED).
// Requires a user auth context with DM scope.
// https://dev.twitter.com/rest/reference/get/direct_messages
func (s *DirectMessageService) Get(params *DirectMessageGetParams) ([]DirectMessage, *http.Response, error) {
dms := new([]DirectMessage)
apiError := new(APIError)
resp, err := s.baseSling.New().Get("direct_messages.json").QueryStruct(params).Receive(dms, apiError)
return *dms, resp, relevantError(err, *apiError)
}
// DirectMessageSentParams are the parameters for DirectMessageService.Sent
// (DEPRECATED).
type DirectMessageSentParams struct {
SinceID int64 `url:"since_id,omitempty"`
MaxID int64 `url:"max_id,omitempty"`
Count int `url:"count,omitempty"`
Page int `url:"page,omitempty"`
IncludeEntities *bool `url:"include_entities,omitempty"`
}
// Sent returns recent Direct Messages sent by the authenticated user
// (DEPRECATED).
// Requires a user auth context with DM scope.
// https://dev.twitter.com/rest/reference/get/direct_messages/sent
func (s *DirectMessageService) Sent(params *DirectMessageSentParams) ([]DirectMessage, *http.Response, error) {
dms := new([]DirectMessage)
apiError := new(APIError)
resp, err := s.sling.New().Get("sent.json").QueryStruct(params).Receive(dms, apiError)
return *dms, resp, relevantError(err, *apiError)
}
// DirectMessageNewParams are the parameters for DirectMessageService.New
// (DEPRECATED).
type DirectMessageNewParams struct {
UserID int64 `url:"user_id,omitempty"`
ScreenName string `url:"screen_name,omitempty"`
Text string `url:"text"`
}
// New sends a new Direct Message to a specified user as the authenticated
// user (DEPRECATED).
// Requires a user auth context with DM scope.
// https://dev.twitter.com/rest/reference/post/direct_messages/new
func (s *DirectMessageService) New(params *DirectMessageNewParams) (*DirectMessage, *http.Response, error) {
dm := new(DirectMessage)
apiError := new(APIError)
resp, err := s.sling.New().Post("new.json").BodyForm(params).Receive(dm, apiError)
return dm, resp, relevantError(err, *apiError)
}
// DirectMessageDestroyParams are the parameters for DirectMessageService.Destroy
// (DEPRECATED).
type DirectMessageDestroyParams struct {
ID int64 `url:"id,omitempty"`
IncludeEntities *bool `url:"include_entities,omitempty"`
}
// Destroy deletes the Direct Message with the given id and returns it if
// successful (DEPRECATED).
// Requires a user auth context with DM scope.
// https://dev.twitter.com/rest/reference/post/direct_messages/destroy
func (s *DirectMessageService) Destroy(id int64, params *DirectMessageDestroyParams) (*DirectMessage, *http.Response, error) {
if params == nil {
params = &DirectMessageDestroyParams{}
}
params.ID = id
dm := new(DirectMessage)
apiError := new(APIError)
resp, err := s.sling.New().Post("destroy.json").BodyForm(params).Receive(dm, apiError)
return dm, resp, relevantError(err, *apiError)
}