Skip to content

Commit

Permalink
app initilization (real)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwrzesien committed Aug 19, 2024
1 parent 7a29d8a commit e028904
Show file tree
Hide file tree
Showing 34 changed files with 1,783 additions and 20 deletions.
Binary file modified .DS_Store
Binary file not shown.
24 changes: 24 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Use the Ubtuntu base image
FROM ubuntu:22.04

# Download development tools
RUN apt-get update
RUN apt-get install -y curl
RUN apt-get install -y git
RUN apt-get install -y unzip

# Run the container as a user
RUN useradd -m -s /bin/bash slackdev
RUN chown slackdev /usr/local/bin
USER slackdev

# Install the project runtime
RUN curl -fsSL https://deno.land/install.sh | sh
RUN export DENO_INSTALL="/home/slackdev/.deno"
RUN export PATH="$DENO_INSTALL/bin:$PATH"

# Set the working directory
WORKDIR /workspaces

# Install the Slack CLI
RUN curl -fsSL https://downloads.slack-edge.com/slack-cli/install.sh | bash -s -- -d
35 changes: 35 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# .devcontainer

A [development container][container] provides a predefined environment with
some tools needed for development, which can be useful in editors such as
[Visual Studio Code][vscode] or remote settings like [Codespaces][codespaces].

This specific container packages [the Slack CLI][cli] with the project runtime
and a few development tools. The `Dockerfile` details the container.

## Editor extensions

Modifications to an editor can be made with changes to the `devcontainer.json`
file:

```diff
{
"customizations": {
"vscode": {
"extensions": [
+ "GitHub.copilot",
"denoland.vscode-deno",
"ms-azuretools.vscode-docker"
],
+ "settings": {
+ "terminal.integrated.defaultProfile.linux": "zsh"
+ }
}
}
}
```

[codespaces]: https://github.com/features/codespaces
[cli]: https://api.slack.com/automation/cli
[container]: https://containers.dev/
[vscode]: https://code.visualstudio.com/docs/devcontainers/containers
13 changes: 13 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "Slack Platform",
"dockerFile": "Dockerfile",
"remoteUser": "slackdev",
"customizations": {
"vscode": {
"extensions": [
"denoland.vscode-deno",
"ms-azuretools.vscode-docker"
]
}
}
}
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Environment variables needed to run your app. Move this file to .env to start!

# The client ID for your Google app
GOOGLE_CLIENT_ID=12345-example.apps.googleusercontent.com
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dist
package
.DS_Store
.slack/apps.dev.json
.env
3 changes: 3 additions & 0 deletions .slack/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"project_id": "238da6c8-8db2-4f4e-9a60-171600147117"
}
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.suggest.imports.hosts": {
"https://deno.land": false
},
"[typescript]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "denoland.vscode-deno"
},
"editor.tabSize": 2
}
197 changes: 189 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# Blank Template
# Simple Survey

This is a blank template used to build out automations using the Slack CLI.
This automation demonstrates workflows for requesting and collecting feedback on
messages and storing responses in a Google Sheet.

https://user-images.githubusercontent.com/18134219/215910112-68c08e0f-597d-4813-bce0-aae174289948.mp4

**Guide Outline**:

