-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_integration.py
36 lines (31 loc) · 1.62 KB
/
slack_integration.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
import requests
import os
from logic import OPEN_TIME, CLOSE_TIME, TIMEZONE
def check_slack() -> bool:
'''
checks if slack integration is configured. Returns True and prints a helpful message about reporting times if it is configured. Otherwise it returns False and prints a helpful message about configuring slack
'''
webhook_url = os.environ.get("SLACK_WEBHOOK_URL")
if webhook_url:
print(
f"Slack integration is configured. You will receive slack messages about the door being unlocked outside of business hours {OPEN_TIME} to {CLOSE_TIME} in the {TIMEZONE} timezone. If you would like to change these times, edit the OPEN_TIME, CLOSE_TIME, and TIMEZONE variables in logic.py"
)
return True
else:
print(
"Slack integration is not configured. If you would like to receive slack messages about the door being unlocked outside of business hours, set the SLACK_WEBHOOK_URL environment variable by running `export SLACK_WEBHOOK_URL=your_webhook_url` before using this. You can learn about setting up a slack webhook at https://api.slack.com/messaging/webhooks"
)
return False
def send_slack_message(message: str):
"""
Send a message to slack at the webhook url in the environment
"""
data = {
"text": message,
}
webhook_url = os.environ.get("SLACK_WEBHOOK_URL")
if not webhook_url:
raise EnvironmentError(
"No webhook URL found. Set the SLACK_WEBHOOK_URL environment variable by running `export SLACK_WEBHOOK_URL=your_webhook_url` before using this."
)
requests.post(webhook_url, json=data)