只想用现成的机器人?看看 好用的机器人:提醒、待办、翻译、群投票…… 加上就能用。
无忌机器人接口
用任何能发 HTTP 请求的语言都能写无忌机器人:调接口发消息,收消息用长轮询或 Webhook。
1. 创建机器人
- 在无忌里搜索
@wujiMother,给它发/newbot。 - 按提示输入名字和用户名(必须以
bot结尾,例如WeatherBot)。 - 它会回复一个令牌(token),形如
1234567890:AbCdEf…。拿到令牌就能控制机器人,请保管好;丢了发/token重新生成,旧令牌立即失效。
2. 调用方式
https://本站域名/bot<令牌>/<方法名>
GET 或 POST 均可;参数放在网址里、表单(application/x-www-form-urlencoded)或 JSON(application/json)。返回:
{"ok": true, "result": …}
{"ok": false, "error_code": 400, "description": "Bad Request: …"}
curl https://本站域名/bot<令牌>/getMe
curl -X POST https://本站域名/bot<令牌>/sendMessage \
-H 'Content-Type: application/json' \
-d '{"chat_id": 1000000002, "text": "你好"}'
Python(只用 requests)
import requests
API = "https://本站域名/bot令牌/"
offset = 0
while True:
r = requests.post(API + "getUpdates", json={"offset": offset, "timeout": 50}, timeout=60).json()
for u in r["result"]:
offset = u["update_id"] + 1
if "message" in u and u["message"].get("text") == "/start":
chat = u["message"]["chat"]["id"]
keyboard = {"inline_keyboard": [[{"text": "点我", "callback_data": "hi"}]]}
requests.post(API + "sendMessage", json={"chat_id": chat, "text": "<b>你好!</b>",
"parse_mode": "HTML", "reply_markup": keyboard})
elif "callback_query" in u:
q = u["callback_query"]
requests.post(API + "answerCallbackQuery", json={"callback_query_id": q["id"], "text": "收到"})
requests.post(API + "editMessageText", json={"chat_id": q["message"]["chat"]["id"],
"message_id": q["message"]["message_id"], "text": "你点了按钮"})
Node.js(不用装库,Node 18 以上)
const API = "https://本站域名/bot令牌/";
const call = (method, body) => fetch(API + method, {
method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body),
}).then((r) => r.json());
let offset = 0;
for (;;) {
const { result } = await call("getUpdates", { offset, timeout: 50 });
for (const u of result) {
offset = u.update_id + 1;
if (u.message?.text === "/start") await call("sendMessage", { chat_id: u.message.chat.id, text: "你好!" });
}
}
3. 收消息
两种方式二选一:
- 长轮询:反复调用
getUpdates,带上offset(上次最大update_id+ 1)确认已收到,timeout最长 50 秒。 - Webhook:调用
setWebhook设一个 https 地址(端口 443 / 80 / 88 / 8443),有新消息时服务器 POST 给你。设置了secret_token时,请求头X-Wuji-Bot-Secret会带上它。返回 2xx 视为成功,失败会自动重试,最近一次错误可用getWebhookInfo查看。
想让用户一键把机器人拉进群:给出链接 /u/机器人用户名?startgroup=参数。用户选好群后,群里会出现一条 /start@机器人用户名 参数,机器人据此知道是从哪里来的。
没取走的消息保留 24 小时。目前推送的 Update 类型:message、callback_query(有人点了按钮)、my_chat_member(机器人被拉进群)、channel_post(频道里其他管理员发的消息)。
用户发来的图片、视频、文件在 photo / video / document 里,file_id 交给 getFile 拿到 file_path,再从 https://本站/file/bot<令牌>/<file_path> 下载。
4. 规则
- 私聊:用户先给机器人发过消息(比如
/start),机器人才能给他发。否则返回 403。 - 群聊:群的
chat_id是负数(-100加群号)。默认隐私模式下,机器人只收到/命令、@它 和回复它的消息;在 @wujiMother 发/setprivacy可以关掉。/setjoingroups可以禁止别人把它拉进群。 - 频道:机器人必须是频道管理员才能在频道发消息,频道主人在频道资料页用机器人的 @用户名 添加。频道的
chat.type是"channel",chat_id同样是负数。其他管理员发的消息以 Update 里的channel_post推送,只有管理员机器人能收到。 - 频率:每个机器人每秒最多 30 条;同一个人每秒 1 条;同一个群每分钟 20 条;频道里放宽到每 10 秒 20 条。超出返回 429,
parameters.retry_after为需要等待的秒数。 - 机器人发的消息同样经过内容审核。
5. 支持的方法
| 方法 | 说明 |
|---|---|
getMe | 机器人自己的信息 |
sendMessage | chat_id、text(最长 4096 字)、parse_mode(HTML / MarkdownV2 / Markdown)或 entities、reply_markup(按钮)、reply_to_message_id 或 reply_parameters、disable_notification |
sendPhoto / sendVideo / sendDocument | 文件可以用表单上传(图片 ≤10MB,其他 ≤50MB)、给一个网址(图片 ≤5MB,其他 ≤20MB),或用收到过的 file_id。支持 caption(≤1024 字,可带格式)和按钮,说明文字在 App 里紧跟在图片下面显示。sendAudio / sendVoice / sendAnimation 按文件发送 |
editMessageText / editMessageCaption / editMessageReplyMarkup | 修改机器人自己 48 小时内发的消息(文字、说明、按钮);不带 reply_markup 时按钮会被去掉 |
answerCallbackQuery | 回应按钮点击:text(≤200 字,App 里提示)、show_alert(弹窗)、url(打开网页)。请在 8 秒内回应 |
sendChatAction | 让 App 显示「正在输入…」「正在发送图片…」等,约 5 秒 |
getFile | 拿到文件的下载路径 |
deleteMessage | 撤回机器人自己 48 小时内发的消息 |
getUpdates | offset、limit(≤100)、timeout(≤50) |
setWebhook / deleteWebhook / getWebhookInfo | url、secret_token、drop_pending_updates、allowed_updates |
setMyCommands / getMyCommands / deleteMyCommands | 命令列表(小写字母、数字、下划线,最多 100 条) |
setMyName / getMyName | 显示名 |
setMyDescription / getMyDescription | 介绍(空对话里显示) |
setMyShortDescription / getMyShortDescription | 简介(资料页显示) |
getChat / getChatMember | 对话信息、群成员身份 |
leaveChat | 让机器人自己退出群或频道(群主不能退) |
logOut / close | 直接返回 true |
按钮只支持消息下方的按钮(inline_keyboard,回调按钮和链接按钮);输入框键盘(ReplyKeyboardMarkup)、内联模式、游戏、支付不支持。没有列出的方法返回 404。
6. wujiMother 命令
/newbot | 创建机器人 |
/mybots | 我的机器人 |
/token | 重新生成令牌 |
/setname /setdescription /setabouttext | 名字、介绍、简介 |
/setcommands /deletecommands | 命令列表,每行「命令 - 说明」 |
/setjoingroups /setprivacy | 能否进群、群隐私模式 |
/deletebot | 删除机器人 |
/cancel | 取消当前操作 |