slackapi / deno-slack-sdk

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

How to send a HTTP request in slack custom function #322

Open zcin opened 4 months ago

zcin commented 4 months ago

The deno-slack versions

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

Deno runtime version

deno 1.42.4 (release, aarch64-apple-darwin) v8 12.3.219.9 typescript 5.4.3

OS info

ProductName:            macOS
ProductVersion:         14.1
BuildVersion:           23B74
Darwin Kernel Version 23.1.0: Mon Oct  9 21:27:24 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6000

Steps to reproduce

I want to send an HTTP request inside my custom function invoked in my app workflow. I am running the following code:

    const response = await fetch("http://localhost:8000");

My slack.json:

{
  "hooks": {
    "get-manifest": "deno run -q --config=deno.jsonc --allow-read --allow-net --allow-env --allow-sys https://deno.land/x/deno_slack_hooks@1.3.0/get_manifest.ts",
    "get-trigger": "deno run -q --config=deno.jsonc --allow-read --allow-net --allow-env https://deno.land/x/deno_slack_hooks@1.3.0/get_trigger.ts",
    "get-hooks": "deno run -q --allow-run --allow-read --allow-net https://deno.land/x/deno_slack_hooks@1.3.0/mod.ts",
    "build": "deno run -q --config=deno.jsonc --allow-read --allow-write --allow-net --allow-run --allow-env --allow-sys https://deno.land/x/deno_slack_hooks@1.3.0/build.ts",
    "start": "deno run -q --config=deno.jsonc --allow-read --allow-net --allow-run --allow-env https://deno.land/x/deno_slack_runtime@1.1.1/local-run.ts"
  }
}

Expected result

I expect the HTTP request to be sent successfully, since I added allow-net in slack.json. However I'm not sure if I added it correctly, or where is the correct place to add --allow-net.

Actual result

error: Uncaught (in promise) PermissionDenied: Detected missing network permissions; add the domain to your manifest's `outgoingDomains`. Original message: Requires net access to "localhost:8000", run again with the --allow-net flag
    const response = await fetch("http://localhost:8000");
                           ^
    at mainFetch (ext:deno_fetch/26_fetch.js:152:43)
    at ext:deno_fetch/26_fetch.js:352:9
    at new Promise (<anonymous>)
    at fetch (ext:deno_fetch/26_fetch.js:315:18)
    at AsyncFunction.<anonymous> (file:///Users/cindyz/asproj/functions/sample_function.ts:78:28)
    at Module.handlerModule (https://deno.land/x/deno_slack_sdk@2.11.0/functions/slack-function.ts:44:28)
    at Object.RunFunction [as function_executed] (https://deno.land/x/deno_slack_runtime@1.1.1/run-function.ts:29:13)
    at DispatchPayload (https://deno.land/x/deno_slack_runtime@1.1.1/dispatch-payload.ts:79:49)
    at async runLocally (https://deno.land/x/deno_slack_runtime@1.1.1/local-run-function.ts:36:16)
    at async https://deno.land/x/deno_slack_runtime@1.1.1/local-run-function.ts:55:3
zimeg commented 4 months ago

Hey @zcin! 👋 Deno requires explicit permission to access external resources, which is exactly what you're seeing with the "missing network permissions". The --allow-net flag is often the answer to this, but the CLI and SDK work together to configure this on startup automatically.

To make HTTP requests to external domains, you'll have to add the domain to your app manifest as an outgoing domain:

export default Manifest({
  name: "example-app",
  outgoingDomains: [
    "localhost",
  ],
});

This should update permissions so that requests can be made to localhost:8000, but please let me know if something still isn't working.

Also want to callout that I believe localhost will only work in a local run context since apps deployed to Slack infrastructure don't have a localhost:8000. No worries if this isn't a problem, but a public endpoint is needed otherwise or this error will appear:

Localhost is only a valid outgoing domain for local apps. (domain_blocked)