66from websocket import create_connection
77import json
88
9+
910class Server (object ):
1011 def __init__ (self , token , connect = True ):
1112 self .token = token
@@ -21,16 +22,19 @@ def __init__(self, token, connect=True):
2122
2223 if connect :
2324 self .rtm_connect ()
25+
2426 def __eq__ (self , compare_str ):
2527 if compare_str == self .domain or compare_str == self .token :
2628 return True
2729 else :
2830 return False
31+
2932 def __str__ (self ):
3033 data = ""
31- for key in self .__dict__ .keys ():
34+ for key in list ( self .__dict__ .keys () ):
3235 data += "{} : {}\n " .format (key , str (self .__dict__ [key ])[:40 ])
3336 return data
37+
3438 def __repr__ (self ):
3539 return self .__str__ ()
3640
@@ -39,7 +43,7 @@ def rtm_connect(self, reconnect=False):
3943 if reply .code != 200 :
4044 raise SlackConnectionError
4145 else :
42- login_data = json .loads (reply .read ())
46+ login_data = json .loads (reply .read (). decode ( 'utf-8' ) )
4347 if login_data ["ok" ]:
4448 self .ws_url = login_data ['url' ]
4549 if not reconnect :
@@ -70,7 +74,9 @@ def parse_channel_data(self, channel_data):
7074 channel ["name" ] = channel ["id" ]
7175 if "members" not in channel :
7276 channel ["members" ] = []
73- self .attach_channel (channel ["name" ], channel ["id" ], channel ["members" ])
77+ self .attach_channel (channel ["name" ],
78+ channel ["id" ],
79+ channel ["members" ])
7480
7581 def parse_user_data (self , user_data ):
7682 for user in user_data :
@@ -92,7 +98,10 @@ def ping(self):
9298 return self .send_to_websocket ({"type" : "ping" })
9399
94100 def websocket_safe_read (self ):
95- """Returns data if available, otherwise ''. Newlines indicate multiple messages """
101+ """ Returns data if available, otherwise ''. Newlines indicate multiple
102+ messages
103+ """
104+
96105 data = ""
97106 while True :
98107 try :
@@ -107,14 +116,17 @@ def attach_channel(self, name, id, members=[]):
107116 self .channels .append (Channel (self , name , id , members ))
108117
109118 def join_channel (self , name ):
110- print self .api_requester .do (self .token , "channels.join?name={}" .format (name )).read ()
119+ print (self .api_requester .do (self .token ,
120+ "channels.join?name={}" .format (name )).read ())
111121
112122 def api_call (self , method , ** kwargs ):
113123 reply = self .api_requester .do (self .token , method , kwargs )
114124 return reply .read ()
115125
126+
116127class SlackConnectionError (Exception ):
117128 pass
118129
130+
119131class SlackLoginError (Exception ):
120132 pass
0 commit comments