Post to Slack Channel

I have not found anything in the actions directory, has anyone ever found a way to post text from Drafts to a Slack channel?

Editing to add: Right now I just have an action that copies the draft text, and runs a shortcut that opens Slack to a place where I can choose channel/person. Once I do that I just paste.

You can go to Slack API: Applications | Slack and create a new app. Then create an Incoming Webhook for the channel you want to post to, which will generate a URL. Then you can POST to that URL using the Drafts

let http = HTTP.create();

let response = http.request({
  "url": "https://hooks.slack.com/services/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",
  "method": "POST",
  "data": {
    "text":"Hello from Drafts!"
  },
  "headers": {
    "HeaderName": "HeaderValue"
  }
});

if (response.success) {
  var text = response.responseText;
  var data = response.responseData;
}
else {
  console.log(response.statusCode);
  console.log(response.error);
}
2 Likes

Thank you! Will be doing this tomorrow morning.

1 Like

Just jumping in here, I’ve used this method before, and it works pretty well once the webhook is set up correctly. Just make sure the channel permissions are right on the Slack side so the messages go through. Let us know how it goes!

I am going to get back it thanks to this post! I just did a quick copy/paste of what is above and I get success message in Draft but nothing showed in Slack. Didn’t take that much time to troubleshoot though. It might be sending no text!

can @Karen or @scaba share the actual action?

I don’t have an action for this. But the code I posted above, while minimal, should send a message to the Slack channel your URL is configured for. Note you will need to replace the URL with the URL you get from the Slack API console. You will also first need to create a Slack app

You can test your Slack webhook with cURL:

curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' \
https://hooks.slack.com/services/Txxxxxxxx/Bxxxxxxxxxx/G7mPqL2xZnR8sVfU4tKyJbW3

Thanks I actually already had done the curl request and it worked. Will keep working at it, thank you!

Next problem will be it shows it comes from the app I created not a user!