91 lines
2.3 KiB
Python
91 lines
2.3 KiB
Python
#!/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)
|
|
|
|
|
|
|
|
#
|
|
# Find list of channels, we'll get info later
|
|
#
|
|
|
|
def list_channel_ids():
|
|
call = call_method_auth("channels.list")
|
|
if call["ok"] == False:
|
|
sys.exit(1)
|
|
for channel in call["channels"]:
|
|
yield channel["id"]
|
|
|
|
def find_channel_info(id):
|
|
call = call_method_auth("channels.info", { "channel" : id })
|
|
if call["ok"] == False:
|
|
sys.exit(1)
|
|
return call["channel"]
|
|
|
|
def read_channel(id, ts):
|
|
args = { "channel" : id, "oldest" : ts }
|
|
call = call_method_auth("channels.history", args)
|
|
if call["ok"] == False:
|
|
sys.exit(1)
|
|
messages = call["messages"]
|
|
messages.reverse()
|
|
for message in messages:
|
|
yield message
|
|
|
|
def print_message(message):
|
|
call = call_method_auth("users.info", {"user" : message["user"]})
|
|
if call["ok"] == False:
|
|
sys.exit(1)
|
|
|
|
name = "%s %s" % (call["user"]["profile"].get("first_name", ""), call["user"]["profile"].get("last_name", ""))
|
|
text = message["text"].encode("utf-8").decode("latin-1")
|
|
print("%s: %s" % (name, message["text"].encode("utf-8").decode("latin-1")))
|
|
|
|
|
|
|
|
|
|
for id in list_channel_ids():
|
|
channel = find_channel_info(id)
|
|
print(channel["name"])
|
|
print("\t%s" % channel["purpose"]["value"].encode("utf-8").decode("latin-1"))
|
|
if channel["is_member"]:
|
|
print("\tUnread count: %s, last read: %s" % (channel["unread_count"], channel["last_read"]))
|
|
for message in read_channel(id, channel["last_read"]):
|
|
print_message(message)
|
|
print()
|