2021-09-04 04:36:00 +12:00
|
|
|
const fetch = require("node-fetch")
|
2021-09-08 04:31:54 +12:00
|
|
|
const { getFetchResponse } = require("./utils")
|
2021-09-04 04:36:00 +12:00
|
|
|
|
2021-09-08 00:58:53 +12:00
|
|
|
const DEFAULT_USERNAME = "Budibase Automate"
|
|
|
|
const DEFAULT_AVATAR_URL = "https://i.imgur.com/a1cmTKM.png"
|
|
|
|
|
2021-09-07 04:53:02 +12:00
|
|
|
exports.definition = {
|
2021-09-04 04:36:00 +12:00
|
|
|
name: "Discord Message",
|
|
|
|
tagline: "Send a message to a Discord server",
|
|
|
|
description: "Send a message to a Discord server",
|
|
|
|
icon: "ri-discord-line",
|
|
|
|
stepId: "discord",
|
|
|
|
type: "ACTION",
|
2021-09-14 23:40:19 +12:00
|
|
|
internal: false,
|
2021-09-08 00:58:53 +12:00
|
|
|
inputs: {},
|
2021-09-04 04:36:00 +12:00
|
|
|
schema: {
|
|
|
|
inputs: {
|
|
|
|
properties: {
|
|
|
|
url: {
|
|
|
|
type: "string",
|
|
|
|
title: "Discord Webhook URL",
|
|
|
|
},
|
|
|
|
username: {
|
|
|
|
type: "string",
|
|
|
|
title: "Bot Name",
|
|
|
|
},
|
|
|
|
avatar_url: {
|
|
|
|
type: "string",
|
|
|
|
title: "Bot Avatar URL",
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
type: "string",
|
|
|
|
title: "Message",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: ["url", "content"],
|
|
|
|
},
|
|
|
|
outputs: {
|
|
|
|
properties: {
|
|
|
|
httpStatus: {
|
|
|
|
type: "number",
|
|
|
|
description: "The HTTP status code of the request",
|
|
|
|
},
|
2021-09-08 04:31:54 +12:00
|
|
|
response: {
|
|
|
|
type: "string",
|
|
|
|
description: "The response from the Discord Webhook",
|
|
|
|
},
|
2021-09-08 00:58:53 +12:00
|
|
|
success: {
|
|
|
|
type: "boolean",
|
|
|
|
description: "Whether the message sent successfully",
|
|
|
|
},
|
2021-09-04 04:36:00 +12:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-07 04:53:02 +12:00
|
|
|
exports.run = async function ({ inputs }) {
|
2021-09-08 00:58:53 +12:00
|
|
|
let { url, username, avatar_url, content } = inputs
|
|
|
|
if (!username) {
|
|
|
|
username = DEFAULT_USERNAME
|
|
|
|
}
|
|
|
|
if (!avatar_url) {
|
|
|
|
avatar_url = DEFAULT_AVATAR_URL
|
|
|
|
}
|
2021-09-04 04:36:00 +12:00
|
|
|
const response = await fetch(url, {
|
|
|
|
method: "post",
|
|
|
|
body: JSON.stringify({
|
|
|
|
username,
|
|
|
|
avatar_url,
|
|
|
|
content,
|
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-09-08 04:31:54 +12:00
|
|
|
const { status, message } = await getFetchResponse(response)
|
2021-09-04 04:36:00 +12:00
|
|
|
return {
|
2021-09-08 04:31:54 +12:00
|
|
|
httpStatus: status,
|
2021-10-13 04:13:54 +13:00
|
|
|
success: status === 200 || status === 204,
|
2021-09-08 04:31:54 +12:00
|
|
|
response: message,
|
2021-09-04 04:36:00 +12:00
|
|
|
}
|
|
|
|
}
|