-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.py
63 lines (50 loc) · 1.65 KB
/
slack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# coding:utf-8
import time
import re
from slackclient import SlackClient
from dots import dots
API_TOKEN = ''
CHANNEL = '#general'
def post(slack, text='', pixel=':black_large_square:', space=':white_large_square:', channel=CHANNEL):
for c in text:
glyph = dots(c, pixel=pixel, space=space)
slack.api_call(
'chat.postMessage', username='dots',
icon_emoji=':robot_face:', channel=channel, text=glyph)
def error(slack, channel=CHANNEL):
msg = '''
Usage: dots <message> <pixel> <space>
'''
slack.api_call(
'chat.postMessage', username='dots',
icon_emoji=':robot_face:', channel=channel, text=msg)
def safe(list, index, default=''):
value = default
try:
value = list[index]
except Exception:
pass
return value
def main():
slack = SlackClient(API_TOKEN)
if slack.rtm_connect():
while True:
for message in slack.rtm_read():
text = message.get('text')
channel = message.get('channel')
if text is not None:
try:
m = re.search(r'『(.*)』', text)
message = m.group(1)
emojis = re.findall('\:\w+?\:', text)
pixel = safe(emojis, 0, ':black:')
space = safe(emojis, 1, ':white:')
post(slack, message, pixel, space, channel)
except Exception:
pass
time.sleep(1)
else:
print('Connection Failed, invalid token?')
if __name__ == '__main__':
main()