How to get chat history using Java Smack library from openfire server?

After installing the Open Archive plugin on the Openfire server, I see a chat dialog between two users from the openfire admin panel, which is quite simple, and it is also based on a web interface. Now I want to get this conversation or chat history from the chat client application (written in java), where I used the Smack library. I did not find a useful resource for this. Any advice would be helpful.

+8
source share
5 answers
Finally, I got an answer. Message archiving features are not currently implemented in the Smack library.

https://community.igniterealtime.org/message/249993#249993

+1
source

Smack has just implemented the MAM function [XEP 0313], but not yet released, I hope to get it in the next release, if you want to use this function, create the smack library from the source or you can use custom IQ to receive archive messages from the server.

+6
source

This may be a late answer, but now, since the SMACK API supports XEP-0136 and XEP-0313, so the code below can help people landing on this page.

public MamManager.MamQueryResult getArchivedMessages(String jid, int maxResults) { MamManager mamManager = MamManager.getInstanceFor(connection); try { DataForm form = new DataForm(DataForm.Type.submit); FormField field = new FormField(FormField.FORM_TYPE); field.setType(FormField.Type.hidden); field.addValue(MamElements.NAMESPACE); form.addField(field); FormField formField = new FormField("with"); formField.addValue(jid); form.addField(formField); // "" empty string for before RSMSet rsmSet = new RSMSet(maxResults, "", RSMSet.PageDirection.before); MamManager.MamQueryResult mamQueryResult = mamManager.page(form, rsmSet); return mamQueryResult; } catch (Exception e) { e.printStackTrace(); } return null; } 
+4
source

The solution you are looking for falls under the XMPP specification XEP-0136 Message Archiving, but Smack has not yet implemented these features. but you can get the message history from the server using the "custom-stanza" functions provided by the SMACK API. The following links describe how to send a custom stanza. " How to get chat history using Java Smack library from openfire server? ".

+2
source

Maybe I was late to answer this question, but maybe it will be useful for others.

 public List<ChatMessage> getChatHistoryWithJID(String jid, int maxResults) { List<ChatMessage> chatMessageList = new ArrayList<>(); MamManager.MamQueryResult mamQueryResult = getArchivedMessages(jid, maxResults); String userSendTo = XmppUtils.parseNameFromJID(jid); try { if (mamQueryResult != null && userSendTo != null) { for (Forwarded forwarded : mamQueryResult.forwardedMessages) { if (forwarded.getForwardedStanza() instanceof Message) { Message msg = (Message) forwarded.getForwardedStanza(); Log.d(TAG, "onCreate: " + msg.toString()); Log.d(TAG, "processStanza: " + msg.getFrom() + " Say:" + msg.getBody() + " String length:" + (msg.getBody() != null ? msg.getBody().length() : "")); ChatMessage chatMessage; if (XmppUtils.parseNameFromJID(msg.getFrom().toString()).equalsIgnoreCase(userSendTo)) { chatMessage = new ChatMessage(msg.getBody(), forwarded.getDelayInformation().getStamp().getTime(), ChatMessage.Type.RECEIVED); } else { chatMessage = new ChatMessage(msg.getBody(), forwarded.getDelayInformation().getStamp().getTime(), ChatMessage.Type.SENT); } chatMessageList.add(chatMessage); } } } else { return chatMessageList; } return chatMessageList; } catch (Exception e) { e.printStackTrace(); } return chatMessageList; } 

Now the request will become like this:

 <iq id='ri7F7-270' type='set'> <query xmlns='urn:xmpp:mam:1' queryid='afd9c922-21cb-437e-b5c4-3a5bf9994e40'> <x xmlns='jabber:x:data' type='submit'> <field var='FORM_TYPE' type='hidden'> <value>urn:xmpp:mam:1</value> </field> <field var='with'> <value> vishal@jabberid </value> </field> </x> <set xmlns='http://jabber.org/protocol/rsm'> <before> </before> <max>100</max> </set> </query> 

And the answer will be like this:

 <message to=" vishal@jabberid "> <result xmlns="urn:xmpp:mam:1" queryid="afd9c922-21cb-437e-b5c4-3a5bf9994e40" id="992"> <forwarded xmlns="urn:xmpp:forward:0"> <delay xmlns="urn:xmpp:delay" stamp="2019-04-05T06:38:40.612Z"/> <message xmlns="jabber:client" to=" vishal@jabberid " id="h58k4-104" type="chat" from=" vishal@jabberid "> <body>Hi</body> </message> </forwarded> </result> </message> 

And you can also read this link https://xmpp.org/extensions/xep-0313.html

0
source

Source: https://habr.com/ru/post/989462/


All Articles