-
Notifications
You must be signed in to change notification settings - Fork 23
/
chat.go
241 lines (202 loc) · 6.15 KB
/
chat.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
package steam
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
)
const (
PersonaStateOffline = iota
PersonaStateOnline
PersonaStateBusy
PersonaStateAway
PersonaStateSnooze
PersonaStateLookingToTrade
PersonaStateLookingToPlay
)
const (
PersonaStateFlagRichPresence = 1 << 0
PersonaStateFlagInJoinableGame = 1 << 1
PersonaStateFlagWeb = 1 << 8
PersonaStateFlagMobile = 1 << 9
PersonaStateFlagBigPicture = 1 << 10
)
const (
MessageTypeStatus = "personastate"
MessageTypeTyping = "typing"
MessageTypeSayText = "saytext"
)
const (
ChatUIModeMobile = "mobile" // empty string works too
ChatUIModeWeb = "web"
)
const (
apiUserPresenceLogin = "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Logon/v1"
apiUserPresenceLogoff = "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Logoff/v1"
apiUserPresencePoll = "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Poll/v1"
apiUserPresenceMessage = "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Message/v1"
)
type ChatMessage struct {
Type string `json:"type"`
Text string `json:"text"`
TimestampOff int64 `json:"timestamp"`
UTCTimestamp int64 `json:"utc_timestamp"`
Partner uint32 `json:"accountid_from"`
StatusFlags uint32 `json:"status_flags"`
PersonaState uint32 `json:"persona_state"`
PersonaName string `json:"persona_name"`
}
type ChatLogMessage struct {
Partner uint32 `json:"m_unAccountID"`
Timestamp int64 `json:"m_tsTimestamp"`
Message string `json:"m_strMessage"`
}
type ChatResponse struct {
Message int `json:"message"` // Login / Internal
UmqID string `json:"umqid"` // Login / Internal
TimestampOff int64 `json:"timestamp"` // Login
UTCTimestamp int64 `json:"utc_timestamp"` // Login
Push int `json:"push"` // Login
ErrorMessage string `json:"error"` // All (returned as error if not "OK")
MessageBase uint32 `json:"messagebase"` // ChatPoll
LastMessages uint32 `json:"messagelast"` // ChatPoll
Messages []*ChatMessage `json:"messages"` // ChatPoll
SecTimeout uint32 `json:"sectimeout"` // ChatPoll
}
type ChatFriendResponse struct {
AccountID uint32 `json:"m_unAccountID"`
SteamID SteamID `json:"m_ulSteamID,string"`
Name string `json:"m_strName"`
State uint8 `json:"m_ePersonaState"`
StateFlags uint32 `json:"m_nPersonaStateFlags"`
AvatarHash string `json:"m_strAvatarHash"`
InGame bool `json:"m_bIngame"`
InGameAppID uint64 `json:"m_nInGameAppID,string"`
InGameName string `json:"m_strInGameName"`
LastMessage int64 `json:"m_tsLastMessage"`
LastView int64 `json:"m_tsLastView"`
}
func (session *Session) ChatLogin(uiMode string) error {
resp, err := session.client.PostForm(apiUserPresenceLogin, url.Values{
"ui_mode": {uiMode},
"access_token": {session.oauth.Token},
})
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("http error: %d", resp.StatusCode)
}
var response ChatResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return err
}
if response.ErrorMessage != "OK" {
return errors.New(response.ErrorMessage)
}
session.umqID = response.UmqID
session.chatMessage = response.Message
return nil
}
func (session *Session) ChatLogoff() error {
resp, err := session.client.PostForm(apiUserPresenceLogoff, url.Values{
"access_token": {session.oauth.Token},
"umqid": {session.umqID},
})
if resp != nil {
resp.Body.Close()
}
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("http error: %d", resp.StatusCode)
}
return nil
}
func (session *Session) ChatSendMessage(sid SteamID, message, messageType string) error {
resp, err := session.client.PostForm(apiUserPresenceMessage, url.Values{
"access_token": {session.oauth.Token},
"steamid_dst": {sid.ToString()},
"text": {message},
"type": {messageType},
"umqid": {session.umqID},
})
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
var response ChatResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return err
}
if response.ErrorMessage != "OK" {
return errors.New(response.ErrorMessage)
}
return nil
}
func (session *Session) ChatPoll(timeoutSeconds string) (*ChatResponse, error) {
resp, err := session.client.PostForm(apiUserPresencePoll, url.Values{
"umqid": {session.umqID},
"access_token": {session.oauth.Token},
"message": {strconv.FormatUint(uint64(session.chatMessage), 10)},
"pollid": {"1"},
"sectimeout": {timeoutSeconds},
"secidletime": {"0"},
"use_accountids": {"1"},
})
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http error: %d", resp.StatusCode)
}
response := &ChatResponse{}
if err := json.NewDecoder(resp.Body).Decode(response); err != nil {
return nil, err
}
return response, nil
}
func (session *Session) ChatFriendState(sid SteamID) (*ChatFriendResponse, error) {
resp, err := session.client.Get("https://steamcommunity.com/chat/friendstate/" + strconv.FormatUint(uint64(sid.GetAccountID()), 10))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http error: %d", resp.StatusCode)
}
response := &ChatFriendResponse{}
if err := json.NewDecoder(resp.Body).Decode(response); err != nil {
return nil, err
}
return response, nil
}
func (session *Session) ChatLog(partner uint32) ([]*ChatLogMessage, error) {
resp, err := session.client.PostForm(fmt.Sprintf("https://steamcommunity.com/chat/chatlog/%d", partner), url.Values{
"sessionid": {session.sessionID},
})
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
log := []*ChatLogMessage{}
if err = json.NewDecoder(resp.Body).Decode(&log); err != nil {
return nil, err
}
return log, nil
}