Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve sender name or its JID in multi user chat message with Smack?

i am working on chat app based on XMPP in Android.

i have done one to one chat functionality, but having some problem in multi user chat. i have successfully created new chat room, multiple users can join that room. i have also written code for addPacketListener for Group chat with PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);

i also receiving messages in that listener when user sents message in group, but i am unable to distinguish which user has sent message.

like one to one chat message packet has function message.getFrom() to retrieve senders JID.

in case of multi user chat, same function returns the group/room JID as sender.

i have also tried to set property of Message while sending message.setFrom(senderJID); or message.setFrom([email protected]/Groupname);

still i am unable to get senderJID or its nickname.

so my question is: how to get sender user JID or its nickname? from message(packet) in messageListener

Code for sending msg in group is:-

String to = strGroupJID;
String text = etChatOnTextBox.getText().toString();
if(!text.equals(""))
{
        Message msg = new Message(to, Message.Type.groupchat);
        msg.setBody(text);
        String name1 = xmppConnection.getUser();
        name1 = name1.substring(0, name1.lastIndexOf("@"));
        name1 = name1 + "@conference.192.168.56.1";
       // name1 = name1 + "@conference.192.168.56.1/" + strGroupName ;
        msg.setFrom(name1);
        muc.sendMessage(msg);
 }

Code for Receive Message is:-

PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
        connection.addPacketListener(new PacketListener() {
            @Override
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message
                            .getFrom());

                    Log.i("ChatOn", "Text Recieved " + message.getBody()
                            + " from " + fromName );
           }
        }
   });

any help or suggestion is appreciated thank you

like image 514
Yograj Shinde Avatar asked Aug 14 '14 11:08

Yograj Shinde


People also ask

How do I find messages in slack?

Slack apps tend to encounter messages most often when receiving them in Events API payloads or in request payloads when users invoke slash commands or custom actions. However, there are some occasions where it might be necessary for an app to actively seek out a message and find it in the wild.

How do I find a slack conversation ID using OAuth?

On the OAuth & Permissions settings page you're brought back to, you should now see an OAuth access token available. Grab this token and store it for later, as we'll use that token to make some Web API calls. In order to find a valid Slack conversation ID, we'll use the conversations.list API method.

What is the structure of message objects retrieved via slack APIs?

The structure of message objects retrieved via Slack APIs is very similar to the general structure of a message payload you want to publish. There are a few additional fields that describe the author (such as user or bot_id ), but there's also an additional ts field.

How to identify parent messages in a thread?

Identify parent messages by comparing the thread_ts and ts values. If they are equal, the message is a parent message. Threaded replies are also identified by comparing the thread_ts and ts values.


1 Answers

This piece of your code will identify the chat room:

String fromName = StringUtils.parseBareAddress(message
        .getFrom());

You can use this code to identify the nick of the chat room user:

String nick = StringUtils.parseResource(message
        .getFrom());

This is because JIDs of multi user chat messages look like roomname@server/nickname, with the nickname of the user being the resource of the JID.

like image 97
legoscia Avatar answered Oct 03 '22 06:10

legoscia