/devilbot

A Rust rewrite of Flameboi to work for our Discord Server.

Primary LanguageRustApache License 2.0Apache-2.0

Devil Bot Rust

devil_bot_rust devil_bot_rust codecov License

Table of Contents

  1. Overview
  2. Prereqs
  3. Getting Started with Git
  4. Set up AWS account
  5. Connecting to your AWS Account to your personal device
  6. Set up project for AL2 target Mac, Ubuntu (aarch64/x86_64), and Windows (WSL 2 Ubuntu 20.04 LTS)
  7. After your project is set up use the following to build your code and deploy it to AWS test
  8. Setting up to test against a personal Slack bot
  9. Useful CDK commands and their descriptions
  10. How to Enable API Throttling
  11. Slack-Morphism

Overview

  • A Rust implementation of a Slack bot that will be used by the CodeDevils Slack workspace.
  • All resources are managed using AWS CDK.
  • The main driver is AWS API Gateway to provide static endpoints and AWS Lambda for serverless compute/request handling.

Prereqs

Getting Started with Git

  1. Create a new local directory for this project.
  2. Run git clone https://github.com/ASU-CodeDevils/devil_bot_rust.git in that new directory.

Set up AWS account

  1. Create a new AWS account for free: https://aws.amazon.com
  2. Go to the IAM console (type "IAM" in search bar on AWS website after logging in).
  3. Click on "Users" on the left-hand side bar under "Access Management".
  4. Click "Add Users" to the right.
  5. For username choose something like "devil-bot-test-user-${your_asu_alias}" (e.g. "devil-bot-test-user-jtmichel").
  6. Make sure "Access key - Programmatic access" check box is checked.
  7. Click "Next: Permissions".
  8. Click "Attach existing policies directly".
  9. Check "AdministratorAccess" (you can use less permissions if you know what you're doing, but this should work fine as long as you don't give away your credentials).
  10. Click "Next: Tags".
  11. Click "Next: Review".
  12. Click "Create user".
  13. Copy both your "Access key ID" and your "Secret access key" somewhere locally (only store this temporarily then delete).
  14. Continue to "Connecting to your AWS Account" steps below.

Connecting to your AWS Account to your personal device

  1. Run the following on your personal device's terminal.
  2. aws configure
  3. For "access key" use your "Access key ID" from the "Set up AWS account" instructions above.
  4. For "secret access key" use your "Secret access key" from the "Set up AWS account" instructions above.
  5. For "default region name" use: us-east-1
  6. For "defaut output format" use: None (just leave blank and press enter)

Set up project for AL2 target Mac, Ubuntu (aarch64/x86_64), and Windows (WSL 2 Ubuntu 20.04 LTS)

  1. If on Windows install and configure Ubuntu 20.04 LTS using WSL2 the following step are to be done wihtin that VM https://docs.microsoft.com/en-us/windows/wsl/install
  2. Ensure you've installed Rust, NPM, AWS-CDK Toolkit, and AWS-CLI
  3. Confirm you've setup your AWS account and Connected it!
  4. chmod +x build-function.sh
  5. sh build-function.sh
  6. cdk bootstrap
  7. cdk deploy

After your project is set up use the following to build your code and deploy it to AWS test

Note: This will deploy only to dev

  1. sh build-function.sh
  2. cdk diff (optional, but useful command)
  3. cdk deploy --app 'cdk.out/' DevilBotRustCdkStack

Setting up to test against a personal Slack bot

  1. Do the following after creating the above infrastructure (after successfully running cdk deploy to your personal AWS account).
  2. Go to the API Gateway console (type "API Gateway" in search bar on AWS website after logging in).
  3. Click on "APIs" on the left-hand side bar under "API Gateway".
  4. Check to make sure you are signed in under us-east-1
  5. Click on "RustSlackEndpoint".
  6. Click on "Stages" on the left-hand side bar under "API: RustSlackEndpoint".
  7. Click on "Prod" under "Stages".
  8. Copy the "Invoke URL" provided.
  9. Contact one of our officers and tell them you have your Slack bot API Gateway endpoint ready for a personal Slack bot app.
  10. Provide the officer with the URL and they will give you a Bot API Token which you can then plug in to the environment list in devil-bot-rust-cdk-stack.ts.
  11. While you are developing, make sure to limit you testing to the #devil-bot-test channel in Slack. Or if you are testing a feture that is not in a channel, reach out to Rhett or Jason for some creative ideas to test. We don't want to spam other public channels. The ID for the test channel is C0351GJ62Q0
  12. When you have your code ready for review, remove the environment variable for the Bot token before creating your PR. Follow the instructions found in CONTRIBUTING.md for more info on creating your PR. If you don't remove this token before making a commit, Slack will uninstall your bot.

