forked from slackhappy/fs-slack
-
Notifications
You must be signed in to change notification settings - Fork 5
/
commands.py
108 lines (83 loc) · 2.89 KB
/
commands.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import api
import entity
import incoming_webhook
import logging
import re
REGISTRY = {}
def register(name, fn, help_text=None):
global REGISTRY
REGISTRY[name] = (fn, help_text)
def run(req):
command = Command(req)
fn, help_text = REGISTRY.get(command.command, (None, None))
if (fn):
return fn(command)
class Command(object):
def __init__(self, req):
self.token = req.get('token')
self.team_id = req.get('team_id')
self.channel_id = req.get('channel_id')
self.channel_name = req.get('channel_name')
self.user_id = req.get('user_id')
self.user_name = req.get('user_name')
self.command = req.get('command')
self.text = req.get('text')
self.req = req
def extract_entity(text):
tokens = filter(None, re.split(r'\s+', text.lstrip(), maxsplit=1, flags=re.UNICODE))
return tokens
def normalize_to(token):
if token.startswith('@'):
token = token[1:]
return token.lower()
def score(command, delta, message, icon_emoji):
info = extract_entity(command.text)
if info:
to = normalize_to(info[0])
r = entity.inc_entity(to, delta)
line = message.format(
to=to + u'\u200E',
score=r.score,
reason='' if len(info) < 2 else info[1] + ' '
)
logging.info(line)
incoming_webhook.post(
line,
username=command.user_name,
channel='#' + command.channel_name,
icon_emoji=icon_emoji)
def plusplus(command):
score(command, 1, u'/++ {to} {reason}(now at {score})', ':thumbsup:')
def minusminus(command):
score(command, -1, u'/-- {to} {reason}(now at {score})', ':thumbsdown:')
def do_paste(command, content, filetype='text'):
api.files_upload(
content=content,
filetype=filetype,
title=command.user_name + ' pasted some text',
channels=[command.channel_id])
def paste(command):
info = extract_entity(command.text)
filetype = 'text'
content = command.text
if len(info) > 1 and info[0] in ['scala', 'python']:
filetype = info[0]
content = info[1]
do_paste(command, content, filetype)
def pscala(command):
do_paste(command, command.text, 'scala')
def h(command):
logging.info('hi')
response = u'plusplusbot commands:\n'
for key in sorted(REGISTRY.keys()):
fn, help_text = REGISTRY[key]
response += u'{0} {1}\n'.format(key, '' if not help_text else help_text)
response += '\nrun /help commands for official slack commands\n'
return response
register(u'/++', plusplus, 'thing [reason]\t\t\t\t\t\tincrement score of thing [for reason]')
register(u'/--', minusminus, 'thing [reason]\t\t\t\t\t\tdecrement score of thing [for reason]')
# em-dash
register(u'/\u2014', minusminus, 'thing [reason]\t\t\t\t\t\tdecrement score of thing [for reason]')
#register(u'/p', paste, '[scala|python] some text\t\tpaste some text [optionally set language]')
#register(u'/pscala', pscala, 'some text\t\t\t\t\tpaste some text in scala')
register(u'/h', h, '\t\t\t\t\t\t\t\t\t\t\tthis help message')