Step 4 - Authorize an App
Enable the helloSlack
action
In src/index.js
, uncomment the import of ./helloSlack
.
import "./helloWorld";
import "./helloSlack";
// import "./basicWebhook";
// import "./githubToSlack";
Take a look at the helloSlack
action
There are a few more things happening here compared to our helloWorld
example.
import { defineAction } from "@runlightyear/lightyear";
import { Slack } from "@runlightyear/slack";
defineAction({
name: "helloSlack",
title: "Hello Slack"
apps: ["slack"],
run: async ({ auths }) => {
const slack = new Slack({ auth: auths.slack });
await slack.postMessage({
channel: "#general", // <-- you might want to change this!
text: "Hello Slack!",
});
console.info("Posted message to Slack");
},
});
First, we've imported the Slack
connector.
import { defineAction } from "@runlightyear/lightyear";
import { Slack } from "@runlightyear/slack";
...
Next, we've identified that we want to connect to the Slack
app using the apps
property.
...
defineAction({
name: "helloSlack",
title: "Hello Slack",
apps: ["slack"],
...
});
Then we're using auths
to instantiate a Slack
connector.
defineAction({
...
run: async ({ auths }) => {
const slack = new Slack({ auth: auths.slack });
await slack.postMessage({
channel: "#general", // <-- you might want to change this!
text: "Hello Slack!",
});
console.info("Posted message to Slack");
},
});
And finally we're using that connector instance to post a message to the #general
channel in Slack. You should select an appropriate channel so you don't irritate or confuse your co-workers! 🙂
defineAction({
...
run: async ({ auths }) => {
const slack = new Slack({ auth: auths.slack });
await slack.postMessage({
channel: "#general", // <-- you might want to change this!
text: "Hello Slack!",
});
console.info("Posted message to Slack");
},
});
Authorize an app
Let's go back to the dashboard (opens in a new tab) and click on Apps
and then click Slack
.
If you don't have a Slack account set up already or you are not authorized to add apps to your existing account, you can create a new account very easily and quickly. Click here to create a new Slack account (opens in a new tab).
Click on the Authorize
button for Slack.
This will take you through a standard OAuth flow. Complete the flow and you will return to see that you have now successfully authorized access to your Slack account.
We can share this auth with the production environment, which will come in handy in a few minutes.
Run the helloSlack
action
Click on the helloSlack
card to go to the action page
Then click the Trigger
button ...
... and see the result.
See the result in Slack
Take a look at the channel you posted the message to and you should see the result: