Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #74 from mikemadden42/master
Browse files Browse the repository at this point in the history
Add example scripts to show listing items & posting to channels.
  • Loading branch information
simonsolnes authored May 11, 2017
2 parents 37a94b0 + 54d8993 commit 865a3d9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
39 changes: 39 additions & 0 deletions examples/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python
"""List items in slack."""

# https://github.com/os/slacker
# https://api.slack.com/methods

import os
from slacker import Slacker


def list_slack():
"""List channels & users in slack."""
try:
token = os.environ['SLACK_TOKEN']
slack = Slacker(token)

# Get channel list
response = slack.channels.list()
channels = response.body['channels']
for channel in channels:
print channel['id'], channel['name']
# if not channel['is_archived']:
# slack.channels.join(channel['name'])
print

# Get users list
response = slack.users.list()
users = response.body['members']
for user in users:
if not user['deleted']:
print user['id'], user['name'], user['is_admin'], user[
'is_owner']
print
except KeyError, ex:
print 'Environment variable %s not set.' % str(ex)


if __name__ == '__main__':
list_slack()
30 changes: 30 additions & 0 deletions examples/post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python
"""Post slack message."""

# https://github.com/os/slacker
# https://api.slack.com/methods

import os
from slacker import Slacker


def post_slack():
"""Post slack message."""
try:
token = os.environ['SLACK_TOKEN']
slack = Slacker(token)

obj = slack.chat.post_message(
channel='#general',
text='',
as_user=True,
attachments=[{"pretext": "Subject",
"text": "Body"}])
print obj.successful, obj.__dict__['body']['channel'], obj.__dict__[
'body']['ts']
except KeyError, ex:
print 'Environment variable %s not set.' % str(ex)


if __name__ == '__main__':
post_slack()

0 comments on commit 865a3d9

Please sign in to comment.