API Version: 6.5

Chat Echo Bot

Downloads

Explained

This is an example of an echo bot over chat. It echoes all messages sent to the currently logged in user back to the rooms they were sent in. It demonstrates basic use of the chat subsystem including listening to rooms and posting messages in them.
/* Enable the chat subsystem */
IPCortex.PBX.enableChat(
    /* Provide a callback for new rooms */
    function (ourRoom) {
        /* Add an update listener to each new room */
        ourRoom.addListener('update', function (room) {
            /* Inspect new messages and echo ... */
            room.messages.forEach(function (message) {
                console.log(message.cN + ' has decreed: ' + message.msg);
                /* ... except my own messages */
                if (!message.own)
                    room.post('Apparently you decreed: ' + message.msg);
            });
        });
    }
);
IPCortex.PBX.enableChat() is a function taking up to two callbacks; the first of which we have provided here and the second one we have omitted as it deals with chat presence data that does not apply to this sample.
Our particular callback is called once for each room that the current logged in user is a part of and provides us with an object representing that room. We then add a listener for updates to that room with a callback that is called whenever we receive new messages. The Room object provided to our listener callback has an array of new messages as a property so we can use Array.forEach() to iterate over these.
In this example, we simply log each message to the console and post it back to the room.
To show this working, use keevio to chat to the logged in user and watch the messages being echoed back by the user.