This repository has been archived by the owner on Jul 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from mikemadden42/master
Add example scripts to show listing items & posting to channels.
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) | ||
|
||
# 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'] | ||
except KeyError, ex: | ||
print 'Environment variable %s not set.' % str(ex) | ||
|
||
|
||
if __name__ == '__main__': | ||
list_slack() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |