A Cloudflare Worker that makes use of the Habitica API and Discord Webhooks
I blogged about it, but since then I moved to using Wrangler, which offers much more features:
- Modules
- TypeScript
- npm packages
The reason the worker returns messages as JSON is only for testing purposes, so that I can make assertions on the response.
Deployments to Cloudflare (dev + production) is done via GitHub Actions
This is environment is only for E2E testing with Cypress running against localhost. It sets the ENVIRONMENT variable to "test" and prevents real API calls (to Discord or Habitica) to be made. This environment is listed as habitica-bot-test
in wrangler.toml
, but it is NOT deployed to Cloudflare.
Command: yarn test-server
(with hot reloading!)
Testing (with UI): yarn test
Testing (without UI, faster): yarn test:ci
This is the default environment: when no argument is provided to the wrangler CLI, it assumes dev
. The corresponding branch on GitHub is named staging
.
Standard staging environment. Deployed to Cloudflare worker as habitica-bot-dev
. Messages are posted to a dedicated Discord server for testing.
Command: yarn dev
(with hot reloading!)
Production worker. Messages are posted to the Habitica Discord server. The corresponding branch on GitHub is named master
.
The worker always returns a 200 success code, even if errors are thrown. The reason is:
Best practice is to respond to the Habitica server as soon as you receive a request with a 200 HTTP code and a non-empty response https://habitica.fandom.com/wiki/Webhooks
However, errors thrown are sent to Sentry.io before the success code is returned.