diff --git a/openpype/modules/slack/README.md b/openpype/modules/slack/README.md index a189fdf978..baf0f9a1ec 100644 --- a/openpype/modules/slack/README.md +++ b/openpype/modules/slack/README.md @@ -46,4 +46,5 @@ Example of message content: ```{SUBSET} for {Asset} was published.``` Integration can upload 'thumbnail' file (if present in instance), for that bot must be -manually added to target channel by Slack admin! \ No newline at end of file +manually added to target channel by Slack admin! +(In target channel write: ```/invite @OpenPypeNotifier``) \ No newline at end of file diff --git a/openpype/modules/slack/plugins/publish/integrate_slack_api.py b/openpype/modules/slack/plugins/publish/integrate_slack_api.py index a2a1ce4d55..371e14214f 100644 --- a/openpype/modules/slack/plugins/publish/integrate_slack_api.py +++ b/openpype/modules/slack/plugins/publish/integrate_slack_api.py @@ -1,28 +1,19 @@ import os -try: - from slackclient import SlackClient - python2 = True -except ImportError: - python2 = False - from slack_sdk import WebClient - from slack_sdk.errors import SlackApiError - +import six import pyblish.api +import copy + from openpype.lib.plugin_tools import prepare_template_data class IntegrateSlackAPI(pyblish.api.InstancePlugin): """ Send message notification to a channel. - Triggers on instances with "slack" family, filled by 'collect_slack_family'. - Expects configured profile in Project settings > Slack > Publish plugins > Notification to Slack. - If instance contains 'thumbnail' it uploads it. Bot must be present in the target channel. - Message template can contain {} placeholders from anatomyData. """ order = pyblish.api.IntegratorOrder + 0.499 @@ -34,32 +25,34 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin): def process(self, instance): message_templ = instance.data["slack_message"] + fill_data = copy.deepcopy(instance.context.data["anatomyData"]) + self.log.debug("fill_data {}".format(fill_data)) fill_pairs = ( - ("project_name", instance.data["anatomyData"]["project"]["name"]), - ("project_code", instance.data["anatomyData"]["project"]["code"]), - ("asset", instance.data["anatomyData"]["asset"]), - ("subset", instance.data["anatomyData"]["subset"]), - ("task", instance.data["anatomyData"]["task"]), - ("username", instance.data["anatomyData"]["username"]), - ("app", instance.data["anatomyData"]["app"]), - ("family", instance.data["anatomyData"]["family"]), - ("version", str(instance.data["anatomyData"]["version"])), + ("asset", fill_data["asset"]), + ("subset", fill_data.get("subset", instance.data["subset"])), + ("task", fill_data.get("task")), + ("username", fill_data.get("username")), + ("app", fill_data.get("app")), + ("family", fill_data.get("family", instance.data["family"])), + ("version", str(fill_data.get("version"))), ) - message = None + self.log.debug("fill_pairs {}".format(fill_pairs)) + multiple_case_variants = prepare_template_data(fill_pairs) + fill_data.update(multiple_case_variants) + self.log.debug("fill_data upd {}".format(fill_data)) + try: - message = message_templ.format( - **prepare_template_data(fill_pairs)) + message = message_templ.format(**fill_data) except Exception: self.log.warning( "Some keys are missing in {}".format(message_templ), exc_info=True) - - self.log.debug("message:: {}".format(message)) + return published_path = self._get_thumbnail_path(instance) for channel in instance.data["slack_channel"]: - if python2: + if six.PY2: self._python2_call(instance.data["slack_token"], channel, message, @@ -74,8 +67,8 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin): """Returns abs url for thumbnail if present in instance repres""" published_path = None for repre in instance.data['representations']: + self.log.debug("repre ::{}".format(repre)) if repre.get('thumbnail') or "thumbnail" in repre.get('tags', []): - repre_files = repre["files"] if isinstance(repre_files, (tuple, list, set)): filename = repre_files[0] @@ -89,6 +82,7 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin): return published_path def _python2_call(self, token, channel, message, published_path): + from slackclient import SlackClient try: client = SlackClient(token) if not published_path: @@ -114,6 +108,8 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin): self.log.warning("Error happened: {}".format(error_str)) def _python3_call(self, token, channel, message, published_path): + from slack_sdk import WebClient + from slack_sdk.errors import SlackApiError try: client = WebClient(token=token) if not published_path: @@ -139,5 +135,4 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin): # the channel msg = " - application must added to channel '{}'.".format(channel) error_str += msg + " Ask Slack admin." - return error_str diff --git a/openpype/settings/defaults/project_settings/slack.json b/openpype/settings/defaults/project_settings/slack.json index 8453945a5e..853ef4bed7 100644 --- a/openpype/settings/defaults/project_settings/slack.json +++ b/openpype/settings/defaults/project_settings/slack.json @@ -1,16 +1,20 @@ { - "token": "", + "token": "xoxb-2134660737317-2122995982087-cALVXgj3mpBhdf2nO36cdWok", "publish": { "CollectSlackFamilies": { "enabled": true, "optional": true, "profiles": [ { - "families": [], - "hosts": [], + "families": [ + "workfile" + ], "tasks": [], - "channel": [], - "message": "" + "hosts": [], + "channel": [ + "things" + ], + "message": "{Asset} was publishet {subset} with {thumbnail}" } ] } diff --git a/openpype/settings/defaults/system_settings/modules.json b/openpype/settings/defaults/system_settings/modules.json index f759546dca..1b74b4695c 100644 --- a/openpype/settings/defaults/system_settings/modules.json +++ b/openpype/settings/defaults/system_settings/modules.json @@ -169,6 +169,6 @@ "enabled": true }, "slack": { - "enabled": true + "enabled": false } } \ No newline at end of file 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 7479924d36..83c8ab0812 100644 --- a/openpype/settings/entities/schemas/projects_schema/schema_project_slack.json +++ b/openpype/settings/entities/schemas/projects_schema/schema_project_slack.json @@ -16,16 +16,17 @@ "key": "publish", "label": "Publish plugins", "children": [ + { + "type": "label", + "label": "Fill combination of families, task names and hosts when to send notification" + }, { "type": "dict", "key": "CollectSlackFamilies", "label": "Notification to Slack", "use_label_wrap": true, + "checkbox_key": "enabled", "children": [ - { - "type": "label", - "label": "" - }, { "type": "boolean", "key": "enabled", @@ -41,6 +42,7 @@ "collapsible": true, "key": "profiles", "label": "Profiles", + "use_label_wrap": true, "object_type": { "type": "dict", "children": [