Skip to content

Instantly share code, notes, and snippets.

@flashlab
Created November 13, 2023 08:57
Show Gist options
  • Save flashlab/26a3dd0e97e0eab4541c42106ba40a9a to your computer and use it in GitHub Desktop.
Save flashlab/26a3dd0e97e0eab4541c42106ba40a9a to your computer and use it in GitHub Desktop.
cf simple worker to make tg/wx/dingding notice for qbittorrent
/**
* cf simple worker to make tg/wx/dingding notice for qbittorrent.
*
* - Required variables:
* - SITE_LIST - e.g. {"hdh": "https://xxx.org/torrents.php?search=", "ttg": "https://xxx.im/browse.php?search_field="}
* - QBIT - link for qbit webui
* - TG_HOST - "https://api.telegram.org/"
* - TG_TOKEN - botxxx:xxxx
* - TG_CHAT_ID
* - WX_HOST - "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
* - WX_TOKEN
* - AL_HOST - "https://oapi.dingtalk.com/robot/send?access_token="
* - AL_TOKEN
*
* - api
* - method - post
* - payload - {"sp": "tg|wx|al", "t": "%N", "s": "KEY IN SITELIST"}
*/
export default {
async fetch(request, env) {
/**
* rawHtmlResponse returns HTML inputted directly
* into the worker script
* @param {string} html
*/
function rawHtmlResponse(html) {
return new Response(html, {
headers: {
'content-type': 'text/html;charset=UTF-8',
},
});
}
async function sendMessage(url, param) {
return fetch(url, {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(param)
});
}
/**
* readRequestBody reads in the incoming request body
* Use await readRequestBody(..) in an async function to get json object
* @param {Request} request the incoming request to read from
*/
async function readRequestBody(request) {
const contentType = request.headers.get('content-type');
if (contentType.includes('application/json')) {
return await request.json();
} else if (contentType.includes('application/text') || contentType.includes('text/html')) {
return {
t: request.text()
};
} else if (contentType.includes('form')) {
const formData = await request.formData();
const body = {};
for (const entry of formData.entries()) {
body[entry[0]] = entry[1];
}
return body;
} else {
// Perhaps some other type of data was submitted in the form
// like an image, or some other binary data.
return {};
}
}
const sites = JSON.parse(env.SITE_LIST);
const { url } = request;
if (url.includes('form')) {
return rawHtmlResponse('forbidden');
}
if (request.method === 'POST') {
const reqBody = await readRequestBody(request);
const respBody = [];
if (!('sp' in reqBody))
return new Response('Unauthorized', { status: 403 });
const site = reqBody['s'] || '';
const title = reqBody['t'] || '';
const sendtext = site ? `#${site}# ${title}` : title;
const sendurl = site in sites ? encodeURI(sites[site] + title) : env.QBIT;
if (reqBody['sp']?.includes('tg')) {
let param = {
chat_id: env.CHAT_ID,
parse_mode: 'markdown',
text: sendtext,
reply_markup: {
inline_keyboard: [
[{
text: '\u{1F648} qbit',
url: env.QBIT
}]
]
}
}
if (site in sites) param.reply_markup.inline_keyboard[0].push({
text: '\u{1F649} 站点',
url: sendurl
})
let resp = await sendMessage(`${env.TG_HOST}${env.TG_TOKEN}/sendMessage`, param);
respBody.push('tg: ' + resp.status)
}
if (reqBody['sp']?.includes('wx')) {
let param = {
msgtype: 'news',
news: {
articles: [{
title: "新种添加",
description: sendtext,
url: sendurl
}]
}
}
let resp = await sendMessage(`${env.WX_HOST}${env.WX_TOKEN}`, param);
respBody.push('wx: ' + resp.status)
}
if (reqBody['sp']?.includes('al')) {
let param = {
msgtype: 'link',
link: {
text: sendtext,
title: '新种添加',
messageUrl: sendurl
}
}
let resp = await sendMessage(`${env.AL_HOST}${env.AL_TOKEN}`, param);
respBody.push('al: ' + resp.status)
}
return new Response(respBody.join('\n'), { status: 201 })
} else if (request.method === 'GET') {
// return await fetchUrl();
return new Response('forbidden', { status: 500 });
}
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment