FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SharpExchange/Examples/Chat/Program.cs at master · SOBotics/SharpExchange · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
SOBotics
/
SharpExchange
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
2
Code
Issues
2
Pull requests
1
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
SharpExchange
/
Examples
/
Chat
/
Program.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
77 lines (60 loc) · 2.6 KB
Breadcrumbs
SharpExchange
/
Examples
/
Chat
/
Program.cs
Copy path
File metadata and controls
77 lines (60 loc) · 2.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using
System
;
using
Newtonsoft
.
Json
.
Linq
;
using
SharpExchange
.
Auth
;
using
SharpExchange
.
Chat
.
Events
;
using
SharpExchange
.
Chat
.
Events
.
User
.
Extensions
;
using
SharpExchange
.
Net
.
WebSockets
;
using
SharpExchange
.
Chat
.
Actions
;
using
System
.
Threading
.
Tasks
;
public
class
AllData
:
ChatEventDataProcessor
,
IChatEventHandler
<
string
>
{
// The type of events we want to process.
private
EventType
[
]
events
=
new
[
]
{
EventType
.
All
}
;
public
override
EventType
[
]
Events
=>
events
;
public
event
Action
<
string
>
OnEvent
;
// Process the incoming JSON data coming from the RoomWatcher's
// WebSocket. In this example, we just stringify the object and
// invoke any listeners.
public
override
void
ProcessEventData
(
EventType
_
,
JToken
data
)
=>
OnEvent
?
.
Invoke
(
data
.
ToString
(
)
)
;
}
public
class
Program
{
// This stuff should ideally be loaded in from a configuration provider.
private
const
string
roomUrl
=
"https://chat.stackexchange.com/rooms/1/sandbox"
;
private
static
void
Main
(
string
[
]
args
)
=>
Demo
(
)
.
Wait
(
)
;
private
static
async
Task
Demo
(
)
{
// Fetch your account's credentials from somewhere.
var
auth
=
new
EmailAuthenticationProvider
(
""
,
""
)
;
// Log in to the community to authenticate with chat.
_
=
auth
.
Login
(
"stackexchange.com"
)
;
// Create an instance of the ActionScheduler. This will
// allow us to execute chat actions like: posting messages,
// kicking users, moving messages, etc.
using
var
actionScheduler
=
new
ActionScheduler
(
auth
,
roomUrl
)
;
// Create an instance of the RoomWatcher class. Here we
// specify (via the type parameter) what WebSocket implementation
// we'd like to use. This class allows you to subscribe to chat events.
using
var
roomWatcher
=
new
RoomWatcher
<
DefaultWebSocket
>
(
auth
,
roomUrl
)
;
// Subscribe to the UserMentioned event.
_
=
roomWatcher
.
AddUserMentionedEventHandler
(
async
m
=>
{
await
actionScheduler
.
CreateReplyAsync
(
"hello!"
,
m
.
MessageId
)
;
}
)
;
// Besides being able to subscribe to the default events,
// you can also create (and listen to) your own. Your class must
// implement the ChatEventDataProcessor class, you can also
// optionally implement IChatEventHandler or IChatEventHandler<T>.
var
customEventHanlder
=
new
AllData
(
)
;
// Add a very basic handler.
customEventHanlder
.
OnEvent
+=
data
=>
Console
.
WriteLine
(
data
)
;
// Add our custom event handler so we can
// begin processing the incoming event data.
roomWatcher
.
EventRouter
.
AddProcessor
(
customEventHanlder
)
;
// Post a simple message.
var
messageId
=
await
actionScheduler
.
CreateMessageAsync
(
"Hello world."
)
;
while
(
Console
.
ReadKey
(
true
)
.
Key
!=
ConsoleKey
.
Q
)
{
}
}
}
Back
|
FazBrowse Home
|
New Git URL