-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fjord v0.1 integration (Webhook + Discord) (#264)
### Notes This PR adds support for webhooks into valkyrie to run automations / send workflow results to Discord. In order to function, a new `.env` variables need to be added as mentioned. This PR adds several new .ENV flags that need to be added on deployment ``` export HUBOT_WEBHOOK_URL="/discord/" export HUBOT_WEBHOOK_AUTH="INSERT passkey" export HUBOT_N8N_WEBHOOK="INSERT n8n router webhook URL" ``` ### Steps to deploy 1. First setup valkyrie deployment to have new .env flags. 2. Test with valkyrie.thesis.co endpoint ### Discord Commands Adds the following commands to discord server `/stale-issues owner repository: Grab a list of stale issues from an n8n workflow` `/issues owner repository: Grab a list of recent issues from an n8n workflow` `/activity owner repository: View activity stats from a specific repository` `/exec workflow-name: run an automation based on the workflow name on n8n router` `/debug : send adapter data to hubot console for debugging channel / thread / guild info` ### To do - [x] Setup valkyrie URL endpoint - [x] Threading for webhooks - [x] Setup ENV flags for webhook URLs - [x] Setup public facing URL on valkyrie n8s instance for webhook - [x] Add auth layer to webhook - [x] Implement proper discord commands - [x] Add dynamic routing to channels based on channel definition in email
- Loading branch information
Showing
7 changed files
with
471 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,87 @@ | ||
import { Client, TextChannel, ChannelType } from "discord.js" | ||
import { Robot } from "hubot" | ||
import express from "express" | ||
|
||
export default async function webhookDiscord( | ||
discordClient: Client, | ||
robot: Robot, | ||
) { | ||
async function sendToDiscordChannel( | ||
channelName: string, | ||
tagUser: string, | ||
title: string, | ||
message: string, | ||
) { | ||
let channel: TextChannel | undefined | ||
const guilds = discordClient.guilds.cache | ||
|
||
guilds.forEach((guild) => { | ||
const matchedChannel = guild.channels.cache.find( | ||
(ch) => ch.name === channelName && ch.type === ChannelType.GuildText, | ||
) | ||
|
||
if (matchedChannel && matchedChannel.type === ChannelType.GuildText) { | ||
channel = matchedChannel as TextChannel | ||
} | ||
}) | ||
|
||
if (!channel) | ||
throw new Error( | ||
"Text-based channel with the given name not found in any guild", | ||
) | ||
|
||
const memberIds = tagUser.split(",") | ||
const existingThread = channel.threads.cache.find( | ||
(thread) => thread.name === title, | ||
) | ||
|
||
if (existingThread) { | ||
await existingThread.send("@here") | ||
await existingThread.send(message) | ||
} else { | ||
const newThread = await channel.threads.create({ | ||
name: title, | ||
autoArchiveDuration: 60, | ||
reason: message, | ||
}) | ||
await newThread.send("@here") | ||
if (tagUser !== "0") { | ||
await Promise.all( | ||
memberIds.map((id) => newThread.members.add(id.trim())), | ||
) | ||
} | ||
await newThread.send(message) | ||
} | ||
} | ||
|
||
if (process.env.HUBOT_WEBHOOK_URL) { | ||
const webhookUrl = process.env.HUBOT_WEBHOOK_URL | ||
const requiredAuth = process.env.HUBOT_WEBHOOK_AUTH | ||
|
||
robot.router.post( | ||
`${webhookUrl}`, | ||
( | ||
req: express.Request, | ||
res: express.Response, | ||
next: express.NextFunction, | ||
) => { | ||
const authHeader = req.headers.authorization | ||
if (!authHeader || authHeader !== requiredAuth) { | ||
res.status(401).send("Unauthorized") | ||
} else { | ||
next() | ||
} | ||
}, | ||
async (req: express.Request, res: express.Response) => { | ||
const { channelName, tagUser, title, message } = req.body | ||
|
||
robot.logger.info( | ||
`Received data: channelName = ${channelName}, title = ${title}, tagged users = ${tagUser} , message = ${message}`, | ||
) | ||
await sendToDiscordChannel(channelName, tagUser, title, message) | ||
|
||
res.status(200).send("Message sent to Discord") | ||
}, | ||
) | ||
} | ||
} |
This file contains 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
Oops, something went wrong.