Get Started
4 Authorize an App

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"
  description: "Send a message to 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",
  description: "Send a message to 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 Manage on Slack.

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
Apps
GitHub Icon
GitHub
github
Available
Slack Icon
Slack
slack
Available
💡

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.

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
Slack Icon
Slack
slack
Type
OAUTH2
Status
BETA
Authorization
slack
Actions
Hello Slack
helloSlack
MANUAL
Slack Icon
Webhooks
No webhooks

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.

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
Slack Icon
Slack
slack
Type
OAUTH2
Status
BETA
Authorization
slack
Shared with production
Actions
Hello Slack
helloSlack
MANUAL
Slack Icon
Webhooks
No webhooks

We can share this auth with the production environment, which will come in handy in a few minutes.

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
Slack Icon
Slack
slack
Type
OAUTH2
Status
BETA
Authorization
slack
Shared with production
Actions
Hello Slack
helloSlack
MANUAL
Slack Icon
Webhooks
No webhooks

Run the helloSlack action

Click on the helloSlack card to go to the action page

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
Slack Icon
Slack
slack
Type
OAUTH2
Status
BETA
Authorization
slack
Shared with production
Actions
Hello Slack
helloSlack
MANUAL
Slack Icon
Webhooks
No webhooks

Then click the Trigger button ...

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
helloSlack
Hello Slack
Ready
Config
Runs
Config
Apps
Slack Icon
Slack
Authorized
Variables
No variables
Secrets
No secrets

... and see the result.

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
helloSlack
Hello Slack
Ready
Config
Runs
Runs
Ran successfully at
Mar 29, 2023 5:48 AM
6779fe29-52a7-409e-81a1-40dec6e16d3b
helloSlack
Mar 29, 2023 5:48 AM
 [INFO]:
Running action helloSlack
 [INFO]:
Posted message to Slack

See the result in Slack

Take a look at the channel you posted the message to and you should see the result:

Hello Slack Screenshot