slackapi / deno-slack-sdk

SDK for building Run on Slack apps using Deno
https://api.slack.com/automation
MIT License
158 stars 27 forks source link

[QUERY] How to distinguish between envs and workspaces #344

Closed djmgit closed 1 month ago

djmgit commented 1 month ago

Question

How to distinguish between env type (local vs deployed) and workspace from within custom functions.

Context

I have been following this: https://api.slack.com/automation/environment-variables#included

I tried getting the value of SLACK_WORKSPACE using Deno.env.get("SLACK_WORKSPACE") in manifest.ts and exporting a constant which I am trying to import and access within the custom function. However it turns out that export from manifest does not work. I am getting the following error:

        at new TypedWorkflowStepDefinition (file:///var/task/functions/issue_status.js:2707:33)
        at WorkflowDefinition.addStep (file:///var/task/functions/issue_status.js:2776:21)
        at file:///var/task/functions/issue_status.js:4152:43

What I am doing from manifest is this: export const workspace = Deno.env.get("SLACK_WORKSPACE");

Directly accessing Deno.env.get("SLACK_WORKSPACE") in custom function throws permission error. The doc says these variables are auto populated with workspace name and env but I am not sure how to access them from custom function. I want to deploy same app in two different workspaces and use different properties (third party service urls, message strings etc) from custom function depending on the workspace the app is running in. So that I can test the app first in stage workspace and then deploy to prod slack workspace.

Environment

"deno-slack-sdk/": "https://deno.land/x/deno_slack_sdk@2.14.0/", "deno-slack-api/": "https://deno.land/x/deno_slack_api@2.7.0/",

deno 1.44.4 (release, aarch64-apple-darwin) v8 12.6.228.9 typescript 5.4.5

ProductName: macOS ProductVersion: 13.6.7 BuildVersion: 22G720 Darwin Kernel Version 22.6.0: Mon Apr 22 20:51:27 PDT 2024; root:xnu-8796.141.3.705.2~1/RELEASE_ARM64_T6020

WilliamBergamin commented 1 month ago

Hi @djmgit thanks for writing in 💯

If you want to get the workspace_id (aka: team_id) or enterprise_id of the workspace where your app is installed to you can use the team_id and enterprise_id Function context properties.

Here is an example of how to use them

export default SlackFunction(
  GreetingFunctionDefinition,
  ({ inputs, team_id, enterprise_id }) => {
    console.log(team_id);
    console.log(enterprise_id);

    // Don't forget any required output parameters
    return { outputs: { } };
  },
);

Let me know if this resolves your issue

djmgit commented 1 month ago

@WilliamBergamin this is cool, I will try this out. Found this in custom functions doc as well 😅 . I was too fixated on the included env variables. But just curious whats the recommended way to use the included env vars: SLACK_WORKSPACE and SLACK_ENV? I was under the impression they get auto populated once the app is deployed.

WilliamBergamin commented 1 month ago

To access the SLACK_WORKSPACE variable you must use the env builtin argument, this allows your app to use env vars in a deployed context But I do suggest using the team_id and enterprise_idFunction context properties. The team_id value may change depending on where your app is invoked if your application is installed at the enterprise level or multiple workspaces.