1
0
Fork 0
mirror of synced 2024-06-27 18:40:42 +12:00

Adding slack integration.

This commit is contained in:
mike12345567 2021-09-07 13:58:53 +01:00
parent 879f1a2368
commit d05c60d470
7 changed files with 102 additions and 11 deletions

View file

@ -35,10 +35,6 @@ exports.APP_PREFIX = DocumentTypes.APP + SEPARATOR
exports.APP_DEV = exports.APP_DEV_PREFIX = DocumentTypes.APP_DEV + SEPARATOR
exports.SEPARATOR = SEPARATOR
function isDevApp(app) {
return app.appId.startsWith(exports.APP_DEV_PREFIX)
}
/**
* If creating DB allDocs/query params with only a single top level ID this can be used, this
* is usually the case as most of our docs are top level e.g. tables, automations, users and so on.
@ -62,6 +58,18 @@ function getDocParams(docType, docId = null, otherProps = {}) {
}
}
exports.isDevAppID = appId => {
return appId.startsWith(exports.APP_DEV_PREFIX)
}
exports.isProdAppID = appId => {
return appId.startsWith(exports.APP_PREFIX) && !exports.isDevAppID(appId)
}
function isDevApp(app) {
return exports.isDevAppID(app.appId)
}
/**
* Generates a new workspace ID.
* @returns {string} The new workspace ID which the workspace doc can be stored under.

View file

@ -280,3 +280,5 @@ exports.trigger = async function (ctx) {
automation,
}
}
exports.test = async function (ctx) {}

View file

@ -9,7 +9,7 @@ const executeQuery = require("./steps/executeQuery")
const outgoingWebhook = require("./steps/outgoingWebhook")
const serverLog = require("./steps/serverLog")
const discord = require("./steps/discord")
// TODO: remove zapier/integromat some time in the future/deprecate them
const slack = require("./steps/slack")
const zapier = require("./steps/zapier")
const integromat = require("./steps/integromat")
@ -26,6 +26,7 @@ const ACTION_IMPLS = {
SERVER_LOG: serverLog.run,
// these used to be lowercase step IDs, maintain for backwards compat
discord: discord.run,
slack: slack.run,
zapier: zapier.run,
integromat: integromat.run,
}
@ -42,6 +43,7 @@ const ACTION_DEFINITIONS = {
SERVER_LOG: serverLog.definition,
// these used to be lowercase step IDs, maintain for backwards compat
discord: discord.definition,
slack: slack.definition,
zapier: zapier.definition,
integromat: integromat.definition,
}

View file

@ -1,5 +1,8 @@
const fetch = require("node-fetch")
const DEFAULT_USERNAME = "Budibase Automate"
const DEFAULT_AVATAR_URL = "https://i.imgur.com/a1cmTKM.png"
exports.definition = {
name: "Discord Message",
tagline: "Send a message to a Discord server",
@ -7,10 +10,7 @@ exports.definition = {
icon: "ri-discord-line",
stepId: "discord",
type: "ACTION",
inputs: {
username: "Budibase Automate",
avatar_url: "https://i.imgur.com/a1cmTKM.png",
},
inputs: {},
schema: {
inputs: {
properties: {
@ -39,14 +39,23 @@ exports.definition = {
type: "number",
description: "The HTTP status code of the request",
},
success: {
type: "boolean",
description: "Whether the message sent successfully",
},
},
},
},
}
exports.run = async function ({ inputs }) {
const { url, username, avatar_url, content } = inputs
let { url, username, avatar_url, content } = inputs
if (!username) {
username = DEFAULT_USERNAME
}
if (!avatar_url) {
avatar_url = DEFAULT_AVATAR_URL
}
const response = await fetch(url, {
method: "post",
body: JSON.stringify({
@ -61,5 +70,6 @@ exports.run = async function ({ inputs }) {
return {
httpStatus: response.status,
success: response.status === 200,
}
}

View file

@ -0,0 +1,59 @@
const fetch = require("node-fetch")
const DEFAULT_USERNAME = "Budibase Automate"
const DEFAULT_ICON_URL = "https://i.imgur.com/a1cmTKM.png"
exports.definition = {
name: "Slack Message",
tagline: "Send a message to Slack",
description: "Send a message to Slack",
icon: "ri-slack-line",
stepId: "slack",
type: "ACTION",
inputs: {},
schema: {
inputs: {
properties: {
url: {
type: "string",
title: "Incoming Webhook URL",
},
text: {
type: "string",
title: "Message",
},
},
required: ["url", "text"],
},
outputs: {
properties: {
httpStatus: {
type: "number",
description: "The HTTP status code of the request",
},
success: {
type: "boolean",
description: "Whether the message sent successfully",
},
},
},
},
}
exports.run = async function ({ inputs }) {
let { url, text } = inputs
const response = await fetch(url, {
method: "post",
body: JSON.stringify({
text,
}),
headers: {
"Content-Type": "application/json",
},
})
return {
httpStatus: response.status,
success: response.status === 200,
}
}

View file

@ -9,6 +9,7 @@ const { coerce } = require("../utilities/rowProcessor")
const { utils } = require("@budibase/auth/redis")
const { JobQueues } = require("../constants")
const { definitions } = require("./triggerInfo")
const { isDevAppID } = require("../db/utils")
const { opts } = utils.getRedisOptions()
let automationQueue = new Queue(JobQueues.AUTOMATIONS, { redis: opts })
@ -19,6 +20,11 @@ async function queueRelevantRowAutomations(event, eventType) {
if (event.appId == null) {
throw `No appId specified for ${eventType} - check event emitters.`
}
// don't queue events which are for dev apps, only way to test automations is
// running tests on them
if (isDevAppID(event.appId)) {
return
}
const db = new CouchDB(event.appId)
let automations = await db.allDocs(
getAutomationParams(null, { include_docs: true })

View file

@ -7,6 +7,8 @@ const {
APP_PREFIX,
SEPARATOR,
StaticDatabases,
isDevAppID,
isProdAppID,
} = require("@budibase/auth/db")
const UNICODE_MAX = "\ufff0"
@ -62,6 +64,8 @@ const BudibaseInternalDB = {
exports.APP_PREFIX = APP_PREFIX
exports.APP_DEV_PREFIX = APP_DEV_PREFIX
exports.isDevAppID = isDevAppID
exports.isProdAppID = isProdAppID
exports.USER_METDATA_PREFIX = `${DocumentTypes.ROW}${SEPARATOR}${InternalTables.USER_METADATA}${SEPARATOR}`
exports.LINK_USER_METADATA_PREFIX = `${DocumentTypes.LINK}${SEPARATOR}${InternalTables.USER_METADATA}${SEPARATOR}`
exports.ViewNames = ViewNames