|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 12 | +exports.ping = exports.send = void 0; |
| 13 | +const node_fetch_1 = require("node-fetch"); |
| 14 | +const send = (webhook, request) => __awaiter(void 0, void 0, void 0, function* () { |
| 15 | + let finalPayload; |
| 16 | + if (typeof request === "string") { |
| 17 | + request = { message: request }; |
| 18 | + } |
| 19 | + if (webhook.startsWith("https://hooks.slack.com/services/")) { |
| 20 | + finalPayload = { |
| 21 | + text: request.message, |
| 22 | + }; |
| 23 | + } |
| 24 | + else if (webhook.startsWith("https://discord.com/api/webhooks")) { |
| 25 | + finalPayload = { |
| 26 | + content: request.message, |
| 27 | + }; |
| 28 | + } |
| 29 | + else { |
| 30 | + return { |
| 31 | + success: false, |
| 32 | + reason: "Invalid webhook", |
| 33 | + }; |
| 34 | + } |
| 35 | + return (0, node_fetch_1.default)(webhook, { |
| 36 | + method: "POST", |
| 37 | + body: JSON.stringify(finalPayload), |
| 38 | + headers: { |
| 39 | + "Content-Type": "application/json", |
| 40 | + }, |
| 41 | + }) |
| 42 | + .then((res) => __awaiter(void 0, void 0, void 0, function* () { |
| 43 | + if (res.ok) { |
| 44 | + return { |
| 45 | + success: true, |
| 46 | + response: yield res.text(), |
| 47 | + }; |
| 48 | + } |
| 49 | + else { |
| 50 | + throw new Error(res.statusText); |
| 51 | + } |
| 52 | + })) |
| 53 | + .catch((err) => { |
| 54 | + return { |
| 55 | + success: false, |
| 56 | + reason: err.message, |
| 57 | + }; |
| 58 | + }); |
| 59 | +}); |
| 60 | +exports.send = send; |
| 61 | +const ping = (webhook) => __awaiter(void 0, void 0, void 0, function* () { |
| 62 | + return yield send(webhook, { message: "Webhook pinged successfully!" }); |
| 63 | +}); |
| 64 | +exports.ping = ping; |
0 commit comments