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.
IPCortex.PBX.enableChat(
function (ourRoom) {
ourRoom.addListener('update', function (room) {
room.messages.forEach(function (message) {
console.log(message.cN + ' has decreed: ' + message.msg);
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.