Useful CDK commands and their descriptions

  • npm run build compile typescript to js
  • npm run watch watch for changes and compile
  • npm run test perform the jest unit tests
  • cargo fmt --all --check --manifest-path resources/Cargo.toml Checks code formatting for Rust
  • rustup update update Rust
  • cdk diff compare deployed stack with current state
  • cdk synth emits the synthesized CloudFormation template
  • cdk deploy --all deploy all stacks to your default AWS account/region
  • cdk deploy --app 'cdk.out/' DevilBotRustCdkStack Deploys to the production Stack of your DevilBot (not the real production version)
  • cdk deploy --app 'cdk.out/' DevilBotRustCdkStackDev Deploys the Dev Stack of your Devil Bot

Any of these three commands cdk deploy cdk deploy --app 'cdk.out/' DevilBotRustCdkStack cdk deploy --app 'cdk.out/' DevilBotRustCdkStackDev will deploy your DevilBot to AWS. We run this app in two stages so it is essentially like having two separate apps. This is mainly so Rhett can deploy the production version from the same account his dev version is on. You are fine to use either stages or both on your account, but:

  • Make sure that you have the correct API key for that Bot or else it won't function.
  • Make sure that the invoke URL you use for that Bot is the same one that is on the Stage you are expecting

Testing with POST requests

Sometimes you may not want to spam messages into the Slack channels when you want to test. In this case you can POST messages directly to your API Gateway endpoint and view CloudWatch logs to troubleshoot problems with your code.

Postman

Postman is a UI alternative to using curl.

  1. Download postman for free from https://www.postman.com/downloads/
  2. Ignore the make an account messages and "workspaces" and just use the "scratchpad" offline feature. This used to be the only way Postman operated, but they are trying to make money so we will forgive them for begging us to use cloud storage on a platform that definitely doesn't need it.
  3. Under the scratchpad menu "Overview" click on the "Create a request" button.
  4. Click on the "GET" dropdown and swap to "POST".
  5. In the "Enter request URL" box insert your API Gateway URL obtained in the Setting up to test against a personal Slack bot section.
  6. Click on the "Body" tab and enter a modified version of one of the message body from below.
  7. Click on the "raw" radio button, and select "JSON" from the dropdown to the right of the radio buttons.
  8. You can now send requests directly to your endpoint.

Example Message Event Body JSON

{
    "api_app_id": "XXXXXXXXXXX",
    "authorizations": [
        {
            "enterprise_id": "XXXXXXXXX",
            "is_bot": false,
            "is_enterprise_install": false,
            "team_id": "XXXXXXXXX",
            "user_id": "XXXXXXXXX"
        }
    ],
    "enterprise_id": "XXXXXXXXX",
    "event": {
        "blocks": [
            {
                "block_id": "xXxx",
                "elements": [
                    {
                        "elements": [
                            {
                                "text": "Test message here.",
                                "type": "text"
                            }
                        ],
                        "type": "rich_text_section"
                    }
                ],
                "type": "rich_text"
            }
        ],
        "channel": "XXXXXXXXXXX",
        "channel_type": "group",
        "client_msg_id": "9173c749-d7b3-4330-9576-590740901793",
        "event_ts": "1645903860.916719",
        "team": "XXXXXXXXX",
        "text": "test",
        "ts": "1645903860.916719",
        "type": "message",
        "user": "XXXXXXXXX"
    },
    "event_context": "4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMk43NkZaM1EiLCJhaWQiOiJBMDJVOUc4NUI2WiIsImNpZCI6IkMwMzUxR0o2MlEwIn0",
    "event_id": "Ev0356A5S917",
    "event_time": 1645903860,
    "is_ext_shared_channel": false,
    "team_id": "XXXXXXXXX",
    "token": "oooooooooooooooooo",
    "type": "event_callback"
}

Example Challenge Body JSON

{
    "token": "Jhj5dZrVaK7ZwHHjRyZWjbDl",
    "challenge": "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P",
    "type": "url_verification"
}

How to Enable API Throttling

  1. Sign in to AWS
  2. In the search bar, search for API Gateway
  3. Click on RustSlackEndpoint
  4. In the left menu, click on Usage Plans
  5. In the Usage Plans menu, create a new usage plan

This is pretty customizable. Recommended to cap your requests per month at 900,000

Slack Morphism