Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions node/alerts-to-chat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Directory used by Appwrite CLI for local development
.appwrite
6 changes: 6 additions & 0 deletions node/alerts-to-chat/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true
}
69 changes: 69 additions & 0 deletions node/alerts-to-chat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 💬 Node.js Alerts to Chat Function

Posts every firing and resolved monitor alert to Slack, Discord, Microsoft Teams or Google Chat.

## 🧰 Usage

### Event: `monitors.*.alerts.*.firing`, `monitors.*.alerts.*.resolved`

Receives the alert as the event payload, formats it for the chat app behind `CHAT_WEBHOOK_URL` and posts it. Usage alerts show the metric, value, threshold and peak; uptime alerts show the check and what it found. A resolved alert is a new message with how long it fired.

Calls that aren't alert events, such as an HTTP request to the function's domain, get a `403`.

**Response**

Sample `200` Response:

```json
{ "ok": true, "target": "slack" }
```

Sample `502` Response, when the chat app refuses the message:

```json
{ "ok": false, "target": "slack", "status": 404 }
```

## ⚙️ Configuration

| Setting | Value |
| ----------------- | ------------------------------------------------------------ |
| Runtime | Node (18.0) |
| Entrypoint | `src/main.js` |
| Build Commands | `npm install` |
| Permissions | None |
| Events | `monitors.*.alerts.*.firing`, `monitors.*.alerts.*.resolved` |
| Timeout (Seconds) | 15 |

## 🔒 Environment Variables

### CHAT_WEBHOOK_URL

Incoming webhook URL of the channel to post to. The host picks the format:

| Host | Chat app |
| ---------------------------------------------------------- | --------------- |
| `hooks.slack.com` | Slack |
| `discord.com/api/webhooks/…` | Discord |
| `*.environment.api.powerplatform.com`, `*.logic.azure.com` | Microsoft Teams |
| `chat.googleapis.com` | Google Chat |

| Question | Answer |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Required | Yes |
| Sample Value | `https://hooks.slack.com/services/T000/B000/XXXX` |
| Documentation | [Slack](https://api.slack.com/messaging/webhooks), [Discord](https://support.discord.com/hc/en-us/articles/228383668), [Teams](https://support.microsoft.com/en-us/office/create-incoming-webhooks-with-workflows-for-microsoft-teams-8ae491c7-0394-4861-ba59-055e33f75498), [Google Chat](https://developers.google.com/workspace/chat/quickstart/webhooks) |

### CHAT_TARGET

The chat app to format for when the webhook URL's host doesn't say, such as a proxy: `slack`, `discord`, `teams` or `google-chat`.

| Question | Answer |
| ------------ | ------- |
| Required | No |
| Sample Value | `slack` |

## 📝 Notes

- Events are delivered at least once, so a chat app can rarely show the same alert twice.
- Microsoft Teams Office 365 connector URLs (`*.webhook.office.com`) are retired. Create the webhook with the Workflows app instead.
34 changes: 34 additions & 0 deletions node/alerts-to-chat/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions node/alerts-to-chat/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "alerts-to-chat",
"version": "1.0.0",
"description": "",
"main": "src/main.js",
"type": "module",
"scripts": {
"format": "prettier --write ."
},
"devDependencies": {
"prettier": "^3.2.5"
},
"engines": {
"node": ">=18.17.0"
}
}
Loading
Loading