无忌机器人接口

用任何能发 HTTP 请求的语言都能写无忌机器人:调接口发消息,收消息用长轮询或 Webhook。

1. 创建机器人

  1. 在无忌里搜索 @wujiMother,给它发 /newbot。
  2. 按提示输入名字和用户名(必须以 bot 结尾,例如 WeatherBot)。
  3. 它会回复一个令牌(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. 收消息

两种方式二选一:

想让用户一键把机器人拉进群:给出链接 /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. 规则

5. 支持的方法

方法说明
getMe机器人自己的信息
sendMessagechat_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 小时内发的消息
getUpdatesoffset、limit(≤100)、timeout(≤50)
setWebhook / deleteWebhook / getWebhookInfourl、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取消当前操作