在cloudfalre worker部署NL站签到脚本加入电报通知

继续讨论: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就行了

管理别挂我号:ac01:

先点赞后操作

都这么嚣张吗

:baishengnv25:

越来越厉害了,签到估计要废了?

来个青龙TG通知版本?

能不能做个 github action 里能运行的版本,谢谢!

@“fscarmen”#p77190 用github action容易封github号,之前用来保活serv00号就没了,cf搞这个不会封

@“xjfkk”#p77191

应该不怕,套个 warp 运行 action 就行了。

@“fscarmen”#p77192 action套warp,这咋搞:ac01:

@“xjfkk”#p77193

https://github.com/fscarmen/warp-on-actions
\- name: Set up WARP
uses: fscarmen/[email protected]
with:
stack: dual # Optional. Support [ ipv4, ipv6, dual ]. Default is dual.

很奇怪,我在 vps 里运行下面的 check-in.js 是成功的,但放在 github 报错,这块不熟,麻烦你帮看看。

sing.yml

``` name: NodeLoc Check-in

on:
schedule:
- cron: ‘0 1 * * *’ # Runs at 1:00 UTC every day
workflow_dispatch: # Allows manual trigger

jobs:
check-in:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/[email protected]

- name: Set up Node.js
  uses: actions/[email protected]
  with:
    node-version: latest

- name: Install dependencies
  run: npm install node-fetch@2

- name: Set up WARP
  uses: fscarmen/[email protected]
  with:
    stack: dual   # Optional. Support [ ipv4, ipv6, dual ]. Default is dual.
  
- name: Run check-in script
  env:
    USER_ID: ${{ secrets.USER_ID }}
    TOKEN: ${{ secrets.TOKEN }}
    TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
    TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
  run: node check-in.js<i>

```

check-in.js

``` const fetch = require('node-fetch');

async function handleRequest() {
// Load environment variables
// 用户id
let userId = process.env.USER_ID;

// 用户令牌 到个人主页-安全-开发者令牌中申请
let token = process.env.TOKEN;

// Telegram Bot Token
let telegramBotToken = process.env.TELEGRAM_BOT_TOKEN;

// Telegram Chat ID
let telegramChatId = process.env.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);
}
}

// Run the script
handleRequest();
```

[upl-image-preview url=https://s.rmimg.com/2024-09-04/1725464205-919981-image.png]

@“fscarmen”#p77196 在github部署格式要进行转换,我之前保活serv00脚本遇到类似问题

@“xjfkk”#p77198

如何改啊,请大佬喂饭。

@“fscarmen”#p77199 明天再搞:ac01:

@“xjfkk”#p77200

同求一个青龙TG通知版

@“fscarmen”#p77196 脚本被封杀了:ac01: