继续讨论:https://www.nodeloc.com/d/8254
如图:
[upl-image-preview url=https://s.rmimg.com/2024-09-04/1725454755-820946-image.png]
cloudflare worker代码:
```
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
// 用户id
let userId = 3732
// 用户令牌 到个人主页-安全-开发者令牌中申请
let token = “”
// Telegram Bot Token
let telegramBotToken = “YOUR_TELEGRAM_BOT_TOKEN”
// Telegram Chat ID
let telegramChatId = “YOUR_TELEGRAM_CHAT_ID”
var data = JSON.stringify({
“data”: {
“type”: “users”,
“attributes”: {
“allowCheckin”: false,
“checkin_days_count”: 3,
“checkin_type”: “R”
},
“id”: ${userId}
}
});
var config = {
method: ‘post’,
url: ‘https://www.nodeloc.com/api/users/’ + userId,
headers: {
‘Authorization’: "Token " + token,
‘x-http-method-override’: ‘PATCH’,
‘Content-Type’: ‘application/json’
},
body: data
};
try {
let response = await fetch(config.url, config);
let responseData = await response.json();
let res = responseData.data.attributes;
let { lastCheckinTime, checkin_last_time, lastCheckinMoney, checkin_days_count } = res;
// 通知内容
let content = `签到时间:${checkin_last_time},签到能量:${lastCheckinMoney}。累计签到:${checkin_days_count}天`;
// 发送到Telegram
await sendTelegram(content, telegramBotToken, telegramChatId);
// 记录日志
console.log(content);
} catch (error) {
console.log('签到失败: ’ + error.message);
await sendTelegram('签到失败: ’ + error.message, telegramBotToken, telegramChatId);
}
}
async function sendTelegram(content, botToken, chatId) {
let telegramUrl = https://api.telegram.org/bot${botToken}/sendMessage
;
let telegramData = {
chat_id: chatId,
text: "NodeLoc签到结果: " + content
};
try {
let response = await fetch(telegramUrl, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’,
},
body: JSON.stringify(telegramData)
});
if (!response.ok) {
console.log('Telegram通知发送失败');
} else {
console.log('Telegram通知发送成功');
}
} catch (error) {
console.log(‘Telegram通知发送错误:’, error);
}
}
```
修改里面的:
`userId`
`token`
`telegramBotToken `
`telegramChatId `
自己在后台设置个cron就行了