-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.py
executable file
·123 lines (91 loc) · 3.4 KB
/
examples.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
import asyncio
import random
import re
import time
import slackreact as sr
class AreYouListening(sr.MessageContainsRule):
"""Check if the bot is listening.
Examples:
`are you there?`
`robots of the world, are you listening?`
"""
async def get_applicable_channels(self):
return ["random"]
async def get_query_strings(self):
return ["are you there", "are you listening"]
async def get_response_text(self, event):
return "Yes. You can't see me, but I'm right behind you."
class DieRoll(sr.MessageMatchesRegexRule):
"""Rolls dice for you.
Examples:
`can i get a d3?`
`10d6 drop lowest`
"""
regex = re.compile(r"\b(\d*)d(\d+)\b")
async def should_respond_to_channel(self, channel_id):
return True
async def get_response_text(self, event):
regex_match = await self.get_regex_match(event)
A = int(regex_match.group(1) or "1")
X = int(regex_match.group(2))
roll = sorted(random.randrange(X) + 1 for _ in range(A))
if "drop lowest" in event["text"]:
roll = roll[1:]
if "drop highest" in event["text"]:
roll = roll[:-1]
return f"Sum of {sum(roll)} from rolling: " + ", ".join(map(str, roll))
class LoveMe(sr.MessageContainsRule):
"""Gives you some love (for five minutes).
Examples:
`does anyone love me :(`
"""
async def load(self):
self.needy = {}
async def should_respond_to_channel(self, channel_id):
return channel_id.startswith("D") # only responds to DMs
async def get_query_strings(self):
return ["love me"]
async def should_respond_to_message(self, message):
if await super().should_respond_to_message(message):
self.needy[message["user"]] = float(message["ts"])
cutoff = time.time() - 5 * 60
self.needy = {k: v for k, v in self.needy.items() if v > cutoff}
return message["user"] in self.needy
async def respond(self, event):
return [
{
"method": "reactions.add",
"name": "heart",
"channel": event["channel"],
"timestamp": event["ts"],
"as_user": True,
}
]
class Email(sr.MessageContainsRule):
"""What is the email of the tagged person?
Examples:
`what is @simba's email`
"""
async def should_respond_to_channel(self, channel_id):
return True
async def get_query_strings(self):
return ["email"]
async def get_response_text(self, event):
match = re.search("<@(U\w+)>", event["text"])
if not match:
return "You have to @tag the person" if event["channel"].startswith("D") else []
user = match.group(1)
if user == self.bot.me:
return "I hope you'll excuse me, but I am a bot who values my privacy."
user_info = await self.bot.api_call(method="users.info", user=user)
return user_info.get("user", {}).get("profile", {}).get("email", "No email found :(")
def run(time=None):
"""Runs the bot for time seconds, or forever if None."""
TOKEN = "insert your token here; get one at {team}.slack.com/apps/manage/custom-integrations"
bot = sr.SlackBot(TOKEN)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(asyncio.wait_for(bot.run(), time))
except asyncio.TimeoutError:
pass
loop.close()