-
-
Notifications
You must be signed in to change notification settings - Fork 148
Add Discord webhook adapter #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
orangecoding
merged 5 commits into
orangecoding:master
from
maribowman:feature/initial-discord-adapter
Sep 27, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
67fabb5
Add Discord webhook adapter
maribowman f2bc27d
apply change from comment to not set null values for listing attributes
maribowman 878b04c
truncate embed title to not exceed the limit
maribowman 83f710c
minor formatting
maribowman 32a1408
apply changes from comments to avoid NPEs
maribowman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import fetch from 'node-fetch'; | ||
| import { getJob } from '../../services/storage/jobStorage.js'; | ||
| import { markdown2Html } from '../../services/markdown.js'; | ||
| import { normalizeImageUrl } from '../../utils.js'; | ||
|
|
||
| /** | ||
| * Generates an idempotent decimal color code. The input string-based color code is | ||
| * generated using the djb2 hash algorithm. | ||
| * | ||
| * @param {string} str - Input string as color code base | ||
| * @returns {number} Generated decimal color code (0 - 16777215) | ||
| */ | ||
| const generateColorFromString = (str) => { | ||
| let hash = 5381; // initial value | ||
| const input = String(str); | ||
|
|
||
| for (let i = 0; i < input.length; i++) { | ||
| // hash * 33 + charCode | ||
| hash = ((hash << 5) + hash) + input.charCodeAt(i); | ||
| // Ensure the hash is 32 bit | ||
| hash |= 0; | ||
| } | ||
|
|
||
| let positiveHash = hash >>> 0; | ||
| const maxColorValue = 16777215; | ||
| const colorDecimal = positiveHash % maxColorValue; | ||
|
|
||
| return colorDecimal; | ||
| }; | ||
|
|
||
| /** | ||
| * Creates an embed per listing | ||
| * (-> see https://birdie0.github.io/discord-webhooks-guide/structure/embeds.html). | ||
| * | ||
| * @param {string} jobKey - Key of job (used to set embed color) | ||
| * @param {object} listing - Object holding listing details | ||
| * @returns {object} Discord webhook embed | ||
| */ | ||
| const buildEmbed = (jobKey, listing) => { | ||
| const maxTitleLength = 252; // Max embed title length is 256 characters | ||
| let title = String(listing.title ?? 'N/A'); | ||
| if (title.length > maxTitleLength) { | ||
| title = title.substring(0, maxTitleLength) + '...'; | ||
| } | ||
|
|
||
| const fields = [ | ||
| { | ||
| name: 'Price', | ||
| value: String(listing.price ?? 'n/a'), | ||
| inline: true, | ||
| }, | ||
| { | ||
| name: 'Size', | ||
| value: listing?.size?.replace(/2m/g, 'm²') ?? 'n/a', | ||
| inline: true, | ||
| }, | ||
| { | ||
| name: 'Address', | ||
| value: String(listing.address ?? 'n/a'), | ||
| inline: true, | ||
| }, | ||
| ] | ||
|
|
||
| const embed = { | ||
| title: title, | ||
| color: generateColorFromString(jobKey), | ||
| url: listing.link, | ||
| fields: fields, | ||
| } | ||
|
|
||
| if (listing.image) { | ||
| embed.image = { | ||
| url: normalizeImageUrl(listing.image), | ||
| }; | ||
| } | ||
|
|
||
| return embed | ||
| }; | ||
|
|
||
| export const send = ({ serviceName, newListings, notificationConfig, jobKey }) => { | ||
| const adapter = notificationConfig.find((adapter) => adapter.id === config.id); | ||
| const webhookUrl = adapter?.fields?.webhookUrl; | ||
| if (!webhookUrl || newListings.length == 0) return Promise.resolve([]); | ||
|
|
||
| const job = getJob(jobKey); | ||
| const jobName = job?.name || jobKey; | ||
|
|
||
| const embeds = newListings.map((listing) => buildEmbed(jobKey, listing)); | ||
|
|
||
| const maxEmbedsPerMessage = 10; // Discord only allows up to 10 embeds | ||
| const webhookPromises = []; | ||
|
|
||
| for (let i = 0; i < embeds.length; i += maxEmbedsPerMessage) { | ||
| // Send multiple Discord messages with up to 10 embeds per message | ||
| const embedChunk = embeds.slice(i, i + maxEmbedsPerMessage); | ||
|
|
||
| const content = i === 0 ? `*${jobName}:* ${serviceName} found **${newListings.length}** new listings.` : ''; | ||
| const body = JSON.stringify({ | ||
| content: content, | ||
| embeds: embedChunk, | ||
| }) | ||
|
|
||
| const fetchPromise = fetch(webhookUrl, { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body, | ||
| }) | ||
| .catch(error => { | ||
| console.error(`Error sending Discord webhook for chunk starting at ${i}:`, error); | ||
| return Promise.reject(new Error(`Webhook failed: ${error.message}`)); | ||
| }); | ||
|
|
||
| webhookPromises.push(fetchPromise); | ||
| } | ||
|
|
||
| return Promise.allSettled(webhookPromises); | ||
| }; | ||
|
|
||
| export const config = { | ||
| id: 'discord_webhook', | ||
| name: 'Discord Webhook', | ||
| readme: markdown2Html('lib/notification/adapter/discord_webhook.md'), | ||
| description: 'Fredy will send new listings to the Discord channel of your choice.', | ||
| fields: { | ||
| webhookUrl: { | ||
| type: 'text', | ||
| label: 'Webhook URL', | ||
| description: 'The URL of the Discord webhook to send messages to.', | ||
| }, | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ### Discord Adapter | ||
|
|
||
| To use the [Discord](https://discord.com/) Adapter, you need to create a webhook on the Discord channel of your choice. You can follow the instructions of _Making A Webhook_ on [this support website](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks). | ||
| Once you have created a webhook, copy and paste the webhook URL. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.