diff --git a/opentech/apply/activity/messaging.py b/opentech/apply/activity/messaging.py index 23b11f271f97a7fa9cf5fcb00dfb34ee0730a835..29c0ad9d865174320b45a13d5f8f5c2f5f73aa4a 100644 --- a/opentech/apply/activity/messaging.py +++ b/opentech/apply/activity/messaging.py @@ -225,19 +225,44 @@ class SlackAdapter(AdapterBase): return f'<{user.slack}>' return '' + def slack_channel(self, submission): + try: + target_room = submission.get_from_parent('slack_channel') + except AttributeError: + # If not a submission object, set room to default. + target_room = self.target_room + else: + if not target_room: + # If no custom room, set to default. + target_room = self.target_room + + # Make sure the channel name starts with a "#". + if target_room and not target_room.startswith('#'): + target_room = f"#{target_room}" + + return target_room + def send_message(self, message, recipient, **kwargs): - if not self.destination or not self.target_room: + try: + submission = kwargs['submission'] + except Exception: + # If no submission, set room to default. + target_room = self.target_room + else: + target_room = self.slack_channel(submission) + + if not self.destination or not target_room: errors = list() if not self.destination: errors.append('Destination URL') - if not self.target_room: + if not target_room: errors.append('Room ID') return 'Missing configuration: {}'.format(', '.join(errors)) message = ' '.join([recipient, message]).strip() data = { - "room": self.target_room, + "room": target_room, "message": message, } response = requests.post(self.destination, json=data) diff --git a/opentech/apply/activity/tests/test_messaging.py b/opentech/apply/activity/tests/test_messaging.py index aa07fb2da609f1eb71aa19b27bfa9ddb4a3d3b53..ec782ecf9cbf2f567b2e96db42c0b513dc17271d 100644 --- a/opentech/apply/activity/tests/test_messaging.py +++ b/opentech/apply/activity/tests/test_messaging.py @@ -225,7 +225,7 @@ class TestActivityAdapter(TestCase): class TestSlackAdapter(AdapterMixin, TestCase): target_url = 'https://my-slack-backend.com/incoming/my-very-secret-key' - target_room = '<ROOM ID>' + target_room = '#<ROOM ID>' @override_settings( SLACK_DESTINATION_URL=target_url,