forked from Inumedia/SlackAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlackSocketClient.cs
235 lines (187 loc) · 6.41 KB
/
SlackSocketClient.cs
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
using System.Net.WebSockets;
using System;
using System.Diagnostics;
using System.Threading;
using SlackAPI.WebSocketMessages;
namespace SlackAPI
{
public class SlackSocketClient : SlackClient
{
SlackSocket underlyingSocket;
public event Action<NewMessage> OnMessageReceived;
public event Action<ReactionAdded> OnReactionAdded;
public event Action<Pong> OnPongReceived;
bool HelloReceived;
public const int PingInterval = 3000;
int pinging;
Timer pingingThread;
public long PingRoundTripMilliseconds { get; private set; }
public bool IsReady { get { return HelloReceived; } }
public bool IsConnected { get { return underlyingSocket != null && underlyingSocket.Connected; } }
public event Action OnHello;
internal LoginResponse loginDetails;
public SlackSocketClient(string token)
: base(token)
{
}
public override void Connect(Action<LoginResponse> onConnected, Action onSocketConnected = null)
{
base.Connect((s) => {
ConnectSocket(onSocketConnected);
onConnected(s);
});
}
protected override void Connected(LoginResponse loginDetails)
{
this.loginDetails = loginDetails;
base.Connected(loginDetails);
}
public void ConnectSocket(Action onSocketConnected){
underlyingSocket = new SlackSocket(loginDetails, this, onSocketConnected);
}
public void ErrorReceiving<K>(Action<WebSocketException> callback)
{
if (callback != null) underlyingSocket.ErrorReceiving += callback;
}
public void ErrorReceivingDesiralization<K>(Action<Exception> callback)
{
if (callback != null) underlyingSocket.ErrorReceivingDesiralization += callback;
}
public void ErrorHandlingMessage<K>(Action<Exception> callback)
{
if (callback != null) underlyingSocket.ErrorHandlingMessage += callback;
}
public void BindCallback<K>(Action<K> callback)
{
underlyingSocket.BindCallback(callback);
}
public void UnbindCallback<K>(Action<K> callback)
{
underlyingSocket.UnbindCallback(callback);
}
public void SendPresence(Presence status)
{
underlyingSocket.Send(new PresenceChange() { presence = Presence.active, user = base.MySelf.id });
}
public void SendTyping(string channelId)
{
underlyingSocket.Send(new Typing() { channel = channelId });
}
public void SendMessage(Action<MessageReceived> onSent, string channelId, string textData, string userName = null)
{
if (userName == null)
{
userName = MySelf.id;
}
if (onSent != null) {
underlyingSocket.Send( new Message() {channel = channelId, text = textData, user = userName, type = "message"}, onSent);
} else {
underlyingSocket.Send(new Message() { channel = channelId, text = textData, user = userName, type = "message" });
}
}
public void SendPing()
{
underlyingSocket.Send(new Ping());
}
public void HandlePongReceived(Pong pong)
{
if (OnPongReceived != null)
OnPongReceived(pong);
}
public void HandleReactionAdded(ReactionAdded reactionAdded)
{
if (OnReactionAdded != null)
OnReactionAdded(reactionAdded);
}
public void HandleHello(Hello hello)
{
HelloReceived = true;
if (OnHello != null)
OnHello();
}
public void HandlePresence(PresenceChange change)
{
UserLookup[change.user].presence = change.presence.ToString().ToLower();
}
public void HandleUserChange(UserChange change)
{
UserLookup[change.user.id] = change.user;
}
public void HandleTeamJoin(TeamJoin newuser)
{
UserLookup.Add(newuser.user.id, newuser.user);
}
public void HandleChannelCreated(ChannelCreated created)
{
ChannelLookup.Add(created.channel.id, created.channel);
}
public void HandleChannelRename(ChannelRename rename)
{
ChannelLookup[rename.channel.id].name = rename.channel.name;
}
public void HandleChannelDeleted(ChannelDeleted deleted)
{
ChannelLookup.Remove(deleted.channel);
}
public void HandleChannelArchive(ChannelArchive archive)
{
ChannelLookup[archive.channel].is_archived = true;
}
public void HandleChannelUnarchive(ChannelUnarchive unarchive)
{
ChannelLookup[unarchive.channel].is_archived = false;
}
public void HandleGroupJoined(GroupJoined joined)
{
GroupLookup.Add(joined.channel.id, joined.channel);
}
public void HandleGroupLeft(GroupLeft left)
{
GroupLookup.Remove(left.channel.id);
}
public void HandleGroupOpen(GroupOpen open)
{
GroupLookup[open.channel].is_open = true;
}
public void HandleGroupClose(GroupClose close)
{
GroupLookup[close.channel].is_open = false;
}
public void HandleGroupArchive(GroupArchive archive)
{
GroupLookup[archive.channel].is_archived = true;
}
public void HandleGroupUnarchive(GroupUnarchive unarchive)
{
GroupLookup[unarchive.channel].is_archived = false;
}
public void HandleGroupRename(GroupRename rename)
{
GroupLookup[rename.channel.id].name = rename.channel.name;
GroupLookup[rename.channel.id].created = rename.channel.created;
}
public void UserTyping(Typing t)
{
}
public void Message(NewMessage m)
{
if (OnMessageReceived != null)
OnMessageReceived(m);
}
public void FileShareMessage(FileShareMessage m)
{
if (OnMessageReceived != null)
OnMessageReceived(m);
}
public void PresenceChange(PresenceChange p)
{
}
public void ChannelMarked(ChannelMarked m)
{
}
public void CloseSocket()
{
underlyingSocket.Close();
}
}
}