Skip to content
This repository has been archived by the owner on Dec 29, 2017. It is now read-only.

Commit

Permalink
Update slack command API (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
ovv authored Jul 18, 2017
1 parent a01b3dd commit 5d1b4d1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 30 deletions.
21 changes: 6 additions & 15 deletions sirbot/slack/dispatcher/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ async def _incoming(self, request):
logger.debug('Command handler received: %s', data['command'])

slack = registry.get('slack')
settings = self._endpoints.get(data['command'])
func = self._endpoints.get(data['command'])

if not settings:
if not func:
raise SlackUnknownCommand(data['command'])

command = await SlackCommand.from_raw(data, slack, settings=settings)
command = await SlackCommand.from_raw(data, slack)

if isinstance(self._save, list) and data['command'] in self._save \
or self._save is True:
Expand All @@ -56,19 +56,11 @@ async def _incoming(self, request):
db, command)
await db.commit()

coroutine = settings['func'](command, slack)
coroutine = func(command, slack)
ensure_future(coroutine=coroutine, loop=self._loop, logger=logger)

# if settings.get('public'):
# return Response(
# status=200,
# body=json.dumps({"response_type": "in_channel"}),
# content_type='application/json'
# )
# else:
return Response(status=200)

def register(self, command, func, public=False):
def register(self, command, func):
logger.debug('Registering slash command: %s, %s from %s',
command,
func.__name__,
Expand All @@ -77,5 +69,4 @@ def register(self, command, func, public=False):
if not asyncio.iscoroutinefunction(func):
func = asyncio.coroutine(func)

settings = {'func': func, 'public': public}
self._endpoints[command] = settings
self._endpoints[command] = func
20 changes: 7 additions & 13 deletions sirbot/slack/store/message/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,21 @@

class SlackCommand:
def __init__(self, command, frm, to, response_url, timestamp,
text='', raw=None, settings=None):
text='', raw=None):

if not raw:
raw = dict()

if not settings:
settings = dict()

self.command = command
self.frm = frm
self.to = to
self.response_url = response_url
self.timestamp = timestamp
self.text = text
self.raw = raw
self.settings = settings

@classmethod
async def from_raw(cls, data, slack, timestamp=None, settings=None):
async def from_raw(cls, data, slack, timestamp=None):

frm = await slack.users.get(data['user_id'])
if data['channel_id'].startswith('C'):
Expand All @@ -47,18 +43,16 @@ async def from_raw(cls, data, slack, timestamp=None, settings=None):
text=data['text'],
timestamp=timestamp,
raw=data,
settings=settings,
)

def response(self):
def response(self, type_='in_channel'):

if self.settings.get('public'):
response_type = 'in_channel'
else:
response_type = 'ephemeral'
if type_ not in ('in_channel', 'ephemeral'):
raise ValueError('''response type must be one of 'in_channel',
'ephemeral' ''')

return SlackMessage(
to=self.to,
response_url=self.response_url,
response_type=response_type
response_type=type_
)
4 changes: 2 additions & 2 deletions sirbot/slack/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ def add_event(self, event, func):
else:
raise SlackInactiveDispatcher

def add_command(self, command, func, public=False):
def add_command(self, command, func):
if 'command' in self._dispatcher:
self._dispatcher['command'].register(command, func, public=public)
self._dispatcher['command'].register(command, func)
else:
raise SlackInactiveDispatcher

Expand Down

0 comments on commit 5d1b4d1

Please sign in to comment.