Send XMPP messages via HTTP on prosody

We switched all our phones to Snom VoIP phones at work. They are managed by an Asterisk telephony switchboard server with the Gemeinschaft web interface.

One very cool thing about the Snom phones is that they are configurable in very many ways, and can fetch their configuration from the Asterisk server automatically. Once you find a setting that's useful for all, simply set it in the provisioning profile and let the phones reboot - and all have the new config setting.

One of the phone's features is calling an HTTP URL when something happens, e.g. start of an incoming or outgoing call, or connecting/disconnecting of the call. My self-assigned task now was to get chat notifications whenever a call comes in.

mod_post_msg

We use Prosody as XMPP (Jabber) server. The list of official plugins is long, and one of them is mod_post_msg . It allows you to send XMPP messages with a simple HTTP call.

Unfortunately, mod_post_msg only supported plain text messages - but I wanted to be able to link the names of the incoming caller to our internal address book, and also provide one-click redial/callback functionality via tel: URLs.

Prosody is written in Lua, a scripting language often used in games. With the help of the prosody MUC channel it was easy to extend the plugin with support for HTML messages. My patch got merged and can be used by everyone now.

Sending messages via PHP

When sending a HTML XMPP message, you should always provide a plain text message as fallback for clients that do not support, or whose users do not want to display HTML content. Apart from that, sending the message via a POST request is not hard:

<?php
$xmppUser = 'asterisk@example.org';
$xmppPass = 'FIXME';
 
$xmppTarget = 'user@example.org';
$callerName = 'FIXME';
$adrLink = 'https://FIXME';
$telLink = 'tel:FIXME';
 
$textMsg = 'Incoming call from ' . $callerName;
$htmlMsg = 'Incoming call from '
    . '<a href="' . htmlspecialchars($adrLink) . '">'
    . htmlspecialchars($callerName)
    . '</a>'
    . ' <a href="' . htmlspecialchars($telLink) . '">'
    . '&#9742;</a>';
 
$postMsg = 'to=' . urlencode($xmppTarget)
        . '&type=chat'
        . '&body=' . urlencode($textMsg)
        . '&html=' . urlencode(
            '<body xmlns="http://www.w3.org/1999/xhtml">'
            . $htmlMsg
            . '</body>body>'
        );
$ctx = stream_context_create(
    array(
        'http' => array(
            'method' => 'POST',
            'header' => array(
                'Content-type: application/x-www-form-urlencoded',
                'Authorization: Basic ' . base64_encode($xmppUser . ':' . $xmppPass),
            ),
            'content' => $postMsg
        )
    )
);
file_get_contents('http://xmpp.example.org:5280/msg/', false, $ctx);
?>

The result will look similar to this:

Chat message from the asterisk server

Written by Christian Weiske.

Comments? Please send an e-mail.