-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathslackbot_alert.py
47 lines (37 loc) · 1.58 KB
/
slackbot_alert.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
import time
import os
from dotenv import load_dotenv
from slackclient import SlackClient
from analysis.file_utils import get_current_inventory
# load ENV from .env
load_dotenv()
CHANNEL = os.environ.get("CHANNEL")
ALERT_DELAY = int(os.environ.get("ALERT_DELAY"))
MIN_BEER_THRESHOLD = int(os.environ.get("MIN_BEER_THRESHOLD"))
SLACK_BOT_OATH_TOKEN = os.environ.get("SLACK_BOT_OAUTH_TOKEN")
# initialize slack client and bot
slack_client = SlackClient(SLACK_BOT_OATH_TOKEN)
beerbot_id = None
if __name__ == "__main__":
if slack_client.rtm_connect(with_team_state=False):
print("BeerBot (alert) connected and running!")
# read bot user ID (call Web API method `auth.test`)
beerbot_id = slack_client.api_call("auth.test")["user_id"]
while True:
# check current inventory
current_inventory = get_current_inventory()
if current_inventory:
_timestamp, count = current_inventory
print("[{}] Current inventory - {}".format(time.strftime('%Y-%m-%dT%H:%M:%SZ', _timestamp), count))
if count < MIN_BEER_THRESHOLD:
slack_client.api_call(
"chat.postMessage",
channel=CHANNEL,
text="@channel We're running out of beer! There are less than {} bottles left".format(
MIN_BEER_THRESHOLD
),
link_names=1,
)
time.sleep(ALERT_DELAY)
else:
print("Connection failed. Exception traceback printed above.")