Skip to content
This repository has been archived by the owner on Aug 16, 2018. It is now read-only.

Commit

Permalink
Add SlackObject and SlackChannel
Browse files Browse the repository at this point in the history
SlackObject is an abstraction of the unified behavior of Slack related
objects
  • Loading branch information
Shir0kamii committed Sep 1, 2015
1 parent ae702e1 commit ae5794f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions slack_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .api import SlackAPI
from .channel import SlackChannel

__version__ = '0.2.16'
29 changes: 27 additions & 2 deletions slack_client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,31 @@

from .exceptions import SlackError, SlackNo

class SlackAPI(object):
class SlackObject:
"""The base object for all Slack Objects"""

_LIST_OBJECTS = dict()

def __init__(self, identifiant, token=None):
"""initialize a way to identify each instance"""
self.identifiant = identifiant
self._LIST_OBJECTS[identifiant] = self
if isinstance(token, SlackAPI):
self.api = token
elif isinstance(token, str):
self.api = SlackAPI.get_object(token)
else:
self.api = None

@classmethod
def get_object(cls, identifiant, token=None):
"""Return an instance of a class based on its identifiant"""
if identifiant in cls._LIST_OBJECTS:
return cls._LIST_OBJECTS[identifiant]
else:
return cls(token, identifiant)

class SlackAPI(SlackObject):
"""Main class for manipulation of the API
Stands as a wrapper for a single token"""
Expand All @@ -15,6 +39,7 @@ def __init__(self, token=None, allow_env_token=False):
The token can be either given through parameter "token"
or be read from the environment (when allow_env_token is True)"""
super().__init__(token)
if token != None:
self.token = token
elif allow_env_token and "SLACK_TOKEN" in os.environ:
Expand All @@ -40,7 +65,7 @@ def _make_request(self, method, parameters):

result = response.json()
if not result['ok']:
raise SlackNo(result['error'])
raise SlackNo(result['error'], result)

return result

Expand Down
30 changes: 30 additions & 0 deletions slack_client/channel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from .api import SlackObject, SlackAPI

class SlackChannel(SlackObject):

def __init__(self, token, id):
super().__init__(id, token)

self.data = self.api.channels.info(channel=id)['channel']

def __getattr__(self, name):
return self.data[name]

@classmethod
def from_name(cls, token, name):
if isinstance(token, SlackAPI):
api = token
elif isinstance(token, string):
api = SlackAPI.get_object(token)
else:
raise TypeError
return cls(api, api.channels_id(name))

def send(self, message, **kwargs):
params = {
'channel': self.id,
'text': message,
'as_user': True
}
params.update(kwargs)
self.api.chat.postMessage(**params)

0 comments on commit ae5794f

Please sign in to comment.