-
Notifications
You must be signed in to change notification settings - Fork 0
/
veganbot.py
66 lines (54 loc) · 1.95 KB
/
veganbot.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
# follow-along with https://www.fullstackpython.com/blog/build-first-slack-bot-python.html
import os
import time
import re
from slackclient import SlackClient
from db_table import pick_facts
# instantiate Slack client
slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
# insert after the bot is started up
veganbot_id = None
RTM_READ_DELAY = 1
FACT_COMMAND = 'fact please'
RESOURCE_COMMAND = 'resources please'
MENTION_REGEX = "^<@(|[WU].+?)>(.*)"
# parse events from Slack RTM API to locate bot commands
def parse_bot_commands(slack_events):
for event in slack_events:
if event['type'] == 'message' and not 'subtype' in event:
user_id, message = parse_direct_mention(event['text'])
if user_id == veganbot_id:
return (message, event['channel'])
return (None, None)
# locates direct mentions and returns user ID of mentioned
def parse_direct_mention(message_text):
matches = re.search(MENTION_REGEX, message_text)
return (matches.group(1), matches.group(2).strip()) if matches else (None, None)
# executes bot command
def handle_command(command, channel):
# default response
default_response = f'Sorry there. Not sure what you mean. Try *{FACT_COMMAND}*'
# find and executes the command
response = None
if command.startswith(FACT_COMMAND):
# query the database and grab a random fact
response = pick_facts()
if command.startswith(RESOURCE_COMMAND):
response = 'resources'
# sends the response back to the channel
slack_client.api_call(
'chat.postMessage',
channel=channel,
text=response or default_response
)
if __name__ == '__main__':
if slack_client.rtm_connect(with_team_state=False):
print('Nice Vegan Bot is connected and running!')
veganbot_id = slack_client.api_call('auth.test')['user_id']
while True:
command, channel = parse_bot_commands(slack_client.rtm_read())
if command:
handle_command(command, channel)
time.sleep(RTM_READ_DELAY)
else:
print('Connection failed. Exception traceback printed above.')