-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_commands.py
210 lines (174 loc) · 6.3 KB
/
bot_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""Fun with Slackbots."""
import re
import os
import json
import requests
from random import randint
from bender.signals import event_received, message_received
WUNDERGROUND_API_KEY = os.environ['WUNDERGROUND_API_KEY']
def _roll_die(sides):
print('rolling a die')
return randint(1, int(sides))
@event_received.connect
def echo(sender, **kwargs):
"""Logs events to the console."""
print(kwargs['event']._raw)
return True
@message_received.connect
def my_name(sender, **kwargs):
"""Responds to mentions of the bot's name."""
try:
event = kwargs['event']
if re.search('deep thought', event.text, re.IGNORECASE):
sender.slack_client.send_message('That\'s my name. :grin:', event.channel)
except:
pass
@message_received.connect
def spock(sender, **kwargs):
"""Mr. Spock"""
try:
event = kwargs['event']
if re.search('spock', event.text, re.IGNORECASE):
sender.slack_client.send_message('https://goo.gl/7wBOIB', event.channel)
except:
pass
@message_received.connect
def meaning_of_life(sender, **kwargs):
"""Explains the meaning of life."""
try:
event = kwargs['event']
if re.search('meaning of life', event.text, re.IGNORECASE):
sender.slack_client.send_message('42', event.channel)
except:
pass
@message_received.connect
def dice(sender, **kwargs):
"""Rolls a die.
Usage: `roll(d12)`.
"""
try:
event = kwargs['event']
regex = re.compile('roll\(d(\d{1,4})\)')
s = regex.search(event.text)
if s is not None:
print(s)
sides = int(s.groups()[0])
print('SIDES: {}'.format(sides))
if sides > 1 and sides < 9999:
output = 'I rolled a {}-sided die and got {}'.format(
str(sides),
str(_roll_die(sides))
)
sender.slack_client.send_message(output, event.channel)
except:
pass
@message_received.connect
def weather(sender, **kwargs):
"""Posts the weather report by US zip code.
Usage: weather(12345)
"""
try:
event = kwargs['event']
regex = re.compile('weather\((\d{5})\)')
w = regex.search(event.text)
if w is not None:
zipcode = w.groups()[0]
q = 'http://api.wunderground.com/api/{key}/geolookup/q/{zipcode}.json'.format(
key=WUNDERGROUND_API_KEY,
zipcode=zipcode
)
res = json.loads(requests.get(q).text)
weather_path = res['location']['requesturl'].split('/')
state = weather_path[-2]
city = weather_path[-1]
weather_url = 'http://api.wunderground.com/api/{key}/forecast/q/{state}/{city}.json'.format(
key=WUNDERGROUND_API_KEY,
state=state,
city=city
)
weather_data = json.loads(requests.get(weather_url).text)
# Uncomment the next line to view JSON
# print(weather_data)
forecast_title = weather_data['forecast']['txt_forecast']['forecastday'][0]['title']
fcttext = weather_data['forecast']['txt_forecast']['forecastday'][0]['fcttext']
forecast_icon = weather_data['forecast']['txt_forecast']['forecastday'][0]['icon_url']
output = '*{}:* {} \n{}'.format(forecast_title, fcttext, forecast_icon)
sender.slack_client.send_message(output, event.channel)
except:
pass
@message_received.connect
def some_message(sender, **kwargs):
"""Responds to mentions of keyboard cat."""
try:
event = kwargs['event']
if 'keyboard cat' in event.text:
sender.slack_client.send_message('https://youtu.be/J---aiyznGQ', event.channel)
except:
pass
@message_received.connect
def cowsay(sender, **kwargs):
"""Cowsay API integration."""
try:
event = kwargs['event']
regex = re.compile('cowsay\((.*)\)')
m = regex.search(event.text)
if m is not None:
text = m.groups()[0].replace(' ', '%20')
q = 'http://cowsay.morecode.org/say?message={}&format=text'.format(text)
output = requests.get(q).text
sender.slack_client.send_message(output, event.channel)
except:
pass
@message_received.connect
def raise_hands(sender, **kwargs):
"""Responds to smiley faces."""
try:
event = kwargs['event']
if ':simple_smile:' in event.text:
sender.slack_client.send_message(':raised_hands:', event.channel)
except:
pass
@message_received.connect
def process_message(sender, **kwargs):
"""Sends commands to the command_router if prefixed with `!`."""
try:
event = kwargs['event']
if event.text.startswith('!'):
command_router(event.text[1:], sender, event.channel)
except:
pass
def zen(sender, channel):
"""Displays the Zen of Python."""
try:
the_zen_of_python = """The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""
sender.slack_client.send_message(the_zen_of_python, channel)
except:
pass
def command_router(command, sender, channel):
"""Routes commands."""
commands = {
'importthis': zen
}
command_fn = commands.get(command)
if command_fn:
command_fn(sender, channel)