1- """Initialize slack client with values provided by the config file"""
2-
3- import json
4-
51import slack
6-
72from mapswipe_workers .definitions import logger
3+ from mapswipe_workers import auth
84from mapswipe_workers .config import SLACK_CHANNEL , SLACK_TOKEN
95
106
117def send_slack_message (message_type : str , project_name : str , project_id : str = None ):
12-
8+ """Initialize slack client with values provided in environment."""
139 if SLACK_TOKEN is None or SLACK_CHANNEL is None :
1410 logger .info (
1511 "No configuration for Slack was found. "
@@ -27,14 +23,58 @@ def send_slack_message(message_type: str, project_name: str, project_id: str = N
2723 + "Make sure to activate the project using the manager dashboard.\n "
2824 + "Happy Swiping. :)"
2925 )
26+ slack_client .chat_postMessage (channel = SLACK_CHANNEL , text = message )
3027 elif message_type == "fail" :
3128 message = (
3229 "### PROJECT CREATION FAILED ###\n "
3330 + "Project Name: {0}\n " .format (project_name )
3431 + "Project draft is deleted."
3532 )
33+ slack_client .chat_postMessage (channel = SLACK_CHANNEL , text = message )
34+ elif message_type == "notification_90" :
35+ message = (
36+ "### ALMOST THERE! PROJECT REACHED 90% ###\n "
37+ + "Project Name: {0}\n " .format (project_name )
38+ + "Project Id: {0}\n \n " .format (project_id )
39+ + "Get your next projects ready."
40+ )
41+ slack_client .chat_postMessage (channel = "mapswipe_managers" , text = message )
42+ elif message_type == "notification_100" :
43+ message = (
44+ "### GREAT! PROJECT REACHED 100% ###\n "
45+ + "Project Name: {0}\n " .format (project_name )
46+ + "Project Id: {0}\n \n " .format (project_id )
47+ + "You can set this project to 'finished' "
48+ + "and activate another one."
49+ )
50+ slack_client .chat_postMessage (channel = "mapswipe_managers" , text = message )
3651 else :
3752 # TODO: Raise an Exception
3853 pass
3954
40- slack_client .chat_postMessage (channel = SLACK_CHANNEL , text = message )
55+
56+ def send_progress_notification (project_id : int ):
57+ """Send progress notification to project managers in Slack."""
58+ fb_db = auth .firebaseDB ()
59+ progress = fb_db .reference (f"v2/projects/{ project_id } /progress" ).get ()
60+ project_name = fb_db .reference (f"v2/projects/{ project_id } /name" ).get ()
61+ notification_90_sent = fb_db .reference (
62+ f"v2/projects/{ project_id } /notification_90_sent"
63+ ).get ()
64+ notification_100_sent = fb_db .reference (
65+ f"v2/projects/{ project_id } /notification_100_sent"
66+ ).get ()
67+ logger .info (
68+ f"{ project_id } - progress: { progress } ,"
69+ f"notifications: { notification_90_sent } { notification_100_sent } "
70+ )
71+
72+ if progress >= 90 and not notification_90_sent :
73+ # send notification and set value in firebase
74+ fb_db .reference (f"v2/projects/{ project_id } /notification_90_sent" ).set (True )
75+ send_slack_message ("notification_90" , project_name , project_id )
76+
77+ if progress >= 100 and not notification_100_sent :
78+ # send notification and set value in firebase
79+ fb_db .reference (f"v2/projects/{ project_id } /notification_100_sent" ).set (True )
80+ send_slack_message ("notification_100" , project_name , project_id )
0 commit comments