go-roblox/message.go
2017-01-07 03:16:02 -05:00

137 lines
3.2 KiB
Go

package roblox
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// 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
func (s *Session) SendMessage(subject string, body string, recipientid int) error {
payload := map[string]interface{}{
"subject": subject,
"body": body,
"recipientId": recipientid,
}
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
}
// ArchiveMessages will archive a list of messages
func (s *Session) ArchiveMessages(ids []string) error {
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
}