Skip to main content

Bot API examples: send and receive test messages with curl or Node.js

Build a minimal MPChat bot with curl and Node.js: verify the token, read a chat_id, and send your first message—with token safety and error handling.

This guide provides curl and Node.js test examples for developers. For guided Windows/macOS instructions, choose the first-test guide; you do not need to complete both. Run your first MPChat Bot: mobile testing and Windows/macOS debugging

Verify your token with getMe, read a test conversation with getUpdates, and send your first reply with sendMessage.


Send your first Bot message

  1. On your phone, open MPChat → Contacts → Bots to reach BotFather. Create a dedicated test Bot or select one you already manage, then copy its token. See the BotFather token instructions.

  2. Open the test bot's private chat in MPChat and send mpchat-quickstart.

  3. Use a test bot with no active webhook or other process polling for updates.

Set MPCHAT_BOT_TOKEN in the environment of the terminal you will use. Keep it out of source files, shell history, screenshots, and logs.

Option A · curl

Run these commands in Bash or zsh. First, confirm that the returned username belongs to your test bot.

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

Find the private message containing mpchat-quickstart in result and copy its message.chat.id.

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

Replace REPLACE_WITH_CHAT_ID with that ID and send the reply.

export MPCHAT_CHAT_ID='REPLACE_WITH_CHAT_ID'
curl -sS -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 MPChat Bot.\"}"

Option B · Node.js

Save the complete example as quickstart.mjs and run node quickstart.mjs using Node.js 22 or later. If you ran the curl path first, send a new test message before running the script.

const token = process.env.MPCHAT_BOT_TOKEN;
if (!token) throw new Error("Set MPCHAT_BOT_TOKEN before running this file.");

async function call(method, body) {
let response;
try {
response = await fetch(
`https://call.mp.net/bot/bot${token}/${method}`,
body === undefined ? undefined : {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
},
);
} catch {
throw new Error(`${method}: network request failed.`);
}

const data = await response.json().catch(() => null);
const code = data?.error_code ?? response.status;
if (code === 409 && method === "getUpdates") {
throw new Error("getUpdates: 409. Use a test bot without a webhook.");
}
if (!response.ok || !data?.ok) {
throw new Error(`${method}: request failed (${code}).`);
}
return data.result;
}

const bot = await call("getMe");
console.log(`Connected as @${bot.username}`);

const updates = await call("getUpdates", {
timeout: 10,
limit: 50,
allowed_updates: ["message"],
});
const matchingChatIds = new Set(updates
.filter(({ message }) => message?.chat?.type === "private" &&
message.text === "mpchat-quickstart")
.map(({ message }) => message.chat.id));
if (matchingChatIds.size !== 1) {
throw new Error("Use a test bot with exactly one matching private chat, then send a new test message.");
}
const [chatId] = matchingChatIds;
if (!(Number.isSafeInteger(chatId) && chatId >= 0) &&
!(typeof chatId === "string" && /^[0-9]+$/.test(chatId))) {
throw new Error("Use a valid Bot API chat ID from the update.");
}
const sent = await call("sendMessage", {
chat_id: String(chatId),
text: "Hello from MPChat Bot.",
});
console.log(`Sent message ${sent.message_id}`);

The Bot should reply with Hello from MPChat Bot. in your private chat. A successful send returns a message_id.

Troubleshooting

  • 401 — Check that the token belongs to your test bot and is still valid.

  • Empty result or no matching message — Send mpchat-quickstart again. A successful getUpdates call consumes only the updates returned in that response.

  • 409 — The bot has an active webhook. Switch to a dedicated test bot without a webhook.

  • 400 on sendMessage — Use the message.chat.id from the test conversation as a decimal string.

Next steps

Did this answer your question?