Notify chatroom users from a custom prosody module

#1

Hello all,

Does anyone knows how to send notifications (normal messages and warnings) to all chatroom users from a custom prosody module and how to display these notification as warning box on the UI :
image

Here’s my code :

module:hook("muc-occupant-joined", function(event)
-- send notification with the following message "This is platform is still experimental. Please contact us for any assistance"
end, 0)

Thanks

#2

I see that there is a method to broadcast a message to a room: room:broadcast_message(msg); and there is a live_rooms() method in mod_muc so you should be able to do that.
Currently there is nothing hooked to show notifications on any message, but it should be easy to be added.

#3

Hello @damencho,
Thanks for the answer.
I used this example but it doesn’t work

module:hook("muc-occupant-joined", function(event)
event.room:broadcast_message("This is platform is still experimental. Please contact us for any assistance")
end, 0)

I have the following error in prosody logs :

Apr 30 13:25:59 mod_bosh	error	Traceback[bosh]: /usr/lib/prosody/modules/muc/history.lib.lua:140: attempt to call method 'get_child' (a nil value)
stack traceback:
	/usr/lib/prosody/modules/muc/history.lib.lua:140: in function '?'
	/usr/lib/prosody/util/events.lua:78: in function </usr/lib/prosody/util/events.lua:74>
	(tail call): ?
	(tail call): ?
	/usr/lib/prosody/modules/muc/history.lib.lua:161: in function '?'
	/usr/lib/prosody/util/events.lua:78: in function </usr/lib/prosody/util/events.lua:74>
	(tail call): ?
	(tail call): ?
	/usr/lib/prosody/modules/muc/muc.lib.lua:167: in function 'broadcast_message'
	/opt/prosody-webconf-modules/mod_jitsi_stats.lua:49: in function '?'
	/usr/lib/prosody/util/events.lua:78: in function </usr/lib/prosody/util/events.lua:74>
	(tail call): ?
	(tail call): ?
	/usr/lib/prosody/modules/muc/muc.lib.lua:431: in function </usr/lib/prosody/modules/muc/muc.lib.lua:388>
	(tail call): ?
	/usr/lib/prosody/util/events.lua:78: in function </usr/lib/prosody/util/events.lua:74>
	(tail call): ?
	/usr/lib/prosody/core/stanza_router.lua:187: in function 'core_post_stanza'
	/usr/lib/prosody/core/stanza_router.lua:135: in function 'dispatch_stanza'
	/usr/lib/prosody/modules/mod_bosh.lua:322: in function 'func'
	/usr/lib/prosody/util/async.lua:90: in function </usr/lib/prosody/util/async.lua:88>
stack traceback:
	(tail call): ?
	/usr/lib/prosody/util/async.lua:163: in function 'run'
	/usr/lib/prosody/modules/mod_bosh.lua:454: in function 'cb_handlestanza'
	/usr/lib/prosody/util/xmppstream.lua:184: in function </usr/lib/prosody/util/xmppstream.lua:164>
	[C]: in function 'parse'
	/usr/lib/prosody/util/xmppstream.lua:271: in function 'feed'
	/usr/lib/prosody/modules/mod_bosh.lua:150: in function '?'
	/usr/lib/prosody/util/events.lua:78: in function </usr/lib/prosody/util/events.lua:74>
	(tail call): ?
	/usr/lib/prosody/net/http/server.lua:239: in function </usr/lib/prosody/net/http/server.lua:180>
	...
	/usr/lib/prosody/net/http/server.lua:112: in function 'process_next'
	/usr/lib/prosody/net/http/server.lua:128: in function 'success_cb'
	/usr/lib/prosody/net/http/parser.lua:177: in function 'feed'
	/usr/lib/prosody/net/http/server.lua:159: in function </usr/lib/prosody/net/http/server.lua:158>
	(tail call): ?
	/usr/lib/prosody/net/server_select.lua:920: in function </usr/lib/prosody/net/server_select.lua:895>
	[C]: in function 'xpcall'
	/usr/bin/prosody:396: in function 'loop'
	/usr/bin/prosody:427: in main chunk
	[C]: ?

Any idea how to solve this ?

#4

In prosody code I see:

	local msg = create_subject_message(from, subject);
	room:broadcast_message(msg);

local function create_subject_message(from, subject)
	return st.message({from = from; type = "groupchat"})
		:tag("subject"):text(subject or ""):up();
end

You need to create a valid stanza and send it.

#5

Great. it worked.

Do you know how to handle this event on the client side ? I tried this but it doesn’t work

APP.conference.addConferenceListener(
  JitsiMeetJS.events.conference.MESSAGE_RECEIVED,
  function(id, text, ts){alert(text)});

On prosody side I have :

event.room:broadcast_message(
    st.message({
    from = event.origin.host;
    type = "groupchat"}):tag("subject"):text("This is platform is still experimental. Please contact us for any assistance"):up());

On the client side I get this :

#6

Try adding a breakpoint in https://github.com/jitsi/lib-jitsi-meet/blob/master/modules/xmpp/ChatRoom.js#L839 and see is it hit with your message and where is your message swallowed.
The APP.conference.addConferenceListener( listener you has added works for me catching the rest of the messages.