go-roblox/message.go

146 lines
3.3 KiB
Go
Raw Normal View History

2017-01-07 05:20:45 +00:00
package roblox
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
2017-01-08 06:32:13 +00:00
"strconv"
2017-01-07 05:20:45 +00:00
)
// Message structure for message data
type Message struct {
ID string
Subject string
Body string
FromID string
FromName string
}
// GetInbox makes a request to retrieve message list
func (s *Session) GetInbox() ([]Message, error) {
var messages []Message
resp, err := s.client.Get("https://www.roblox.com/messages/api/get-messages")
if err != nil {
return messages, err
}
if resp.StatusCode == 200 {
var data map[string]interface{}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return messages, err
}
err = json.Unmarshal(body, &data)
if err != nil {
return messages, err
}
collection, exists := data["Collection"]
if exists {
for _, value := range collection.([]interface{}) {
// We will assume if we got this far everything exists
msg := value.(map[string]interface{})
sender := msg["Sender"].(map[string]interface{})
message := Message{}
message.ID = string(int(msg["Id"].(float64)))
message.Subject = msg["Subject"].(string)
message.Body = msg["Body"].(string)
message.FromID = string(int(sender["UserId"].(float64)))
message.FromName = sender["UserName"].(string)
messages = append(messages, message)
}
}
}
return messages, err
}
// SendMessage will send a message via post
2017-01-08 02:30:45 +00:00
func (s *Session) SendMessage(subject string, body string, recipientid string) error {
2017-01-08 06:32:13 +00:00
reciever, err := strconv.Atoi(recipientid)
if err != nil {
return err
}
2017-01-07 05:20:45 +00:00
payload := map[string]interface{}{
"subject": subject,
"body": body,
2017-01-08 06:32:13 +00:00
"recipientId": reciever,
2017-01-07 05:20:45 +00:00
}
data, err := json.Marshal(&payload)
if err != nil {
return err
}
resp, err := s.client.Post("https://www.roblox.com/messages/send", "application/json", bytes.NewBuffer(data))
if err != nil {
return err
}
defer resp.Body.Close()
// CSRF Token Status
if resp.StatusCode == 403 {
req, err := http.NewRequest("POST", "https://www.roblox.com/messages/send", bytes.NewBuffer(data))
req.Header.Set("X-Csrf-Token", resp.Header["X-Csrf-Token"][0])
req.Header.Set("Content-Type", "application/json")
resp, err := s.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("Messaged send failed. Status %d", resp.StatusCode)
}
}
return nil
}
2017-01-07 08:16:02 +00:00
// ArchiveMessages will archive a list of messages
2017-01-07 05:20:45 +00:00
func (s *Session) ArchiveMessages(ids []string) error {
2017-01-07 09:58:52 +00:00
if len(ids) <= 0 {
return fmt.Errorf("ID list was empty")
}
2017-01-07 05:20:45 +00:00
payload := map[string][]string{
"messageIds": ids,
}
data, err := json.Marshal(&payload)
if err != nil {
return err
}
resp, err := s.client.Post("https://www.roblox.com/messages/api/archive-messages", "application/json", bytes.NewBuffer(data))
if err != nil {
return err
}
defer resp.Body.Close()
// CSRF Token Status
if resp.StatusCode == 403 {
req, err := http.NewRequest("POST", "https://www.roblox.com/messages/api/archive-messages", bytes.NewBuffer(data))
req.Header.Set("X-Csrf-Token", resp.Header["X-Csrf-Token"][0])
req.Header.Set("Content-Type", "application/json")
resp, err := s.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("Archive failed. Status %d", resp.StatusCode)
}
}
return nil
}