diff --git a/data/config.example.lua b/data/config.example.lua index bd76c96..cda111b 100644 --- a/data/config.example.lua +++ b/data/config.example.lua @@ -11,5 +11,11 @@ notEnogthMoney = "message=Insufficient amount of krist sent.", notEnogthStock = "message=We don't have that much stock!", change = "message=Here is your change! Thanks for using our shop." - } + }, + webhooks = { + { + type = "discord", + url = "" + } + }, } diff --git a/src/backend.lua b/src/backend.lua index 2ffa987..71eaae7 100644 --- a/src/backend.lua +++ b/src/backend.lua @@ -1,6 +1,7 @@ local kristly = require("/src/libs/kristly") local utils = require("/src/utils") local logger = require("/src/logger"):new({ debugging = true }) +local webhooks = require("/src/webhook") logger:info("Starting Kristify! Thanks for choosing Kristify. <3") logger:debug("Debugging mode is enabled!") @@ -121,6 +122,19 @@ turtle.drop() end end + + local message = "Kristify: `" .. + transaction.from .. "` bought " .. amount .. "x " .. product.id .. " (" .. transaction.value .. "kst)" + + logger:debug("Running webhooks") + for _, webhook in ipairs(config.webhooks) do + logger:debug("Webhook: ", webhook.type, webhook.URL) + if webhook.type == "discord" then + webhooks.discord(webhook.URL, message) + elseif webhook.type == "googleChat" then + webhooks.googleChat(webhook.URL, message) + end + end end local function startKristly() diff --git a/src/webhook.lua b/src/webhook.lua new file mode 100644 index 0000000..e616358 --- /dev/null +++ b/src/webhook.lua @@ -0,0 +1,29 @@ +local webhooks = {} +local expect = require("cc.expect").expect +---Sends a webhook to a Discord Guild +---@param URL string The webhook URL +---@param message string The message to send +---@param username string|nil The username of the webhook sender +---@param avatar string|nil The link to the avatar sender +function webhooks.discord(URL, message, username, avatar) + expect(1, URL, "string") + expect(2, message, "string") + expect(3, username, "string", "nil") + expect(4, avatar, "string", "nil") + + username = username or "Kristify shop" + avatar = avatar or "https://media.discordapp.net/attachments/1014151202855976973/1014162892414783559/Kristify.png" + + http.post(URL, "content=" .. message .. "&username=" .. username .. "&avatar_url=" .. avatar) +end + +---Sends a webhook to a Google Chat (requires Google Workspace) +---@param URL string The webhook URL +---@param message string The messge to send +function webhooks.googleChat(URL, message) + http.post(URL, textutils.serialiseJSON({ + ["text"] = message + }), { ["Content-Type"] = "application/json; charset=UTF-8" }) +end + +return webhooks