slackmail/slackmail.py

40 lines
867 B
Python
Raw Normal View History

#!/usr/bin/python
# Copyright 2015 (c) Anna Schumaker.
import json
import sys
from urllib import request
# TODO: Move the token file outside of code directory
TOKEN = ""
with open("token") as f:
TOKEN = f.read().strip()
def call_method(method, args = dict()):
arglist = []
for (key, value) in args.items():
arglist += ["%s=%s" % (key, value)]
argstr = "&&".join(arglist)
server = "https://slack.com/api/"
with request.urlopen("%s/%s?%s" % (server, method, argstr)) as f:
return json.loads(f.read().decode())
return {"ok" : False}
def call_method_auth(method, args = dict()):
args["token"] = TOKEN
return call_method(method, args)
#
# Test connection before doing anything else
#
if call_method("api.test")["ok"] == False:
sys.exit(1)
if call_method_auth("auth.test")["ok"] == False:
sys.exit(1)