2021-01-22 21:47:22 +00:00
|
|
|
// Package groupme defines a client capable of executing API commands for the GroupMe chat service
|
2020-07-30 02:20:57 +00:00
|
|
|
package groupme
|
|
|
|
|
|
|
|
import (
|
2020-08-24 01:25:02 +00:00
|
|
|
"bytes"
|
2021-01-22 21:47:22 +00:00
|
|
|
"context"
|
2020-08-24 01:25:02 +00:00
|
|
|
"encoding/json"
|
2020-07-30 02:20:57 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GroupMe documentation: https://dev.groupme.com/docs/v3#direct_messages
|
|
|
|
|
2021-01-22 21:47:22 +00:00
|
|
|
/*//////// Endpoints ////////*/
|
2020-07-30 02:20:57 +00:00
|
|
|
const (
|
|
|
|
// Used to build other endpoints
|
|
|
|
directMessagesEndpointRoot = "/direct_messages"
|
|
|
|
|
|
|
|
// Actual Endpoints
|
|
|
|
indexDirectMessagesEndpoint = directMessagesEndpointRoot // GET
|
|
|
|
createDirectMessageEndpoint = directMessagesEndpointRoot // POST
|
|
|
|
)
|
|
|
|
|
2021-01-22 21:47:22 +00:00
|
|
|
/*//////// API Requests ////////*/
|
2020-07-30 02:20:57 +00:00
|
|
|
|
2021-01-22 21:47:22 +00:00
|
|
|
// IndexDirectMessagesQuery defines the optional URL parameters for IndexDirectMessages
|
2020-07-30 02:20:57 +00:00
|
|
|
type IndexDirectMessagesQuery struct {
|
|
|
|
// Returns 20 messages created before the given message ID
|
|
|
|
BeforeID ID `json:"before_id"`
|
|
|
|
// Returns 20 messages created after the given message ID
|
|
|
|
SinceID ID `json:"since_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q IndexDirectMessagesQuery) String() string {
|
|
|
|
return marshal(&q)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IndexDirectMessagesResponse contains the count and set of
|
|
|
|
// messages returned by the IndexDirectMessages API request
|
|
|
|
type IndexDirectMessagesResponse struct {
|
|
|
|
Count int `json:"count"`
|
|
|
|
Messages []*Message `json:"direct_messages"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r IndexDirectMessagesResponse) String() string {
|
|
|
|
return marshal(&r)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
IndexDirectMessages -
|
|
|
|
|
|
|
|
Fetch direct messages between two users.
|
|
|
|
|
|
|
|
DMs are returned in groups of 20, ordered by created_at
|
|
|
|
descending.
|
|
|
|
|
|
|
|
If no messages are found (e.g. when filtering with since_id) we
|
|
|
|
return code 304.
|
|
|
|
|
|
|
|
Note that for historical reasons, likes are returned as an array
|
|
|
|
of user ids in the favorited_by key.
|
|
|
|
|
|
|
|
Parameters:
|
2023-09-20 02:08:46 +00:00
|
|
|
|
2020-07-30 02:20:57 +00:00
|
|
|
otherUserID - required, ID(string); the other participant in the conversation.
|
|
|
|
See IndexDirectMessagesQuery
|
|
|
|
*/
|
2023-09-20 02:08:46 +00:00
|
|
|
func (c *Client) IndexDirectMessages(ctx context.Context, otherUserID string, req *IndexDirectMessagesQuery, authToken string) (IndexDirectMessagesResponse, error) {
|
2020-07-30 02:20:57 +00:00
|
|
|
httpReq, err := http.NewRequest("GET", c.endpointBase+indexDirectMessagesEndpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
return IndexDirectMessagesResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
query := httpReq.URL.Query()
|
2021-01-22 21:47:22 +00:00
|
|
|
query.Set("other_user_id", otherUserID)
|
2020-07-30 02:20:57 +00:00
|
|
|
if req != nil {
|
|
|
|
if req.BeforeID != "" {
|
|
|
|
query.Add("before_ID", req.BeforeID.String())
|
|
|
|
}
|
|
|
|
if req.SinceID != "" {
|
|
|
|
query.Add("since_id", req.SinceID.String())
|
|
|
|
}
|
|
|
|
}
|
2021-05-08 19:39:39 +00:00
|
|
|
httpReq.URL.RawQuery = query.Encode()
|
2020-07-30 02:20:57 +00:00
|
|
|
|
|
|
|
var resp IndexDirectMessagesResponse
|
2023-09-20 02:08:46 +00:00
|
|
|
err = c.doWithAuthToken(ctx, httpReq, &resp, authToken)
|
2020-07-30 02:20:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return IndexDirectMessagesResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-01-22 21:47:22 +00:00
|
|
|
CreateDirectMessage - Send a DM to another user
|
2020-07-30 02:20:57 +00:00
|
|
|
|
|
|
|
If you want to attach an image, you must first process it
|
|
|
|
through our image service.
|
|
|
|
|
|
|
|
Attachments of type emoji rely on data from emoji PowerUps.
|
|
|
|
|
|
|
|
Clients use a placeholder character in the message text and
|
|
|
|
specify a replacement charmap to substitute emoji characters
|
|
|
|
|
|
|
|
The character map is an array of arrays containing rune data
|
|
|
|
([[{pack_id,offset}],...]).
|
|
|
|
*/
|
2023-09-20 02:08:46 +00:00
|
|
|
func (c *Client) CreateDirectMessage(ctx context.Context, m *Message, authToken string) (*Message, error) {
|
2020-07-30 02:20:57 +00:00
|
|
|
URL := fmt.Sprintf(c.endpointBase + createDirectMessageEndpoint)
|
|
|
|
|
|
|
|
m.SourceGUID = uuid.New().String()
|
2020-08-24 01:25:02 +00:00
|
|
|
var data = struct {
|
|
|
|
DirectMessage *Message `json:"direct_message,omitempty"`
|
|
|
|
}{
|
|
|
|
m,
|
|
|
|
}
|
2020-07-30 02:20:57 +00:00
|
|
|
|
2020-08-24 01:25:02 +00:00
|
|
|
jsonBytes, err := json.Marshal(&data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-30 02:20:57 +00:00
|
|
|
|
2020-08-24 01:25:02 +00:00
|
|
|
httpReq, err := http.NewRequest("POST", URL, bytes.NewBuffer(jsonBytes))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-30 02:20:57 +00:00
|
|
|
|
|
|
|
var resp struct {
|
2021-05-08 19:39:39 +00:00
|
|
|
*Message `json:"direct_message"`
|
2020-07-30 02:20:57 +00:00
|
|
|
}
|
2023-09-20 02:08:46 +00:00
|
|
|
err = c.doWithAuthToken(ctx, httpReq, &resp, authToken)
|
2020-07-30 02:20:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Message, nil
|
|
|
|
}
|