I want to write a Slack bot that sends IM Direct Messages. It’s a bit indirect in Slack. You have to first call im.list to get a list of all the “DM channels” the bot can write to. Then you have to look up the user info for each channel to figure out which person it actually is. Here’s some throwaway code that does that.
Bonus wrapper for the official Python slackclient library to decode the response and throw appropriate exceptions. That library has gotten little love from Slack, it could use some help.
from pprint import pprint from slackclient import SlackClient import json _slack = SlackClient("SECRET") def slack(*args, **kwargs): "Wrapper for Slack API. Manages stub, also decodes responses and raises exceptions" rb = _slack.api_call(*args, **kwargs) r = json.loads(rb.decode("utf-8")) if r.get("ok", False) != True: raise Exception("Error making Slack call", r) else: return r imList = slack("im.list") for user in imList['ims']: pprint(user) info = slack("users.info", user=user['user']) pprint(info)