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

Function ntfyNotify

tools/sendNotify.js:1277–1335  ·  view source on GitHub ↗
(text, desp)

Source from the content-addressed store, hash-verified

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

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