Skip to content

Commit 95eb57a

Browse files
author
Medivh
committed
fix: use tg.sendData() with VPS fallback
1 parent 8a3ab03 commit 95eb57a

1 file changed

Lines changed: 33 additions & 14 deletions

File tree

news-recommend/index.html

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -268,31 +268,50 @@ <h1>📰 新闻推荐</h1>
268268
btn.classList.add("sending");
269269
btn.disabled = true;
270270
btn.textContent = "发送中...";
271-
log("发送到 VPS...");
272-
273-
// 测试端点:httpbin.org (支持跨域,返回收到的数据)
274-
const ENDPOINT = "https://httpbin.org/post";
275-
271+
272+
// 方案 1: 使用 tg.sendData() 直接发送到 Telegram Bot
273+
log("方案1: 通过 Telegram sendData 发送...");
274+
275+
try {
276+
if (typeof tg.sendData === 'function') {
277+
tg.sendData(JSON.stringify(data));
278+
log("✅ sendData 调用成功,等待 Bot 回复...");
279+
280+
btn.classList.remove("sending");
281+
btn.classList.add("success");
282+
btn.textContent = "✅ 已发送";
283+
showToast("发送成功!");
284+
285+
setTimeout(() => tg.close(), 2000);
286+
setTimeout(() => {
287+
selectedIds.clear();
288+
renderNews();
289+
updateBtn();
290+
btn.classList.remove("success");
291+
btn.textContent = "📡 发送选择";
292+
}, 3000);
293+
return;
294+
}
295+
} catch (e) {
296+
log("sendData 不可用: " + e.message);
297+
}
298+
299+
// 方案 2: 备用 VPS 端点
300+
log("方案1 失败,回退到 VPS 端点...");
301+
const ENDPOINT = "https://fc01a6f5b1d624.lhr.life/receive";
302+
276303
try {
277-
// 使用简单请求 (Simple Request) 避免 CORS Preflight 被拦截
278-
// 不设置 Content-Type,默认 text/plain,浏览器直接发送 POST
279304
const res = await fetch(ENDPOINT, {
280305
method: "POST",
281306
body: JSON.stringify(data)
282307
});
283308

284309
if (res.ok) {
285-
const result = await res.json();
286-
log("✅ 发送成功!");
287-
// 显示收到的数据摘要
288-
log("响应: " + (result.json ? "数据已到达服务器" : "未知响应"));
289-
310+
log("✅ VPS 发送成功!等待 Bot 回复...");
290311
btn.classList.remove("sending");
291312
btn.classList.add("success");
292313
btn.textContent = "✅ 已发送";
293314
showToast("发送成功!");
294-
295-
// 3 秒后重置
296315
setTimeout(() => {
297316
selectedIds.clear();
298317
renderNews();

0 commit comments

Comments
 (0)