-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
106 lines (93 loc) · 5.16 KB
/
bot.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
import zulip
import re
import sys
import nltk
import text_gen
SUBSCRIPTIONS = set(['455 Broadway', 'Broadcasts', 'Iron Blogger', 'OSS at HS',
'Off-Topic', 'Sublime', 'Twisted', 'Unix commands', 'Victory',
'announce', 'arduino', 'bittorrent', 'code review', 'commits',
'elixir', 'food', 'friday-jobs-prep', 'git', 'html5', 'javascript',
'jobs', 'machine learning', 'math', 'nand2tetris', 'ocaml', 'pairing',
'poker', 'programming', 'python', 'social', 'test-bot', 'test-bot2',
'zulip', '1 Liberty Plaza', '137 Varick St', '455', '455\\', '6502',
'Broadway', 'C', 'ClojureScript Compiler', 'Hackathons!', 'Hackettes',
'ICFP 2013 Contest', 'Linux kernel', 'Matlab', 'PIG', 'SICP slowly',
'SICP study group', 'SICP-summer2013', 'San Francisco', 'Stripe CTF',
'VIM', 'WHAT', 'algorithms', 'alumni', 'audio programming', 'checkins',
'clojure', 'common-lisp', 'compilers', 'coq tutorial', 'couchsurfing',
'coursera PL course', 'coursera pl clourse', 'crossfit', 'data', 'dijkstra',
'emacs-users', 'erlang', 'functional data structures', 'golang', 'graph databases',
'hacker-music', 'haklsdjfas', 'haskell', 'help', 'housing', 'interactive fiction',
'julia', 'julia webstack doc-a-thon', 'linux', 'lockpicking', 'lovelace',
'lunch circle', 'meta', 'miniKanren', 'mongodb', 'music', 'music software',
'network', 'newish hackers', 'node', 'objective-c', 'off the wall',
'participation', 'princeton algos coursera', 'projects', 'pushups etc',
'racket', 'risk', 'ruby', 'russell-checkin', 'rust', 'sandbox', 'scala',
'spot-the-fail', 'stanford algos coursera', 'systems papers reading group',
'tapl', 'tech policy', 'test-stream', 'tictax', 'ttrpg', 'videogames',
'weekend-retreat', 'work', 'working on'])
SUBSCRIPTIONS_DICT = dict((item.lower(), item) for item in SUBSCRIPTIONS)
def extract_messages(response):
# import pdb
# pdb.set_trace()
return [msg['content'] for msg in response['messages'] if msg['content'] if msg['type'] != 'private' and msg['display_recipient'] != 'test-bot']
def stringify(lst):
return ' '.join(lst)
def strip_html(lst):
"""Takes a list of strings possibly with HTML tags,
returns a concatenated string with HTML stripped"""
#strip punctuation
#re-render escaped symbols
#preserve <code> blocks
stripped = re.sub('<code>.*</code>', ' ', ' '.join(lst), flags=re.DOTALL)
stripped = re.sub('<div class="codehilite">.*</div>', ' ', stripped, flags=re.DOTALL)
stripped = re.sub('<a.*</a>', ' ', stripped, flags=re.DOTALL)
stripped = re.sub('<[^<]+?>', ' ', stripped)
return stripped
def make_tokens(s):
sentences = nltk.sent_tokenize(s)
res = [word for sent in sentences for word in nltk.word_tokenize(sent)]
return res
def get_messages(anchor, num_before, streams, num_after=0):
messages = []
for stream in streams:
query_response = client.do_api_query({'anchor':anchor, 'num_before': num_before, 'num_after': num_after, 'narrow': [['stream', stream]]},
'https://zulip.com/api/v1/messages',
method='GET'
)
messages += extract_messages(query_response)
return messages
def respond(message):
if message['type'] == 'private' and message['sender_email'] != '[email protected]':
streams = set(SUBSCRIPTIONS_DICT.get(word.strip().lower(), word) for word in message['content'].split(','))
msg_id = message['id']
sender = message['sender_email']
fails = streams - SUBSCRIPTIONS
wins = SUBSCRIPTIONS & streams
if fails:
client.send_message({
'type': 'private',
'to': sender,
'content': 'Sorry, the following streams were not recognized: %s' %(', '.join(fails))
})
messages = get_messages(msg_id, 400, wins)
if wins:
client.send_message({
'type': 'private',
'to': sender,
'subject': 'This is what you sound like',
'content': text_gen.gen(make_tokens(strip_html(messages)))
})
def echo(message):
print message
anchor = message['id']
print client.do_api_query({'anchor':anchor, 'num_before': 0, 'num_after': 0},
'https://zulip.com/api/v1/messages',
method='GET'
)
# if message['type'] == 'private' and message['sender_email'] != '[email protected]':
# print message
if __name__ == '__main__':
client = zulip.Client()
client.call_on_each_message(respond)
# print get_messages(, 10, ['test-bot'])