diff --git a/openpype/modules/slack/plugins/publish/collect_slack_family.py b/openpype/modules/slack/plugins/publish/collect_slack_family.py index fefc0c8f56..2110c0703b 100644 --- a/openpype/modules/slack/plugins/publish/collect_slack_family.py +++ b/openpype/modules/slack/plugins/publish/collect_slack_family.py @@ -30,7 +30,6 @@ class CollectSlackFamilies(pyblish.api.InstancePlugin): profile = filter_profiles(self.profiles, key_values, logger=self.log) - self.log.debug("profile ::{}".format(profile)) # make slack publishable if profile: if instance.data.get('families'): @@ -38,8 +37,8 @@ class CollectSlackFamilies(pyblish.api.InstancePlugin): else: instance.data['families'] = ['slack'] - instance.data["slack_channel"] = profile["channel"] - instance.data["slack_message"] = profile["message"] + instance.data["slack_channel_message_profiles"] = \ + profile["channel_messages"] slack_token = (instance.context.data["project_settings"] ["slack"] diff --git a/openpype/modules/slack/plugins/publish/integrate_slack_api.py b/openpype/modules/slack/plugins/publish/integrate_slack_api.py index d3f0f36140..ccd00dc1c8 100644 --- a/openpype/modules/slack/plugins/publish/integrate_slack_api.py +++ b/openpype/modules/slack/plugins/publish/integrate_slack_api.py @@ -23,8 +23,30 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin): optional = True def process(self, instance): - message_templ = instance.data["slack_message"] + published_path = self._get_thumbnail_path(instance) + for message_profile in instance.data["slack_channel_message_profiles"]: + message = self._get_filled_message(message_profile["message"], + instance) + if not message: + return + + for channel in message_profile["channels"]: + if six.PY2: + self._python2_call(instance.data["slack_token"], + channel, + message, + published_path, + message_profile["upload_thumbnail"]) + else: + self._python3_call(instance.data["slack_token"], + channel, + message, + published_path, + message_profile["upload_thumbnail"]) + + def _get_filled_message(self, message_templ, instance): + """Use message_templ and data from instance to get message content.""" fill_data = copy.deepcopy(instance.context.data["anatomyData"]) fill_pairs = ( @@ -40,27 +62,15 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin): multiple_case_variants = prepare_template_data(fill_pairs) fill_data.update(multiple_case_variants) + message = None try: message = message_templ.format(**fill_data) except Exception: self.log.warning( "Some keys are missing in {}".format(message_templ), exc_info=True) - return - published_path = self._get_thumbnail_path(instance) - - for channel in instance.data["slack_channel"]: - if six.PY2: - self._python2_call(instance.data["slack_token"], - channel, - message, - published_path) - else: - self._python3_call(instance.data["slack_token"], - channel, - message, - published_path) + return message def _get_thumbnail_path(self, instance): """Returns abs url for thumbnail if present in instance repres""" @@ -79,11 +89,13 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin): break return published_path - def _python2_call(self, token, channel, message, published_path): + def _python2_call(self, token, channel, message, + published_path, upload_thumbnail): from slackclient import SlackClient try: client = SlackClient(token) - if published_path and os.path.exists(published_path): + if upload_thumbnail and \ + published_path and os.path.exists(published_path): with open(published_path, 'rb') as pf: response = client.api_call( "files.upload", @@ -108,12 +120,14 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin): error_str = self._enrich_error(str(e), channel) self.log.warning("Error happened: {}".format(error_str)) - def _python3_call(self, token, channel, message, published_path): + def _python3_call(self, token, channel, message, + published_path, upload_thumbnail): from slack_sdk import WebClient from slack_sdk.errors import SlackApiError try: client = WebClient(token=token) - if published_path and os.path.exists(published_path): + if upload_thumbnail and \ + published_path and os.path.exists(published_path): _ = client.files_upload( channels=channel, initial_comment=message, diff --git a/openpype/settings/defaults/project_settings/slack.json b/openpype/settings/defaults/project_settings/slack.json index 853ef4bed7..e70ef77fd2 100644 --- a/openpype/settings/defaults/project_settings/slack.json +++ b/openpype/settings/defaults/project_settings/slack.json @@ -1,20 +1,15 @@ { - "token": "xoxb-2134660737317-2122995982087-cALVXgj3mpBhdf2nO36cdWok", + "token": "", "publish": { "CollectSlackFamilies": { "enabled": true, "optional": true, "profiles": [ { - "families": [ - "workfile" - ], + "families": [], "tasks": [], "hosts": [], - "channel": [ - "things" - ], - "message": "{Asset} was publishet {subset} with {thumbnail}" + "channel_messages": [] } ] } diff --git a/openpype/settings/entities/schemas/projects_schema/schema_project_slack.json b/openpype/settings/entities/schemas/projects_schema/schema_project_slack.json index 83c8ab0812..532aa083b3 100644 --- a/openpype/settings/entities/schemas/projects_schema/schema_project_slack.json +++ b/openpype/settings/entities/schemas/projects_schema/schema_project_slack.json @@ -68,16 +68,32 @@ "type": "separator" }, { + "key": "channel_messages", + "label": "Messages to channels", "type": "list", - "object_type": "text", - "key": "channel", - "label": "Channel" - }, - { - "type": "text", - "multiline": true, - "key": "message", - "label": "Message" + "use_label_wrap": true, + "object_type": { + "type": "dict", + "children": [ + { + "type": "list", + "object_type": "text", + "key": "channels", + "label": "Channels" + }, + { + "type": "boolean", + "key": "upload_thumbnail", + "label": "Upload thumbnail" + }, + { + "type": "text", + "multiline": false, + "key": "message", + "label": "Message" + } + ] + } } ] } diff --git a/website/docs/assets/slack_project.png b/website/docs/assets/slack_project.png index 496013800f..417cdab2de 100644 Binary files a/website/docs/assets/slack_project.png and b/website/docs/assets/slack_project.png differ diff --git a/website/docs/module_slack.md b/website/docs/module_slack.md index e993f36965..0a3d542393 100644 --- a/website/docs/module_slack.md +++ b/website/docs/module_slack.md @@ -48,20 +48,23 @@ Eg. If I want to be notified when render is published from Maya, setting is: - family: 'render' - host: 'Maya' -### Channel -Message could be delivered to one or multiple channels, by default app allows Slack bot +### Messages to channels + +#### Channels +Multiple messages could be delivered to one or multiple channels, by default app allows Slack bot to send messages to 'public' channels (eg. bot doesn't need to join the channel first). ![Configure module](assets/slack_system.png) +#### Upload thumbnail Integration can upload 'thumbnail' file (if present in instance), for that bot must be manually added to target channel by Slack admin! (In target channel write: ```/invite @OpenPypeNotifier``) -### Message +#### Message Message content can use Templating (see https://openpype.io/docs/admin_settings_project_anatomy/#available-template-keys). -Pre selected set of keys could be used in lowercase, Capitalized or UPPERCASE format, values will be modified accordingly. +Pre-selected set of keys could be used in lowercase, Capitalized or UPPERCASE format, values will be modified accordingly. ({Asset} >> "Asset", {FAMILY} >> "RENDER") **Available keys:**