MCPcopy Create free account
hub / github.com/smallfawn/QLScriptPublic / ntfyNotify

Function ntfyNotify

sendNofity.js:1281–1339  ·  view source on GitHub ↗
(text, desp)

Source from the content-addressed store, hash-verified

1279}
1280
1281function ntfyNotify(text, desp) {
1282 function encodeRFC2047(text) {
1283 const encodedBase64 = Buffer.from(text).toString('base64');
1284 return `=?utf-8?B?${encodedBase64}?=`;
1285 }
1286
1287 return new Promise((resolve) => {
1288 const {
1289 NTFY_URL,
1290 NTFY_TOPIC,
1291 NTFY_PRIORITY,
1292 NTFY_TOKEN,
1293 NTFY_USERNAME,
1294 NTFY_PASSWORD,
1295 NTFY_ACTIONS,
1296 } = push_config;
1297 if (NTFY_TOPIC) {
1298 const options = {
1299 url: `${NTFY_URL || 'https://ntfy.sh'}/${NTFY_TOPIC}`,
1300 body: `${desp}`,
1301 headers: {
1302 Title: `${encodeRFC2047(text)}`,
1303 Priority: NTFY_PRIORITY || '3',
1304 Icon: 'https://qn.whyour.cn/logo.png',
1305 },
1306 timeout,
1307 };
1308 if (NTFY_TOKEN) {
1309 options.headers['Authorization'] = `Bearer ${NTFY_TOKEN}`;
1310 } else if (NTFY_USERNAME && NTFY_PASSWORD) {
1311 options.headers['Authorization'] =
1312 `Basic ${Buffer.from(`${NTFY_USERNAME}:${NTFY_PASSWORD}`).toString('base64')}`;
1313 }
1314 if (NTFY_ACTIONS) {
1315 options.headers['Actions'] = encodeRFC2047(NTFY_ACTIONS);
1316 }
1317
1318 $.post(options, (err, resp, data) => {
1319 try {
1320 if (err) {
1321 console.log('Ntfy 通知调用API失败😞\n', err);
1322 } else {
1323 if (data.id) {
1324 console.log('Ntfy 发送通知消息成功🎉\n');
1325 } else {
1326 console.log(`Ntfy 发送通知消息异常 ${JSON.stringify(data)}`);
1327 }
1328 }
1329 } catch (e) {
1330 $.logErr(e, resp);
1331 } finally {
1332 resolve(data);
1333 }
1334 });
1335 } else {
1336 resolve();
1337 }
1338 });

Callers 1

sendNotifyFunction · 0.70

Calls 4

encodeRFC2047Function · 0.70
postMethod · 0.45
logMethod · 0.45
logErrMethod · 0.45

Tested by

no test coverage detected