Skip to content

Commit

Permalink
Merge branch 'main' into python-dynamodb
Browse files Browse the repository at this point in the history
  • Loading branch information
eladcon authored May 26, 2024
2 parents 92106ce + 58b6dea commit b78812d
Show file tree
Hide file tree
Showing 17 changed files with 694 additions and 2 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/canary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,39 @@ jobs:
cd simtools
wing test
timeout_minutes: 5
canary-slack:
name: Test slack
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: slack
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x
registry-url: https://registry.npmjs.org
- name: Install winglang and dependencies
uses: nick-fields/retry@v3
with:
max_attempts: 3
command: npm i -g winglang --loglevel verbose
timeout_minutes: 3
- name: Install dependencies
uses: nick-fields/retry@v3
with:
max_attempts: 3
command: cd slack && npm i --include=dev --loglevel verbose
timeout_minutes: 3
- name: Run tests
uses: nick-fields/retry@v3
with:
max_attempts: 3
command: |-
cd slack
wing test
timeout_minutes: 5
canary-sns:
name: Test sns
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pull-request-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
sagemaker
ses
simtools
slack
sns
tf
tsoa
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/slack-pull.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: slack-pull
on:
pull_request:
paths:
- slack/**
jobs:
build-slack:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: slack
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: slack
- name: Test
run: wing test
working-directory: slack
- name: Pack
run: wing pack
working-directory: slack
54 changes: 54 additions & 0 deletions .github/workflows/slack-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: slack-release
on:
push:
branches:
- main
paths:
- slack/**
- "!slack/package-lock.json"
jobs:
build-slack:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: slack
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: slack
- name: Test
run: wing test
working-directory: slack
- name: Pack
run: wing pack
working-directory: slack
- name: Get package version
run: echo WINGLIB_VERSION=$(node -p "require('./package.json').version") >>
"$GITHUB_ENV"
working-directory: slack
- name: Publish
run: npm publish --access=public --registry https://registry.npmjs.org --tag
latest *.tgz
working-directory: slack
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Tag commit
uses: tvdias/[email protected]
with:
repo-token: ${{ secrets.PROJEN_GITHUB_TOKEN }}
tag: slack-v${{ env.WINGLIB_VERSION }}
- name: GitHub release
uses: softprops/action-gh-release@v1
with:
name: slack v${{ env.WINGLIB_VERSION }}
tag_name: slack-v${{ env.WINGLIB_VERSION }}
files: "*.tgz"
token: ${{ secrets.PROJEN_GITHUB_TOKEN }}
6 changes: 6 additions & 0 deletions .mergify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ pull_request_rules:
- -check-failure=build-simtools
- -check-pending=build-simtools
- -check-stale=build-simtools
- -check-failure=build-slack
- -check-pending=build-slack
- -check-stale=build-slack
- -check-failure=build-sns
- -check-pending=build-sns
- -check-stale=build-sns
Expand Down Expand Up @@ -195,6 +198,9 @@ pull_request_rules:
- -check-failure=build-simtools
- -check-pending=build-simtools
- -check-stale=build-simtools
- -check-failure=build-slack
- -check-pending=build-slack
- -check-stale=build-slack
- -check-failure=build-sns
- -check-pending=build-sns
- -check-stale=build-sns
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ publishing them for you.
| [sagemaker](./sagemaker) | [@winglibs/sagemaker](https://www.npmjs.com/package/@winglibs/sagemaker) | sim, tf-aws |
| [ses](./ses) | [@winglibs/ses](https://www.npmjs.com/package/@winglibs/ses) | sim, tf-aws |
| [simtools](./simtools) | [@winglibs/simtools](https://www.npmjs.com/package/@winglibs/simtools) | sim |
| [slack](./slack) | [@winglibs/slack](https://www.npmjs.com/package/@winglibs/slack) | sim, tf-aws |
| [sns](./sns) | [@winglibs/sns](https://www.npmjs.com/package/@winglibs/sns) | awscdk, sim, tf-aws |
| [tf](./tf) | [@winglibs/tf](https://www.npmjs.com/package/@winglibs/tf) | sim, tf-aws |
| [tsoa](./tsoa) | [@winglibs/tsoa](https://www.npmjs.com/package/@winglibs/tsoa) | sim |
Expand Down
2 changes: 1 addition & 1 deletion python/lib.w
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub class InflightApiEndpointHandler impl cloud.IApiEndpointHandler {
if target == "sim" {
this.inner = new simapi.InflightApiEndpointHandler(props);
} elif target == "tf-aws" {
this.inner = new tfawsapi.Inflight_tfaws(props);
this.inner = new tfawsapi.InflightApiEndpointHandler_aws(props);
} else {
throw "Unsupported target ${target}";
}
Expand Down
2 changes: 1 addition & 1 deletion python/tfaws/api_onrequest_inflight.w
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ bring util;
bring "../types.w" as types;
bring "./inflight.w" as inflyght;

pub class Inflight_tfaws extends inflyght.Inflight_tfaws impl types.IApiOnRequest {
pub class InflightApiEndpointHandler_aws extends inflyght.Inflight_tfaws impl types.IApiOnRequest {
new(props: types.InflightProps) {
super(props);
}
Expand Down
139 changes: 139 additions & 0 deletions slack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# slack

This library allows using Slack with Wing.

## Prerequisites

* [winglang](https://winglang.io)

## Supported Features
- Events
- Direct Channel Messages

### Unsupported Features (TODO)
- Distribution Management
- Interactive Components
- Slash Commands
- Workflows
- Incoming Webhooks

## Installation

Use `npm` to install this library:

```sh
npm i @winglibs/slack
```
## Bring it

```js
bring slack;
bring cloud;

let myBotToken = new cloud.Secret(name: "mybot_token");

let slackbot = new slack.App(botToken: myBotToken);

/// When registering events, the inflight function will be called with the context and event
/// The `ctx` is a reference to the context of the event, and provides client methods to interact with channels, threads, etc..
/// The `event` is the raw Json event data from Slack and can be used to extract information from the event
slackbot.onEvent("app_mention", inflight (ctx, event) => {
let message = new slack.Message();
message.addSection({
fields: [
{
type: slack.FieldType.mrkdwn,
text: "*Wow this is markdown!!*\ncool beans!!!"
}
]
});

ctx.channel.postMessage(message);
});
```

### Create your Slack App

1. Go to the [Slack API Dashboard](https://api.slack.com/apps) and create a new app.
2. Select **Create from Scratch**.
3. For the README example above, ensure you provide the following permissions:
- `app_mentions:read`
- `chat:write`
- `chat:write.public`
- `channels:read`
4. Navigate to **Events** and subscribe to the following events:
- `app_mention`
5. Navigate to **OAuth & Permissions** and install the app to your workspace
6. Copy the **Bot User OAuth Token** to your clipboard
7. Navigate to **Event Subscriptions** and enable events, then subscribe to bot events:
- `app_mention`

### Running in the Wing Simulator

First lets configure our Slack bot token as a secret in the simulator.

```sh
wing secrets
```

When prompted, paste the Bot User OAuth Token you copied earlier.

Next when running in the Wing Simulator, you will need to expose the endpoint of the Slackbot API, this can be done through the simulator console by selecting `Open tunnel for this endpoint`

![Open Tunnel](image.png)

Take this URL and navigate back to your Slack App, under the Event Subscriptions section, paste the URL into the Request URL field and append `/slack/events` to the end of the URL.

### Running in AWS

First lets configure our Slack bot token as a secret in AWS.

```sh
wing secrets -t tf-aws
```

When prompted, paste the Bot User OAuth Token you copied earlier.

Next, let's run the following command to deploy our app to AWS.

```sh
wing compile -t tf-aws
terraform -chdir=target/main.tfaws init
terraform -chdir=target/main.tfaws apply
```


After compiling and deploying your app using `tf-aws` you there will be an endpoint called `Slack_Request_Url` that is part of the terraform output. The URL should end with `/slack/events`. It will look something like this:

```sh
Apply complete! Resources: 11 added, 0 changed, 0 destroyed.

Outputs:

App_Api_Endpoint_Url_E233F0E8 = "https://zgl8r8wsng.execute-api.us-east-1.amazonaws.com/prod"
App_Slack_Request_Url_FF26641D = "https://zgl8r8wsng.execute-api.us-east-1.amazonaws.com/prod/slack/events"
```

Navigate back to your Slack App, under the Event Subscriptions section, paste the URL into the Request URL field and append `/slack/events` to the end of the URL.


## Post Directly to a Channel

If you want to post directly to a channel, you can do so by using the following code:

```js
bring slack;
bring cloud;

let slackbot = new slack.Slackbot();

let postMessage = new cloud.Function(inflight () => {
let channel = slackbot.getChannel("NAME|ID");

channel.postText("hello world!");
});
```

### Finding a Channel ID

If you prefer to use a channel ID over a channel name, you can find the channel ID by right-clicking on the channel name and select `View channel details`. The channel ID will be at the bottom of the popup modal.
Loading

0 comments on commit b78812d

Please sign in to comment.