-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
101 lines (87 loc) · 3.97 KB
/
app.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
import os
import pprint
import requests
import re
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
# FIRSTMN.CSA website data
# WebForm data
# Form name - cardForm
# submit button name - submit
# title - type string - used as title for the issue
# teamNumber - type int - used as the team number with the issue
# frcEvent - string - used to identify which event has the team with an issue
# problemCategory - type list of strings - need mapping if possible, future item
# priority - type list of strings - set to medium for now
# description - type long string - used for additional details, drop all text in here for now
# attachments - binary data - used for additional contextual information in form of photos, video, etc...
# contactName - string - used to indicate who is reporting the issue
# contactEmail - string - used to indicate who is reporting the issue
firstmncsa = {}
# Grab the environment variables needed
frcEventName = os.environ['FRC_EVENT_NAME']
firstmncsa['api_key'] = os.environ['FIRSTMNCSA_API_KEY']
firstmncsa['url'] = os.environ['FIRSTMNCSA_URL']
firstmncsa['api_endpoint'] = os.environ['FIRSTMNCSA_API_ENDPOINT']
# Install the Slack app and get xoxb- token in advance
app = App(token=os.environ["SLACK_BOT_TOKEN"])
#@app.message("hello")
#def message_hello(message, say):
# # say() sends a message to the channel where the event was triggered
# say(f"Hey there <@{message['user']}>!")
# Function to grab data from blocks.
def get_block_text(block):
pprint.pp(block)
return str(block['text']['text'])
# Look for CSA Requests
@app.message('')
def message_hello(message, say):
pprint.pp(message)
# Check to see if the message was sent by a bot
if ('subtype','bot_message') in message.items():
print("Message from a bot: {}".format(message['bot_id']))
if "requested help" in message['text']:
# Prep the requests object used to post this to the firstmn.csa web form
webform = {
'title': message['text'],
'teamNumber': re.search(r"\d+",message['text']).group(),
'frcEvent': frcEventName,
'priority': 'Medium',
'description': "\n".join(list(map(get_block_text, message['blocks']))),
'contactName': 'Nexus - FTA',
'contactEmail': '[email protected]',
'problemCategory': 'Other or not sure',
'attachments': []
}
elif "FTA request" in message['text']:
# Prep the requests object used to post this to the firstmn.csa web form
webform = {
'title': message['text'],
'teamNumber': re.search(r"\d+",message['text']).group(),
'frcEvent': frcEventName,
'priority': 'Medium',
'description': "\n".join(list(map(get_block_text, message['blocks']))),
'contactName': 'Nexus - FTA',
'contactEmail': '[email protected]',
'problemCategory': 'Other or not sure',
'attachments': []
}
else:
print("Unrecognized message, please tell Chris");
return();
headers = {'Content-type': 'application/json',
'API-Key': firstmncsa['api_key']}
print("Posting form to URL: {}".format(firstmncsa['api_endpoint']))
pprint.pp(webform)
# Post the data to the First MN CSA
resp = requests.post(url=firstmncsa['url'], headers=headers, json=webform)
# How did that go?
print("Response from web form submission: %s" % resp.text)
# Let the world know it was submitted.
say("Message report status: {}".format(resp.text))
else:
# say() sends a message to the channel where the event was triggered
say(f"USER: <@{message['user']}>!")
say ("Message was not from Nexus, ignoring.")
if __name__ == "__main__":
SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()