- [Included Workflows](#included-workflows)
- [Setup](#setup)
- [Install the Slack CLI](#install-the-slack-cli)
- [Clone the Template](#clone-the-template)
- [Clone the Sample App](#clone-the-sample-app)
- [Prepare Google Services](#prepare-google-services)
- [Running Your Project Locally](#running-your-project-locally)
- [Creating Triggers](#creating-triggers)
- [Datastores](#datastores)
Expand All @@ -18,6 +23,21 @@ This is a blank template used to build out automations using the Slack CLI.

---

## Included Workflows

- **Prompt survey creation**: Ask if a user wants to create a survey when a
:clipboard: reaction is added to a message
- **Create a survey**: Respond to the reacted message with a feedback form and
make a new spreadsheet to store responses
- **Respond to a survey**: Open the feedback form and store responses in the
spreadsheet
- **Remove a survey**: Delete messages with survey related link triggers
- **Event configurator**: Update the channels to survey and surveying users for
reaction events
- **Maintenance job**: A daily run workflow that ensures bot user membership in
channels specified for event reaction triggers. Recommended for
production-grade operations

## Setup

Before getting started, first make sure you have a development workspace where
Expand All @@ -27,22 +47,148 @@ project require that the workspace be part of

### Install the Slack CLI

To use this template, you need to install and configure the Slack CLI.
To use this sample, you need to install and configure the Slack CLI.
Step-by-step instructions can be found in our
[Quickstart Guide](https://api.slack.com/automation/quickstart).

### Clone the Template
### Clone the Sample App

Start by cloning this repository:

```zsh
# Clone this project onto your machine
$ slack create my-app -t slack-samples/deno-blank-template
$ slack create my-app -t slack-samples/deno-simple-survey

# Change into the project directory
$ cd my-app
```

### Prepare Google Services

With [external authentication](https://api.slack.com/automation/external-auth)
you can programmatically interact with Google services and APIs from your app,
as though you're the authorized user.

The client credentials needed for these interactions can be collected from a
Google Cloud project with OAuth enabled and with access to the appropriate
services.

#### Create a Google Cloud Project

Begin by creating a new project from
[the Google Cloud resource manager](https://console.cloud.google.com/cloud-resource-manager),
then
[enabling the Google Sheets API](https://console.cloud.google.com/apis/library/sheets.googleapis.com)
for this project.

Next,
[create an OAuth consent screen](https://console.cloud.google.com/apis/credentials/consent)
for your app. The "User Type" and other required app information can be
configured as you wish. No additional scopes need to be added here, and you can
add test users for development if you want (especially useful if one Google
account owns the Google Cloud project but you want to test the authentication
flow using a different Google account).

Client credentials can be collected by
[creating an OAuth client ID](https://console.cloud.google.com/apis/credentials/oauthclient)
with an application type of "Web application". Under the "Authorized redirect
URIs" section, add `https://oauth2.slack.com/external/auth/callback` then click
"Create".

You'll use these newly created client credentials in the next steps.

#### Set the Client ID

Start by renaming the `.env.example` file at the top level of your project to
`.env`, being sure not to commit this file to version control. This file will
store sensitive, app-specific variables that are determined by the environment
being used.

From your new Google project's dashboard, copy the **Client ID** and paste it as
the value for `GOOGLE_CLIENT_ID` in the `.env` file. This value will be used in
`external_auth/google_provider.ts` – the custom OAuth2 provider definition for
this Google project.

Once complete, use `slack run` or `slack deploy` to update your local or hosted
app!

> Note: Unlike environment variables used at runtime, this variable is only used
> when generating your app manifest. Therefore, you do **not** need to use the
> `slack env add` command to set this value for
> [deployed apps](#deploying-your-app).
#### Validate Your App

At this point you should be able to build and start your project. Go ahead and
execute the following command to see if your app works properly.

When prompted:

- install the app to your workspace
- create the `triggers/configurator.ts` [trigger](#creating-triggers).

```zsh
# Run app locally
$ slack run

Connected, awaiting events
```

Note: Ignore warnings at this stage!

Once complete, press `<CTRL> + C` to end the process. You will need to create
additional secrets before using your application.

#### Save the Client Secret

With your client secret ready, run the following command, replacing
`GOOGLE_CLIENT_SECRET` with your own secret:

```sh
$ slack external-auth add-secret --provider google --secret GOOGLE_CLIENT_SECRET
```

When prompted to select an app, choose the `(local)` app only if you're running
the app locally.

#### Initiate the OAuth2 Flow

With your Google project created and the Client ID and secret set, you're ready
to initiate the OAuth flow!

If all the right values are in place, the following command will prompt you to
choose an app, select a provider (hint: choose the `google` one), then pick the
Google account you want to authenticate with:

```sh
$ slack external-auth add
```

> :unlock: Spreadsheets generated as part of the **Create a survey** workflow
> will be created from the account you authenticate with! To limit the users
> that can create surveys, an **Event configurator** workflow is used.
Once you've successfully connected your account, you're almost ready to create
surveys at the press of a reaction!

To complete the connection process, you need to let your app know what
authenticated account you'll be using for specific workflows.

For this specific app, only the `CreateSurvey` workflow requires a configured
external Google account, so we can set that up with our freshly authed account.
To do so, run:

```sh
slack external-auth select-auth
```

Select the workspace and app environment for your app, then select the
`#/workflows/create_survey` workflow to give it access to your Google account.
Then, select the same provider and the external account that you authenticated
with above.

At last - you're all set to survey! :sparkles:

## Running Your Project Locally

While building your app, you can see your changes appear in your workspace in
Expand Down Expand Up @@ -100,13 +246,25 @@ or deployed!**
To manually create a trigger, use the following command:

```zsh
$ slack trigger create --trigger-def triggers/<YOUR_TRIGGER_FILE>.ts
$ slack trigger create --trigger-def triggers/configurator.ts
```

### Using the Configurator Trigger

With the configurator link trigger (`triggers/configurator.ts`) you can
configure the channel list and surveying users, as shown below:

<img src="https://user-images.githubusercontent.com/18134219/215911063-e3ab2892-1644-4f63-9383-f37be2954172.gif" width="600">

Once the app is added to the channel being surveyed, configured users that add a
`:clipboard:` reaction to a message will begin the survey process with a prompt
to create a new survey.

## Datastores

For storing data related to your app, datastores offer secure storage on Slack
infrastructure. The use of a datastore requires the
infrastructure. For an example of a datastore, see
`datastores/survey_datastore.ts`. The use of a datastore requires the
`datastore:write`/`datastore:read` scopes to be present in your manifest.

## Testing
Expand All @@ -133,6 +291,23 @@ When deploying for the first time, you'll be prompted to
app. When that trigger is invoked, the workflow should run just as it did when
developing locally (but without requiring your server to be running).

### Production Maintenance Job

For production, we recommend enabling the included `maintenance_job.ts`
workflow.

The app's bot user must be a member of a channel in order to listen for events
there. When you add a new channel in the configuration modal, the bot user
automatically joins the channel. **However, anyone can remove the bot user from
the channel at any time.**

To enable a job that will re-add the bot user to channel, run the following
command that generates a scheduled trigger to run daily:

```zsh
$ slack trigger create --trigger-def triggers/daily_maintenance_job.ts
```

## Viewing Activity Logs

Activity logs of your application can be viewed live and as they occur with the
Expand All @@ -155,6 +330,12 @@ development and deployed apps.
for your application on Slack infrastructure. Required scopes to use datastores
include `datastore:write` and `datastore:read`.

### `external_auth/`

[External authentication](https://api.slack.com/automation/external-auth)
enables connections to external services using OAuth2. Once connected, you can
perform actions as the authorized user on these services using custom functions.

### `functions/`

[Functions](https://api.slack.com/automation/functions) are reusable building
Expand Down
Loading

0 comments on commit e028904

Please sign in to comment.