Last active
December 11, 2015 17:59
-
-
Save emmettbutler/4638808 to your computer and use it in GitHub Desktop.
bash and python scripts for posting standups and sitdowns to Yammer via their REST API
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/python | |
import argparse | |
import json | |
import os | |
from os.path import expanduser | |
import urllib | |
import urllib2 | |
args = None | |
ACCESS_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxx" | |
ID_FILE = expanduser("~/standup.id") | |
# replace with your own actual access token, instructions here | |
# http://developer.yammer.com/oauth2-quickstart/ | |
def yammer_post(): | |
url = "https://www.yammer.com/api/v1/messages.json" | |
if args.sitdown: | |
with open(ID_FILE, "r") as f: | |
standup_id = f.readline() | |
url = "{}?replied_to_id={}".format(url, standup_id) | |
body = ' '.join(args.body) | |
hashtag = "" | |
if args.standup: | |
hashtag = "#standup" | |
elif args.sitdown: | |
hashtag = "#sitdown" | |
body = "{} {}".format(hashtag, body) | |
req = urllib2.Request(url) | |
req.method = "POST" | |
req.add_data(urllib.quote("body={}".format(body))) | |
req.add_header("Authorization", "Bearer {}".format(ACCESS_TOKEN)) | |
try: | |
res = urllib2.urlopen(req).read() | |
except Exception as e: | |
raise e | |
else: | |
if args.sitdown: | |
os.remove(ID_FILE) | |
if args.standup: | |
standup_id = json.loads(res)['messages'][0]['id'] | |
with open(ID_FILE, "w") as f: | |
f.write(str(standup_id)) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Post statuses to Yammer") | |
parser.add_argument('body', metavar='B', type=str, nargs='+', | |
help="The body of the status") | |
parser.add_argument('--standup', action="store_true", | |
help="Use the #standup hashtag") | |
parser.add_argument('--sitdown', action="store_true", | |
help="Use the #sitdown hashtag and reply to most recent standup") | |
args = parser.parse_args() | |
yammer_post() |
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
#!/bin/bash | |
access_token="xxxxxxxxxxxxxxxxxxxxxxxxxx" | |
# replace with your own actual access token after going through OAuth2 flow | |
# http://developer.yammer.com/oauth2-quickstart/ | |
standup=false | |
sitdown=false | |
dry_run=false | |
hashtag="" | |
function printUsage() | |
{ | |
echo "Usage: yammer [-u [-d [-n]]]" | |
echo | |
echo "-u: prefix with #standup" | |
echo "-d: prefix with #sitdown" | |
echo "-n: dry run" | |
echo "Sorry, each flag must be prefixed with its own dash" | |
exit | |
} | |
for var in "$@" | |
do | |
post_body=$var | |
if [[ $var == "-u" ]]; then | |
standup=true | |
elif [[ $var == "-d" ]]; then | |
sitdown=true | |
elif [[ $var == "-n" ]]; then | |
dry_run=true | |
elif [[ $var == "-h" ]]; then | |
printUsage | |
elif $standup; then | |
hashtag="#standup " | |
post_body=$var | |
elif $sitdown; then | |
hashtag="#sitdown " | |
post_body=$var | |
fi | |
done | |
if [[ $post_body == "" ]]; then | |
printUsage | |
else | |
echo "$hashtag$post_body" | |
if $dry_run; then | |
echo "[DRY RUN]" | |
else | |
curl -X POST --data-urlencode "body=$hashtag$post_body" -H "Authorization: Bearer $access_token" https://www.yammer.com/api/v1/messages.json | |
echo "Posted successfully!" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment