Skip to content

Commit a45af00

Browse files
authored
Merge pull request slack-samples#16 from aerickson/output_can_take_username
output can take a username as an arg
2 parents 6a13371 + ad1586b commit a45af00

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

rtmbot/core.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import time
66
import logging
7+
import json
78

89
from slackclient import SlackClient
910

@@ -116,12 +117,26 @@ def output(self):
116117
for plugin in self.bot_plugins:
117118
limiter = False
118119
for output in plugin.do_output():
119-
channel = self.slack_client.server.channels.find(output[0])
120-
if channel is not None and output[1] is not None:
120+
destination = output[0]
121+
message = output[1]
122+
# things that start with U are users. convert to an IM channel.
123+
if destination.startswith('U'):
124+
try:
125+
result = json.loads(self.slack_client.api_call('im.open', user=destination))
126+
except ValueError:
127+
self._dbg("Parse error on im.open call results!")
128+
channel = self.slack_client.server.channels.find(
129+
result.get(u'channel', {}).get(u'id', None))
130+
elif destination.startswith('G'):
131+
result = self.slack_client.api_call('groups.open', channel=destination)
132+
channel = self.slack_client.server.channels.find(destination)
133+
else:
134+
channel = self.slack_client.server.channels.find(destination)
135+
if channel is not None and message is not None:
121136
if limiter:
122137
time.sleep(.1)
123138
limiter = False
124-
channel.send_message(output[1])
139+
channel.send_message(message)
125140
limiter = True
126141

127142
def crons(self):

0 commit comments

Comments
 (0)