Bot for Slack that evaluates arithmetic expressions
  • Ruby 97.8%
  • Dockerfile 1.8%
  • Shell 0.3%
Find a file
Anirban Mukhopadhyay 9cb7544a94
Merge pull request #35 from anirbanmu/fix-credentials-again
Set correct Slack credentials in production creds
2024-03-16 15:20:57 -07:00
.github/workflows Fix credentials 2024-03-16 15:08:19 -07:00
app Fix credentials 2024-03-16 15:08:19 -07:00
bin Upgrade to Rails 7.1 2024-03-16 13:58:32 -07:00
config Set correct Slack credentials in production creds 2024-03-16 15:20:40 -07:00
lib Upgrade to Rails 7.1 2024-03-16 13:58:32 -07:00
log Generate barebones API only rails application 2018-01-26 17:13:08 -06:00
public Generate barebones API only rails application 2018-01-26 17:13:08 -06:00
spec Fix credentials 2024-03-16 15:08:19 -07:00
tmp Generate barebones API only rails application 2018-01-26 17:13:08 -06:00
vendor Generate barebones API only rails application 2018-01-26 17:13:08 -06:00
.dockerignore Add Dockerfile & make fly.io deployment possible 2022-08-26 23:51:51 -07:00
.gitignore Fix credentials 2024-03-16 15:08:19 -07:00
.rspec Add ability to post a chat message back to Slack via their Web API 2018-01-27 19:55:31 -06:00
.rubocop.yml Rails 6 & Ruby 3 upgrade 2021-04-26 21:21:51 -07:00
.ruby-gemset Rails 6 & Ruby 3 upgrade 2021-04-26 21:21:51 -07:00
.ruby-version Downgrade to latest ruby 3.2 since 3.3 hits a segfault 2024-03-16 14:20:17 -07:00
config.ru Rails 6 & Ruby 3 upgrade 2021-04-26 21:21:51 -07:00
Dockerfile Downgrade to latest ruby 3.2 since 3.3 hits a segfault 2024-03-16 14:20:17 -07:00
entrypoint.sh Only make sure RAILS_MASTER_KEY is set 2024-03-16 15:14:05 -07:00
fly.toml Update fly.tml to fly.io apps v2 2024-03-16 13:23:07 -07:00
Gemfile Downgrade to latest ruby 3.2 since 3.3 hits a segfault 2024-03-16 14:20:17 -07:00
Gemfile.lock Downgrade to latest ruby 3.2 since 3.3 hits a segfault 2024-03-16 14:20:17 -07:00
Procfile Add Procfile 2018-01-27 21:28:28 -06:00
Rakefile Rails 6 & Ruby 3 upgrade 2021-04-26 21:21:51 -07:00
README.md Upgrade to Rails 7.1 2024-03-16 13:58:32 -07:00
slack-manifest.yml Update to newer Slack app model 2022-08-24 20:39:31 -07:00

slack-calc-bot

Bot for Slack that evaluates arithmetic expressions. Supports addition (+), subtraction (-), multiplication (*, ×), division (\, ÷), exponentiation (^) (use exponentiation for square roots & other roots) & parenthesis ((, )).

Setup

These steps jump between local machine, Slack side & fly.io a lot, so please bear with me.

On your machine

  • Install Ruby.
  • Install Bundler - gem install bundler
  • Clone this repo
  • (Optional) If you'd like to host the bot on fly.io
    • Install flyctl (instructions here).
    • Auth flyctl
    • From the repo directory, run flyctl apps create & choose options as prompted.
    • Note down the the app name you chose here, referred to as <FLY-IO-APP-NAME>.
    • Fill in this name <FLY-IO-APP-NAME> in fly.toml where it says <FLY-IO-APP-NAME>.
    • Go to https://fly.io/apps/<FLY-IO-APP-NAME> & note down it's hostname, will be referred to as <FLY-IO-APP-URL>.

On Slack side

  • Create a new application at https://api.slack.com/apps using the template manifest @ slack-manifest.yml filling in the following details:
    • <URL-TO-YOUR-APP>: can be either ngrok URL for development or <FLY-IO-APP-URL> if you're hosting on fly.io.
  • Note down the 'Signing Secret' given on the 'Basic Information' page for the application (should be of form https://api.slack.com/apps/<some-app-id>/general?)
  • Install application to your workspace & note down the Bot User OAuth Token on the 'Install App' page for the app (should be of form https://api.slack.com/apps/<some-app-id>/install-on-team?)

Back on your machine (for local development)

  • From the repo directory, run bundle install
  • Set two environment variables as follows from the Slack information we noted down earlier:
    • SLACK_SIGNING_SECRET=<Signing Secret noted down from the 'Basic Information' page on Slack>
    • SLACK_BOT_ACCESS_TOKEN=<Bot User OAuth Token noted down from the 'Install App' page on Slack>
  • Run the Rails application via bundle exec rails server

Back on your machine (for fly.io deployment)

  • Run flyctl secrets set SLACK_SIGNING_SECRET=<Signing Secret noted down from the 'Basic Information' page on Slack>
  • Run flyctl secrets set SLACK_BOT_ACCESS_TOKEN=<Bot User OAuth Token noted down from the 'Install App' page on Slack>
  • Run bundle exec rails secret & copy the value that's printed.
  • Run flyctl secrets set SECRET_KEY_BASE=<value copied in last step>
  • Run flyctl deploy to deploy
  • You should see that fly.io successfully deploys your bot

Back on Slack

  • Navigate to the 'App Manfiest' page for your app (should be of form https://app.slack.com/app-settings/<some-id>/<some-id>/app-manifest)
  • You'll see a message at the top regarding your request URL not being verified, go ahead and verify the URL. As long as you did the previous block properly on your machine/server, the verification should be successful.

You're all set to start messaging the app/bot. Invite the bot to your channel & highlight it with arithmetic expressions. Direct messages to the application also work.

Tests

This assumes you've completed the On your machine & Back on your machine (for local development) section above. To execute tests run bundle exec rspec.

Main source code locations

  • Slack::EventsController - implements endpoint for Events API app/controllers/slack
  • Slack::WebApi - Wrapper to call Slack WebAPI lib/slack
  • InfixEvaluator - Class to parse and evaluate infix arithmetic lib
  • Slack::CalculateAndSendJob - Async job to do infix evaluation & sending of slack message app/jobs/slack
  • Test specs - RSpec tests spec