通过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 有效负载以实现更复杂的消息。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 通过 HTTP 发送 Jabber/XMPP 消息 (gultsch.de) 13 分,由 inputmice 1 小时前发布 | 隐藏 | 过去 | 收藏 | 2 条评论 帮助 giancarlostoro 33 分钟前 [–] > 我们正在安装 Prosody IM,社区模块和 certbot(获取证书)。我的理解是 certbot 是 letsencrypt 的一部分,最终会导致你无法使用 XMPP 证书,我理解错了吗?https://news.ycombinator.com/item?id=46950780 回复 inputmice 20 分钟前 | 父评论 [–] 博文解释了 XMPP 社区为了跟上 Lets encrypt 的变化需要做出的调整。 简而言之:人们只需要升级他们的服务器。 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

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