forked from vishwa35/slackbot-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduled.py
37 lines (30 loc) · 1.03 KB
/
scheduled.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
import os
import schedule
import time
import logging
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
logging.basicConfig(level=logging.DEBUG)
def sendMessage(slack_client, msg):
# make the POST request through the python slack client
# check if the request was a success
try:
slack_client.chat_postMessage(
channel='#test',
text=msg
)#.get()
except SlackApiError as e:
logging.error('Request to Slack API Failed: {}.'.format(e.response.status_code))
logging.error(e.response)
if __name__ == "__main__":
SLACK_BOT_TOKEN = os.environ['SLACK_BOT_TOKEN']
slack_client = WebClient(SLACK_BOT_TOKEN)
logging.debug("authorized slack client")
# # For testing
msg = "Good Morning!"
schedule.every(60).seconds.do(lambda: sendMessage(slack_client, msg))
# schedule.every().monday.at("13:15").do(lambda: sendMessage(slack_client, msg))
logging.info("entering loop")
while True:
schedule.run_pending()
time.sleep(5) # sleep for 5 seconds between checks on the scheduler