https://github.com/postlight/slash-mercury-parser
🤖 Slack slash command that turns a link to an article into a readable post within a channel
https://github.com/postlight/slash-mercury-parser
aws-lambda labs mercury mercury-parser serverless slack slackbot
Last synced: 20 days ago
JSON representation
🤖 Slack slash command that turns a link to an article into a readable post within a channel
- Host: GitHub
- URL: https://github.com/postlight/slash-mercury-parser
- Owner: postlight
- License: apache-2.0
- Created: 2019-03-07T10:15:34.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-02-08T11:08:59.000Z (about 3 years ago)
- Last Synced: 2025-06-10T14:50:15.221Z (10 months ago)
- Topics: aws-lambda, labs, mercury, mercury-parser, serverless, slack, slackbot
- Language: TypeScript
- Homepage:
- Size: 175 KB
- Stars: 11
- Watchers: 29
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# Parser
This is a [serverless](https://serverless.com/) Slack Bot that uses Postlight's [mercury parser](https://github.com/postlight/mercury-parser) to display the parsed content directly in your slack channel! So instead of sending a link to a certain article that will open in a new browser window, you will be sending the article as a readable post directly.
Once everything is set, use it as follows:
```bash
/parser
```

## Development
This repo is based on Postlight's [serverless-babel-starter](https://github.com/postlight/serverless-babel-starter). You can refer to it for serverless development documentation.
### Setting up the workspace
clone this repo:
```bash
git clone https://github.com/postlight/slash-mercury-parser.git
```
Install dependencies:
```bash
yarn install
```
### Creating your Slack Bot
Create a new slack app [here](https://api.slack.com/apps?new_app=1) and link it to your development slack workspace.
Under `Add feature and functionality`:
- Add a bot:
- Click on `Bots`
- Click on `Add a bot User`
- Enter a `Display name` and `Default username`
- Click on `Add bot User`
Â
- Add a slash command
- Click on `Slash Commands`
- Click on `Create New Command`
- Enter `/parser`, `https://example.com/invoke`, and `Renders the parsed content` for `Command`, `Request URL`, and `Short Description`
- Click on `Save`
**NOTE:** you will fill in a correct `Request URL` once you deploy your functions. You can leave it as is for now.
### Environment variables
After you've installed the bot in your slack workspace, navigate to `Install App` under `Settings` and copy your `Bot User OAuth Access Token`. This token will be used to verify the slack API call.
Create a `secrets.json` file in the project root and add the following:
```json
{
"SLACK_AUTH_TOKEN": "xxxx-YOUR-ACCESS-TOKEN"
}
```
### Deploying the functions
```bash
yarn deploy:env
```
Windows users should modify the `deploy` scripts as follows:
```json
{
"deploy:dev": "sls deploy --stage dev",
"deploy:stage": "sls deploy --stage stage",
"deploy:production": "sls deploy --stage production"
}
```
**NOTE:** save your `/post` URL when the deployment is complete. It looks like this: `https://xxxxxxx.execute-api.region.amazonaws.com/env/invoke` and replace your `Request URL` with it.
### Testing the bot
Inside a `public` channel in your slack workspace, invoke the bot using:
```bash
/parser
```
You should see a `Parsing your article ..` message which is only visible to you, followed by a bot response of the parsed content as a post.
## The logic behind a slack bot
- Whenever the slash command is executed, slack makes a `POST` request to your app via the `Request URL` that was set.
- Your logic will get executed and will invoke a slack API call.
- Slack expects a response within 3 seconds
- [Slack API Documentation](https://api.slack.com/web)
## The logic behind the parser bot
Since Slack expects an `OK` response within 3 seconds, the first lambda function `./src/invoke.js` checks for any errors in the URL, fetches the content from mercury, and invokes the second function `./src/uploadPost.js`. The second function is responsible for hitting the slack API and sending the post. If an error occurs, the user will be notified.
For example, `/parser blabla` will result in this error:

## Contributing
Unless it is explicitly stated otherwise, any contribution intentionally submitted for inclusion in the work, as defined in the Apache-2.0 license, shall be dual licensed as above without any additional terms or conditions.
---
🔬 A Labs project from your friends at [Postlight](https://postlight.com/labs)