通过 HTTP 发送 Jabber/XMPP 消息
Source: Hacker News
本教程的目标是搭建一个简易的 REST API,以便向已有的 XMPP 账户发送 XMPP 消息。它可以轻松集成到监控系统或其他发送状态信息的脚本中。
虽然有像 go‑sendxmpp 这样的命令行工具可以直接连接到 XMPP 服务器发送消息,但本指南专注于提供 HTTP 接口。
curl "https://ntfy.stdmsg.tech:5281/msg/user@example.com" \
-u coffee@ntfy.stdmsg.tech:secret \
-H "Content-Type: text/plain" \
-d "Your flat white is done"
提示: 如果你没有用于接收这些消息的 XMPP 账户,可以在 公共服务器 上创建一个账户,使用 Conversations(F‑Droid、Google Play)或其他你喜欢的 XMPP 客户端。
Requirements
本指南假设已进行相对干净的 Debian 13 安装。其他 Linux 发行版或已有的安装也可能同样适用。您还需要一个域名(您控制的任意子域)。示例中使用的域名是 ntfy.stdmsg.tech——请将其替换为您自己的域名。
设置
安装
我们安装 Prosody IM、社区模块以及 Certbot:
apt install prosody prosody-modules lua-unbound liblua5.4-dev certbot
我们需要的 Prosody 社区模块并未包含在 prosody-modules Debian 包中,因此使用 Prosody 插件安装器:
prosodyctl install --server=https://modules.prosody.im/rocks/ mod_post_msg
配置
创建一个最小化的 Prosody 配置,只保留 REST API 所需的功能。将 /etc/prosody/prosody.cfg.lua 替换为如下内容:
pidfile = "/var/run/prosody/prosody.pid"
modules_enabled = {
"tls";
"dialback";
"http";
"admin_shell";
"post_msg";
}
modules_disabled = {
"c2s";
"offline";
}
log = {
info = "prosody.log";
error = "prosody.err";
}
certificates = "certs"
VirtualHost "ntfy.stdmsg.tech"
获取证书
使用 Certbot 获取 Let’s Encrypt 证书。如果你已经有 Certbot 环境,请相应地调整 --standalone 参数。
certbot certonly --standalone -d ntfy.stdmsg.tech
prosodyctl --root cert import /etc/letsencrypt/live/
确保你的域名的 A 记录指向你的服务器。
检查并首次启动
验证配置并重启 Prosody:
prosodyctl check
systemctl restart prosody
你可以使用以下命令确认 Prosody 正在监听端口 5269(XMPP S2S)和端口 5281(用于 REST API 的 HTTPS 端口):
ss -ltnp
发送你的第一条消息
创建用户
prosodyctl adduser coffee@ntfy.stdmsg.tech
在提示时输入你选择的密码(示例使用 “secret”)。
使用 REST API
curl "https://ntfy.stdmsg.tech:5281/msg/user@example.com" \
-u coffee@ntfy.stdmsg.tech:secret \
-H "Content-Type: text/plain" \
-d "Your flat white is done"
user@example.com(路径部分)是接收者。-u 参数中的凭证是你刚创建的发送者。
如果需要职责分离,你可以创建多个用户。

进一步阅读
使用的模块是 mod_post_msg。其文档描述了它如何接受 JSON 负载,这在某些用例中可能更合适。
您也可以配置内置的 HTTP 服务器或将其放在反向代理后面。
对于更永久的设置,考虑使用Certbot 钩子将新证书重新导入 Prosody。