Skip to content

Commit

Permalink
project: start slack app example
Browse files Browse the repository at this point in the history
  • Loading branch information
practicalli-johnny committed Sep 15, 2023
1 parent 66a0dc0 commit e253611
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 0 deletions.
118 changes: 118 additions & 0 deletions docs/projects/slack-app/create-slack-app.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Create Slack App

Create a Slack account and follow the prompts to create a new workspace. The workspace will be used to deploy the Slack App for testing purposes.

> Use an existing Slack workspace if sufficient administration privelleges are available (and you wont affect other peoples use of Slack)
[Slack Quickstart](https://api.slack.com/start/quickstart) describes how to create a Slack app.

[Create a new Slack app with the Slack UI](https://api.slack.com/apps)

![Slack Web UI - Create an App](https://github.com/practicalli/graphic-design/blob/live/clojure-web-services/slack-app/clojure-web-services-slack-app-your-apps-create-an-app.png?raw=true#only-dark#){loading=lazy}

Select **From Scratch**

![Slack Web UI - create an app options](https://github.com/practicalli/graphic-design/blob/live/clojure-web-services/slack-app/clojure-web-services-slack-app-your-apps-create-an-app--options.png?raw=true#only-dark#){loading=lazy}

Enter App Name and select the Development Workspace to experiment and build the app.

![Slack Web UI - name app and choose workspace](https://github.com/practicalli/graphic-design/blob/live/clojure-web-services/slack-app/clojure-web-services-slack-app-your-apps-create-an-app--from-scratch-clojure-function-app-name.png
?raw=true#only-dark#){loading=lazy}

> Regardless of development workspace, the app can be distributed to any other workspaces.
The Slack Web UI displays the basic information for the newly created Slack app.

![Slack Web UI - new slack app basic information](https://github.com/practicalli/graphic-design/blob/live/clojure-web-services/slack-app/clojure-web-services-slack-app-manifest-code-config-popup.png?raw=true#only-dark#){loading=lazy}

<!-- TODO: add App Manifest approach to creating a Slack App -->


## Configure scopes

Add a scope to post messages to a channel

**Sidebar** > **OAuth & Permissions** > **Scopes** > **Add an OAuth Scope**

![Slack Web UI - Slack App Scopes](https://github.com/practicalli/graphic-design/blob/live/clojure-web-services/slack-app/slack-app-settings-oauth-scopes.png?raw=true#only-dark#){loading=lazy}

Add the following Bot Token Scopes

- `chat:write` scope to the Bot Token to allow the app to post messages
- `channels:read` scope too so your app can gain knowledge about public Slack channels

![Slack Web UI - Add Bot Token Scopes](https://github.com/practicalli/graphic-design/blob/live/clojure-web-services/slack-app/slack-app-settings-oauth-scopes-chat-write.png?raw=true#only-dark#){loading=lazy}


> Reisntall the app if changing scopes and other features

## Update Display information

> This feels like it should have been done before installing the app
Provide a short & long description of the app and set background colour. Optionally add an app icon (between 512 and 2000 px in size).

- **Short description**: A random function from the Clojure Standard Library
- **Long description**: A random function is selected from the Clojure Standard Library and displayed along with the documentation (doc string) to explain what the function does. A bonus feature will be to provide examples of function use.


## Install app in workspace

Install your app to your Slack workspace to test it and generate the tokens you need to interact with the Slack API. You will be asked to authorize this app after clicking an install option.

Sidebar > Settings > Basic Information > Install your app

![Slack Web UI - Choose a workspace to deploy the Slack app](https://github.com/practicalli/graphic-design/blob/live/clojure-web-services/slack-app/slack-workspace-sign-in.png?raw=true#only-dark#){loading=lazy}


### Authorization token

The Authorization token for the workspace is in the [app management web page](https://api.slack.com/apps/) for the specific app

**Sidebar** > **OAuth & Permissions** > **OAuth Tokens for Your Workspace**

Create an environment variable to hold the authorization token with the value of the **Bot User OAuth Token**

```shell
export SLACK_AUTHENTICATION_TOKEN=xxxx-123412341234-12345123451245-...
```

> Environment variables used with Clojure must be set before running the REPL, so the variables are available to the Java Virtual Machine process.
>
> Add the environment variables to `.bashrc` for Bash or `.zshenv` for Zsh

Access tokens represent the permissions delegated to the app by the installing user.

> Avoid checking access tokens into version control

## Test app - save for later...

Add the app to a public channel and test its (as yet unconfigured slash command)

![Slack Web UI - Add Clojure function Slack App to a channel](https://github.com/practicalli/graphic-design/blob/live/clojure-web-services/slack-app/slack-workspace-add-app-to-channel.png?raw=true#only-dark#){loading=lazy}

Confirm the Slack App should be added to the selected workspace

![Slack Web UI - Slack App requesting permission to access the workspace](https://github.com/practicalli/graphic-design/blob/live/clojure-web-services/slack-app/slack-app-api-settings-add-app-to-workspace-allow.png?raw=true#only-dark#){loading=lazy}



## Local Development

Use Socket Mode to route the app interactions and events over a WebSockets connection instead sending payloads to Request URLs, the public HTTP endpoints.

Socket mode is intended for internal apps that are in development or need to be deployed behind a firewall. It is not intended for widely distributed apps.

Alternatively, use ngrock to redirect requests to the local app.


### Install app into workspace

Install Slack app into a workspace (not the development workspace)

**Sidebar** > **Install App** > **Install App To Workspace** > **Slack OAuth UI**


16 changes: 16 additions & 0 deletions docs/projects/slack-app/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Clojure powered Slack Application

Slack applications provide a way to extend the functionality of Slack and provide integration with other services, e.g. GitHub, Atlassian Jira & Confluence, etc.


## Development process Overview

- create a slack account and slack space (to test the application)
- create a slack app
- deploy the slack app in the slack test workspace created previously
- Create a clojure project and interact with the Slack API
- Setup ngrok domain for Slack app to access a locally running Clojure web service (to respond to interaction with the app - assuming the slack isnt just push driven)
- alternatively, webservices connection can be configured between the locally running Clojure app and slack (desktop app required? - how does this work...)

- deploy clojure application to public cloud (AWS, Render.com, etc)

32 changes: 32 additions & 0 deletions docs/projects/slack-app/slack-api-methods.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Slack API Methods

An access token allows the calling of the methods described by the scopes requested during a Slack App installation,
e.g., the chat:write scope allows an app to post messages.

The app isn't a member of any channels when installed. Choose a channel suitable for testing purposes and `/invite` the Slack app to the channel.

You can find the corresponding id for the channel that your app just joined by looking through the results of the conversations.list method:

```shell
curl https://slack.com/api/conversations.list -H "Authorization: Bearer xoxb-1234..."
```

You'll receive a list of conversation objects.

Now, post a message to the same channel your app just joined with the chat.postMessage method:

```shell
curl -X POST -F channel=C1234 -F text="Reminder: we've got a softball game tonight!" https://slack.com/api/chat.postMessage -H "Authorization: Bearer xoxb-1234..."
```

![Slack Web UI - Web API Methods search](https://github.com/practicalli/graphic-design/blob/live/clojure-web-services/slack-app/slack-app-api-settings-add-app-to-workspace-allow.png?raw=true#only-dark#){loading=lazy}


Voila! We're already well on our way to putting a full-fledged Slack app on the table.

[Web API Guide](https://api.slack.com/web)

[API Methods list](https://api.slack.com/methods)

[Interactive Workflows](https://api.slack.com/interactivity)

26 changes: 26 additions & 0 deletions docs/projects/slack-app/slack-scopes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Slack Scopes Overview

Scopes give the app permission to carry out actions, e.g. post messages, in the development workspace.

Open the development workspace, either in a web page or in the Slack desktop app.

**Sidebar** > **OAuth & Permissions** > **Scopes** > **Add an OAuth Scope**

- `chat:write` scope to the Bot Token to allow the app to post messages
- `channels:read` scope too so your app can gain knowledge about public Slack channels
- `commands` scope to build a Slash command.
- `incoming-webhook` scope to use Incoming Webhooks.
- `chat:write.public` scope to gain the ability to post in all public channels, without joining. Otherwise, you'll need to use conversations.join, or have your app invited by a user into a channel, before you can post.
- `chat:write.customize` scope to adjust the app's message authorship to make use of the username, icon_url, and icon_emoji parameters in `chat.postMessage`.


> Add scopes to the Bot Token.
>
> Only add scopes to the User Token when the app needs to act as a specific user (e.g. post message as user, set user status, etc.)

If the scopes applied to a Slack App are changed, the Slack App must be redeployed to the workspace (and Slack App Directory) for the changes to take effect.

![Slack Web UI - Warning changed slack app permission scopes - reinstall the app](https://github.com/practicalli/graphic-design/blob/live/clojure-web-services/slack-app/slack-app-settings-oauth-scopes-reinstall-warning.png?raw=true#only-dark#){loading=lazy}


5 changes: 5 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ nav:
- Projects:
- projects/index.md
# - full-app/index.md
- Slack App:
- projects/slack-app/index.md
- Create Slack App: projects/slack-app/create-slack-app.md
- projects/slack-app/slack-scopes.md
- projects/slack-app/slack-api-methods.md
- Status Monitor:
- projects/status-monitor-deps/index.md
- Application Server: projects/status-monitor-deps/application-server.md
Expand Down

0 comments on commit e253611

Please sign in to comment.