跳转到主要内容

开发者快速接入:getMe、getUpdates、sendMessage(curl + Node.js)

用 curl 与 Node.js 搭起最小 MPChat Bot 接入:验证 token、读取 chat_id、发送第一条消息,内置 token 安全与错误处理。

最小链路:getMe(验证 token)→ getUpdates(拿 chat_id)→ sendMessage(发送)。token 从环境变量读取,绝不硬编码。


总览

这是各桌面系统通用的开发者主路径。基础 URL:

https://call.mp.net/bot/bot{MPCHAT_BOT_TOKEN}/{methodName}

凭证放进环境变量或被 git 忽略的 .env

MPCHAT_BOT_TOKEN=your_mpbot_token_here
MPCHAT_CHAT_ID=your_chat_id_here

1. getMe(验证 token)

curl -s "https://call.mp.net/bot/bot${MPCHAT_BOT_TOKEN}/getMe"

const token = process.env.MPCHAT_BOT_TOKEN;
const res = await fetch(`https://call.mp.net/bot/bot${token}/getMe`);
const data = await res.json();
if (!data.ok) throw new Error(`${data.error_code}: ${data.description}`);
console.log(data.result); // { id, is_bot, username }

401 表示 token 无效或已 revoke。

2. getUpdates(获取 chat_id)

curl -s -X POST "https://call.mp.net/bot/bot${MPCHAT_BOT_TOKEN}/getUpdates" \
  -H "Content-Type: application/json" \
  -d '{"timeout":30,"limit":50,"allowed_updates":["message"]}'

const res = await fetch(`https://call.mp.net/bot/bot${token}/getUpdates`, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ timeout: 30, limit: 50, allowed_updates: ["message"] }),
});
const data = await res.json();
if (!data.ok) throw new Error(`${data.error_code}: ${data.description}`);
const chatId = data.result?.find((u) => u.message)?.message?.chat?.id;

成功响应会消费该批 updates;持续轮询时传 offset = 上一批最大 update_id + 1。返回 409 表示已启用 Webhook。

3. sendMessage(发送第一条消息)

curl -s -X POST "https://call.mp.net/bot/bot${MPCHAT_BOT_TOKEN}/sendMessage" \
  -H "Content-Type: application/json" \
  -d "{\"chat_id\":\"${MPCHAT_CHAT_ID}\",\"text\":\"Hello from mpbot\"}"

const res = await fetch(`https://call.mp.net/bot/bot${token}/sendMessage`, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ chat_id: process.env.MPCHAT_CHAT_ID, text: "Hello from mpbot" }),
});
const data = await res.json();
if (!data.ok) throw new Error(`${data.error_code}: ${data.description}`);

请求前检查清单

  • token 从环境变量读取,绝不硬编码或提交到仓库。

  • ID 类型符合文档约定(安全整数或十进制字符串)。

  • POST body 是合法 JSON,带 Content-Type: application/json

  • 媒体方法使用公网 URL 或 multipart(不复用 file_id)。

相关文章

默认限流为每 token 每秒 30 次请求,超限返回 429。上线前请加入指数退避重试。

这是否解答了您的问题?