ns上面好多人问chatid怎么获取,chatid 巴拉巴拉
机器人伺候
话不多说 开始开始
1. 直接使用(懒人配置)
- TG中搜索
@readme_tg_bot或直接点击链接 https://t.me/readme_tg_bot - 点击进入 输入/chatinfo 你就能看到你的chatid
- 获取群组的 chatid 需要邀请机器人进入你的群组
- 进入群组点击标题,在弹出的卡片中选择邀请新成员的界面
- 输入
readme_tg_bot并选择bot,然后右下角点击邀请 - 在对话中输入
/chatinfo就会返回当前对话的chatid,注意chatid是会带-的
要是哪天tg机器人被抬了,就往下看吧
2. 自己部署
- 创建你的bot,TG搜索
@botfather或点击 https://t.me/botfather - 输入
/newbot进入创建流程,依次输入bot的名称(显示的名字),bot的tg号需要以bot结尾,会返回一个token给你
请注意不要把token给到你不相信的人,相信的人最好也别给
3. 进入大善人的workers页面,并创建应用
4. 选择从Hello World! 开始
5. 这里输入你想要的名称,一般他会给你个随机字符串,可以不修改,然后点击部署
6. 然后会回到这个页面,点击编辑代码
7. 将下面的代码直接贴进去,点击右上角的代码
export default { async fetch(request, env, ctx) { const url = new URL(request.url); // 1) 初始化命令 + 自动设置 Webhook if (url.pathname === "/initCommands") { const origin = url.origin; const webhookUrl = origin + "/tg-webhook"; // 先设置命令 await setBotCommands(env.BOT_TOKEN); // 再设置 webhook await setWebhook(env.BOT_TOKEN, webhookUrl); return new Response( `Commands & webhook set.\nWebhook URL: ${webhookUrl}`, { status: 200 } ); } // 2) Telegram Webhook 回调 if (url.pathname === "/tg-webhook") { if (request.method !== "POST") { return new Response("OK", { status: 200 }); } let update; try { update = await request.json(); } catch (e) { return new Response("Bad Request", { status: 400 }); } const message = update.message; if (!message || !message.text) { return new Response("No message", { status: 200 }); } const chatId = message.chat.id; const text = message.text.trim(); if (text.startsWith("/help")) { const helpText = [ "可用命令:", "", "/chatinfo - 查看当前对话的 chat_id 等信息", "/help - 查看本帮助信息", ].join("\n"); await sendMessage(env.BOT_TOKEN, chatId, helpText); return new Response("OK", { status: 200 }); } if (text.startsWith("/chatinfo")) { const infoText = [ `chat_id: ${chatId}`, "", `对话类别: ${message.chat.type}`, message.chat.title ? `群组名称: ${message.chat.title}` : "", message.chat.username ? `用户名: @${message.chat.username}` : "", ] .filter(Boolean) .join("\n"); await sendMessage(env.BOT_TOKEN, chatId, infoText); return new Response("OK", { status: 200 }); } return new Response("OK", { status: 200 }); } // 3) 其他路径 return new Response("OK", { status: 200 }); },};/** * 发送消息 */async function sendMessage(botToken, chatId, text, options = {}) { const url = `https://api.telegram.org/bot${botToken}/sendMessage`; const body = { chat_id: chatId, text, ...options, }; const res = await fetch(url, { method: "POST", headers: { "content-type": "application/json", }, body: JSON.stringify(body), }); if (!res.ok) { const errorText = await res.text(); console.error("Telegram API error:", res.status, errorText); }}/** * 设置 Bot Commands */async function setBotCommands(botToken) { const url = `https://api.telegram.org/bot${botToken}/setMyCommands`; const commands = [ { command: "chatinfo", description: "查看当前对话的 chat_id" }, { command: "help", description: "查看命令说明" }, ]; const res = await fetch(url, { method: "POST", headers: { "content-type": "application/json", }, body: JSON.stringify({ commands }), }); const data = await res.json(); console.log("setMyCommands result:", data);}/** * 自动设置 Webhook */async function setWebhook(botToken, webhookUrl) { const url = `https://api.telegram.org/bot${botToken}/setWebhook`; const res = await fetch(url, { method: "POST", headers: { "content-type": "application/json", }, body: JSON.stringify({ url: webhookUrl }), }); const data = await res.json(); console.log("setWebhook result:", data);}
8. 设置BOT_TOKEN,拿到你第2步获取到的token,在worker的设置中配置你的BOT_TOKEN
9. 回到最开始的页面复制你的域名,这个域名是ns给你分配的也是第5步中设置的
10. 在浏览器中访问你的域名+/initCommands会返回设置成功
比如示例中的域名
floral-term-8e8b.readme-ns.workers.dev+/initCommands
完整的链接就是floral-term-8e8b.readme-ns.workers.dev/initCommands
然后你就可以跟着 上面 1. 直接使用的步骤使用了 只是readme_tg_bot需要改为你自己的
我的域名已经修改了访问是不通的
README 让你 README 一下
评论 (0)