Last active
February 23, 2016 23:32
-
-
Save benjaminjkraft/3064559f6eb2270a5b40 to your computer and use it in GitHub Desktop.
slackimate.py
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
#!/usr/bin/env python | |
# | |
# Script to generate animated slack messages | |
# | |
# Credit for the idea goes to @mroth. | |
# | |
# Usage: ./slackimate.py '#channel-name' 'frame-1' 'frame-2' 'frame-3' ... | |
# (ctrl-C to end) | |
# If `--delete` is an argument, the message will be deleted on exit. | |
# | |
# alternately: | |
# >>> import slackimate | |
# >>> do_animation('#channel-name', frames) # frames is any iterable | |
import itertools | |
import time | |
import requests | |
import sys | |
# Token from https://api.slack.com/web | |
TOKEN = 'xoxp-FILL-ME-IN' | |
class SlackNotOkayException(Exception): | |
pass | |
def call_api(call, data): | |
data['token'] = TOKEN | |
res = requests.post('https://slack.com/api/' + call, data).json() | |
if res.get('ok'): | |
return res | |
else: | |
raise SlackNotOkayException(res.get('error', str(res))) | |
def do_animation(channel, frames, delay=1, delete=False): | |
frames = itertools.cycle(frames) | |
resp = call_api('chat.postMessage', { | |
'channel': channel, | |
'text': next(frames), | |
'as_user': 'true', | |
}) | |
ts = resp['ts'] | |
channel = resp['channel'] | |
try: | |
for frame in frames: | |
time.sleep(delay) | |
resp = call_api('chat.update', { | |
'ts': ts, | |
'channel': channel, | |
'text': frame, | |
}) | |
finally: | |
if delete: | |
call_api('chat.delete', { | |
'ts': ts, | |
'channel': channel, | |
}) | |
def main(): | |
if '--delete' in sys.argv: | |
delete = True | |
sys.argv.remove('--delete') | |
else: | |
delete = False | |
sys.exit(do_animation(sys.argv[1], sys.argv[2:], delete=delete)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment