快速查询硅基流动余额 适合拥有多个账号key的老铁
直接cloudflare部署即可,支持刷新查询,用的缓存,将就用吧。
效果:
// tgbot机器人 查询硅基流动key余额及信息
// cloudflare-worker-bot.js
// 部署后执行https://域名/init_webhook 设置tg的webhook。
// tg @hkxiaoyao
const TELEGRAM_BOT_TOKEN = 'your_telegram_bot_token'; // 替换为你的 Telegram Bot Token 只需要修改这里
const SILICON_FLOW_API_URL = 'https://api.siliconflow.cn/v1/user/info'; // 硅基流动 API 地址
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
const path = url.pathname;
// 初始化 Webhook 地址(仅在访问 /init_webhook 时触发)
if (path === '/init_webhook') {
const webhookUrl = `https://${url.host}/webhook`;
return await initWebhook(webhookUrl);
}
// Telegram 机器人逻辑
if (request.method === 'POST') {
const json = await request.json();
if (json.message) {
// 处理普通消息
return await handleTelegramMessage(json.message);
} else if (json.callback_query) {
// 处理回调查询(刷新按钮点击事件)
return await handleCallbackQuery(json.callback_query);
}
}
return new Response('Not Found', { status: 404 });
}
async function initWebhook(webhookUrl) {
const response = await fetch(`https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/setWebhook`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: webhookUrl
})
});
const data = await response.json();
return new Response(JSON.stringify(data), {
headers: { 'Content-Type': 'application/json' }
});
}
async function handleTelegramMessage(message) {
const chatId = message.chat.id;
const text = message.text;
// 检查文本中是否包含 sk- 开头且长度为 51 位的 key
const keyPattern = /sk-[a-zA-Z0-9\-]{48}/g; // 严格匹配 sk- 开头且 48 位的 key
const keys = text.match(keyPattern);
if (keys) {
const key = keys[0]; // 获取第一个匹配的 key
try {
// 调用硅基流动 API 查询用户信息
const siliconFlowResponse = await fetch(SILICON_FLOW_API_URL, {
method: 'GET',
headers: {
'Authorization': `Bearer ${key}`
}
});
if (siliconFlowResponse.status !== 200) {
return sendTelegramMessage(chatId, '❌ 无效的 key 或 API 错误');
}
const siliconFlowData = await siliconFlowResponse.json();
const userInfo = siliconFlowData.data;
// 获取当前东八区时间并格式化
const now = new Date();
now.setHours(now.getHours() + 8); // 转换为东八区时间
const formattedDate = now.toISOString().replace('T', ' ').substring(0, 19);
// 构造回复文本(保留符号)
const responseText = `
🖥️ 用户ID:${userInfo.id}
🚀 用户名:${userInfo.name}
🌐 邮箱:${userInfo.email}
⏳ 账户状态:${userInfo.status}
📈 赠送余额:${userInfo.balance}
📋 充值余额:${userInfo.chargeBalance}
🌐 总余额:${userInfo.totalBalance}
🕒 查询时间:${formattedDate}
`.trim();
// 构造带有刷新按钮的键盘
const keyboard = [
[{
text: '🔄 刷新查询',
callback_data: `/query ${key}`
}]
];
return sendTelegramMessageWithKeyboard(chatId, responseText, keyboard);
} catch (error) {
console.error(error);
return sendTelegramMessage(chatId, '❌ 处理请求时发生错误');
}
} else {
// 如果没有检测到有效的 key,则回复提示信息
return sendTelegramMessage(chatId, '❌ 请发送一个有效的硅基流动 key,格式为 sk- 开头且长度为 51 位');
}
}
async function handleCallbackQuery(callbackQuery) {
const { message, data } = callbackQuery;
const chatId = message.chat.id;
const messageId = message.message_id;
const key = data.split(' ')[1]; // 提取回调数据中的 key
try {
// 调用硅基流动 API 查询用户信息
const siliconFlowResponse = await fetch(SILICON_FLOW_API_URL, {
method: 'GET',
headers: {
'Authorization': `Bearer ${key}`
}
});
if (siliconFlowResponse.status !== 200) {
return editTelegramMessage(chatId, messageId, '❌ 无效的 key 或 API 错误');
}
const siliconFlowData = await siliconFlowResponse.json();
const userInfo = siliconFlowData.data;
// 获取当前东八区时间并格式化
const now = new Date();
now.setHours(now.getHours() + 8); // 转换为东八区时间
const formattedDate = now.toISOString().replace('T', ' ').substring(0, 19);
// 构造回复文本(保留符号)
const responseText = `
🖥️ 用户ID:${userInfo.id}
🚀 用户名:${userInfo.name}
🌐 邮箱:${userInfo.email}
⏳ 账户状态:${userInfo.status}
📈 赠送余额:${userInfo.balance}
📋 充值余额:${userInfo.chargeBalance}
🌐 总余额:${userInfo.totalBalance}
🕒 查询时间:${formattedDate}
`.trim();
// 构造带有刷新按钮的键盘
const keyboard = [
[{
text: '🔄 刷新查询',
callback_data: `/query ${key}`
}]
];
// 修改原消息内容
return editTelegramMessageWithKeyboard(chatId, messageId, responseText, keyboard);
} catch (error) {
console.error(error);
return editTelegramMessage(chatId, messageId, '❌ 处理请求时发生错误');
}
}
async function editTelegramMessage(chatId, messageId, text) {
const url = `https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/editMessageText`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
chat_id: chatId,
message_id: messageId,
text: text
})
});
return new Response(JSON.stringify(await response.json()), {
headers: { 'Content-Type': 'application/json' }
});
}
async function editTelegramMessageWithKeyboard(chatId, messageId, text, keyboard) {
const url = `https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/editMessageText`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
chat_id: chatId,
message_id: messageId,
text: text,
reply_markup: {
inline_keyboard: keyboard
}
})
});
return new Response(JSON.stringify(await response.json()), {
headers: { 'Content-Type': 'application/json' }
});
}
async function sendTelegramMessage(chatId, text) {
const url = `https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
chat_id: chatId,
text: text
})
});
return new Response(JSON.stringify(await response.json()), {
headers: { 'Content-Type': 'application/json' }
});
}
async function sendTelegramMessageWithKeyboard(chatId, text, keyboard) {
const url = `https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
chat_id: chatId,
text: text,
reply_markup: {
inline_keyboard: keyboard
}
})
});
return new Response(JSON.stringify(await response.json()), {
headers: { 'Content-Type': 'application/json' }
});
}