-
Notifications
You must be signed in to change notification settings - Fork 19
/
rocketchat.py
136 lines (98 loc) · 3.63 KB
/
rocketchat.py
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
from MeteorClient import MeteorClient
import time
class RocketChatBot():
def __init__(self, user, password, server='demo.rocket.chat'):
self.username = user
self.password = password
self.server = server
self.debug = True
self._prefixs = []
self.client = client = MeteorClient('wss://demo.rocket.chat/websocket')
# registering internal handlers
self.client.on('connected', self._connected)
self.client.on('closed', self._closed)
self.client.on('logging_in', self._logging_in)
self.client.on('failed', self._failed)
self.client.on('added', self._added)
self.client.on('changed', self._changed)
self.client.on('unsubscribed', self._unsubscribed)
self.client.on('subscribed', self._subscribed)
"""
Internal events handlers
"""
def _connected(self):
print("[+] rocketchat: connected")
self.client.subscribe('stream-messages', [], self.cb1)
def _closed(self, code, reason):
print('[-] rocketchat: connection closed: %s (%d)' % (reason, code))
def _logging_in(self):
print('[+] rocketchat: logging in')
def _failed(self, collection, data):
print('[-] %s' % str(data))
def _added(self, collection, id, fields):
print('[+] %s: %s' % (collection, id))
if not fields.get('args'):
return
args = fields['args']
if args[0] == "GENERAL":
print("[+] message: general, skipping")
return
if args[1].get('msg'):
return self._incoming(args[1])
if args[1].get('attachments'):
return self._downloading(args[1])
print(args)
print(args[0])
print(args[1])
# for key, value in fields.items():
# print('[+] %s: %s' % (key, value))
def _changed(self, collection, id, fields, cleared):
print('[+] changed: %s %s' % (collection, id))
for key, value in fields.items():
print('[+] field %s: %s' % (key, value))
for key, value in cleared.items():
print('[+] cleared %s: %s' % (key, value))
def _subscribed(self, subscription):
print('[+] subscribed: %s' % subscription)
def _unsubscribed(self, subscription):
print('[+] unsubscribed: %s' % subscription)
"""
Internal callback handlers
"""
def cb(self, error, data):
if not error:
if self.debug:
print(data)
return
print('[-] callback error:')
print(error)
def cb1(self, data):
if not self.debug:
return
if(data):
print(data)
else:
print("[+] callback success")
"""
Internal dispatcher
"""
def _incoming(self, data):
print("[+] Message from %s: %s" % (data['u']['username'], data['msg']))
for prefix in self._prefixs:
if data['msg'].startswith(prefix['prefix']):
prefix['handler'](self, data)
def _downloading(self, data):
print("[+] attachement from %s: %d files" % (data['u']['username'], len(data['attachments'])))
"""
Public initializers
"""
def start(self):
self.client.connect()
self.client.login(self.username, self.password.encode('utf-8'), callback=self.cb)
# let's yeld to background task
while True:
time.sleep(3600)
def addPrefixHandler(self, prefix, handler):
self._prefixs.append({'prefix': prefix, 'handler': handler})
def sendMessage(self, id, message):
self.client.call('sendMessage', [{'msg': message, 'rid': id}], self.cb)