通过HTTP发送Jabber/XMPP消息
Sending Jabber/XMPP Messages via HTTP

原始链接: https://gultsch.de/posts/xmpp-via-http/

本教程指导您使用 Prosody IM 设置 REST API,以发送 XMPP 消息——非常适合与监控系统或脚本集成。与直接的 XMPP 工具不同,它提供了一个用于发送消息的 HTTP 接口。 该设置专为 Debian 13 设计(但可适应其他 Linux 发行版),需要一个具有可控 A 记录的域名。它涉及安装 Prosody、必要的模块(包括 `mod_post_msg`)以及 Certbot 用于 SSL 证书。 最小的 Prosody 配置会禁用标准 XMPP 功能,仅专注于 REST API 功能。 在通过 Certbot 获取 Let’s Encrypt 证书后,您可以使用 `prosodyctl adduser` 创建一个用户(例如 `[email protected]`)。然后使用 `curl` 通过 API 端点、用户名/密码身份验证和消息内容发送消息。例如:`curl "https://ntfy.stdmsg.tech:5281/msg/[email protected]" -u [email protected]:secret -H "Content-Type: text/plain" -d "Your flat white is done"`。`mod_post_msg` 还支持 JSON 有效负载以实现更复杂的消息。

一个黑客新闻的讨论围绕着一篇博客文章,详细介绍了如何通过HTTP发送Jabber/XMPP消息(gultsch.de)。用户表达了对早期XMPP系统灵活性的怀旧之情,并将其与如今封闭的消息平台(如WhatsApp和Telegram)形成对比。 对话涉及这种HTTP方法的潜在用途,特别是将其与大型语言模型(LLM)集成,以创建自动化机器人——让人联想到充满活力的IRC机器人场景。 许多用户强调使用这种方法轻松地向XMPP帐户发送自动化通知,并提到了Apprise服务作为相关工具。 有人提出了Let's Encrypt证书与XMPP兼容性的问题,但澄清需要服务器升级,而非完全不兼容。 总而言之,该讨论反映了对更开放和自托管的消息解决方案的需求。
相关文章

原文

The goal of this tutorial is to set up a simple REST API that allows you to send XMPP messages to an existing XMPP account. This can be easily integrated into monitoring solutions or other scripts that send out status information.

While there are command-line tools like go-sendxmpp that send messages by connecting to an XMPP server directly, this guide is specifically about providing an HTTP interface.

curl "https://ntfy.stdmsg.tech:5281/msg/[email protected]" \
  -u [email protected]:secret \
  -H "Content-Type: text/plain" \
  -d "Your flat white is done"

Hint: If you don’t have an XMPP account to receive these messages, you can create one on a public server from within Conversations (F-Droid, Google Play) or another XMPP client of your choosing.

Requirements

This guide assumes you have a relatively clean install of Debian 13. However, using other Linux distributions or integrating this into an install with existing services will probably work as well. The only other requirement is that you have a domain. Any subdomain over which you control the A record will do. In this tutorial, we will use the domain ntfy.stdmsg.tech (be sure to replace it with your own).

Setup

Installation

We are installing Prosody IM, the community modules and certbot (to get certificates).

apt install prosody prosody-modules lua-unbound liblua5.4-dev certbot

The Prosody community module we need doesn’t come with the prosody-modules Debian package. We are going to use the Prosody plugin installer instead.

prosodyctl install --server=https://modules.prosody.im/rocks/ mod_post_msg

Configuration

We will be creating a mininal configuration file for Prosody, usually a regular XMPP server, that has everything disabled, except for the stuff that is required to provide the REST API.

Replace /etc/prosody/prosody.cfg.lua with this configuration:

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"

Getting certificates

We are using certbot to retrieve Let’s Encrypt certificates. If you already have a certbot setup make sure to change the --standalone parameter to something more suitable.

certbot certonly --standalone  -d ntfy.stdmsg.tech
prosodyctl --root cert import /etc/letsencrypt/live/

This assumes you have the A record of your domain pointed to your server.

Check and first start

You can use prosodyctl check to check over the configuration file. Afterwards restart Prosody with the new configuration file.

systemctl restart prosody

You can use ss -ltnp to check if Prosody is running on port 5269 (XMPP S2S) and port 5281 (The HTTPS port used for the REST API).

Send your first message

Creating a user

prosodyctl adduser [email protected]

Enter a password of your choice when prompted. We pick “secret” in this example.

Using the REST API

curl "https://ntfy.stdmsg.tech:5281/msg/[email protected]" \
  -u [email protected]:secret \
  -H "Content-Type: text/plain" \
  -d "Your flat white is done"

The [email protected] (part of the path) is the recipient. The -u part is the user you’ve just created and its password. It’s the sender of the XMPP message.

You can create multiple users depending on how much seperation of concern you want.

A notification on Android from the app Conversations that shows the message ‘Your flat white is done’ coming from the user ‘coffee’

A notification on Android from the app Conversations that shows the message ‘Your flat white is done’ coming from the user ‘coffee’

Additional reading

The module we are using is called mod_post_msg. Its documentation describes how it can take JSON payloads, if this is more appropriate for your use case.

You can also configure the built-in HTTP server and/or put it behind a reverse proxy.

For a more permanent setup, you might want to look into setting up a certbot hook that reimports fresh certificates into Prosody.

联系我们 contact @ memedata.com