Categories
Commercials Computing Java WebSphere

Building an IM Bot with the Smack API. Avoid setting Roster Permissions

So I built what is an instant messaging bot using the Smack API and the Openfire XMPP server. Doing it is pretty simple of you read the documentation and Smack’s developer notes. The bot relies on a queue-like object that Smack gives you called a PacketCollector. All you need to do is log in to the server, and let the packet collector wait for incoming messages. Clearly, you need to respond to the messages and use thread tools to do the waiting and all, but in general, the effort is relatively straightforward.

One thing I noticed was that if you use a Smack-based IM client to send messages to Openfire, and on to the bot, the bot worked fine and received the messages. When I tried to send a message to the bot from a JSJaC-based client (JavaScript XMPP library), the bot would not receive any messages. Even admin messages were not received by the bot, while the JSJaC client received them fine.

The culprit appeared to be the fact that I set the roster to accept all subscription requests. The roster, in plain English, is the buddy list. Subscriptions mean that as my buddy, you can see my status. My code looked like this:

// set roster subscription mode
Roster roster = conn1.getRoster(); 
roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
 
// wait for messages, accepting all of them
PacketFilter pf = new PacketTypeFilter(Message.class);
PacketCollector pc = conn1.createPacketCollector(pf);

I found that if I used JSJaC I had to be logged-in to the server as a buddy of the bot for the bot to get the messages I sent it. On the other hand, when I removed these lines from my code, any message was received — which is what you want from a bot. So, if you do NOT set your roster subscription mode, you will be open to get all messages…

Weird.

Share

2 replies on “Building an IM Bot with the Smack API. Avoid setting Roster Permissions”

Hi,
I am using smack api and xmpp ,i am facing issue when want to add new contact in my list.
your help is appreciable.
plz send your help on my mail.
 
Regards,
Monika

how to set the subcription to both using codes…when i add a particular user to the roster using createEntry() method..plz help

Leave a Reply

Your email address will not be published.

 

Share