Bug detected - communication with my bot via GTalk account

Hi,

I wrote the little bot for translating English to Serbian and vice versa using the Smack API.

Bot’'s JID is recnik@jabber.org.

When users send a word to the bot in English, he answers with the Serbian translation. Users can change the translation direction using # as the command prefix. So, for example, #smer change the direction.

Users from any jabber server, that is not Google’‘s, don’'t have any problems.

But, sometimes happens that the bot doesn’‘t receive the message that is sent from the Google account. Listener doesn’'t react at all?!

So, users have to send couple of messages (sometimes 10 or 20) to the bot, and suddenly he wakes up. After couple of minutes - the same problem

You can see the code:

package jabber; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.*;
import recnik.RecnikEnSrNew;
import recnik.RecnikSrEnNew; public class Bot extends Thread {      private RecnikEnSrNew recnikEnSr;
     private RecnikSrEnNew recnikSrEn;
     private Map<String, Boolean> smerovi;
     private XMPPConnection con;
     private List<String> admini;
     private static final String vanja = "vpetreski@gmail.com";
     private static final String resource = "Bot Re?nik Vesna";
     private static final String status = "Vama na usluzi :)";
     
     public Bot(String user, String pass, String server) {
          try {
               recnikEnSr = new RecnikEnSrNew();
               recnikSrEn = new RecnikSrEnNew();
               smerovi = new HashMap<String, Boolean>();
               admini = new ArrayList<String>();
               dodajAdmine();
               // XMPPConnection.DEBUG_ENABLED = true;
               con = new SSLXMPPConnection(server);
               con.login(user, pass, resource);
               Roster roster = con.getRoster();
               roster.setSubscriptionMode(Roster.SUBSCRIPTION_ACCEPT_ALL);
               Presence presence = new Presence(Presence.Type.AVAILABLE);
               presence.setStatus(status);
               presence.setPriority(1);
               con.sendPacket(presence);
               PacketFilter filter = new PacketTypeFilter(Message.class);
               PacketListener osluskivac = new PacketListener() {
                  public void processPacket(Packet packet) {
                       Message m = (Message)packet;
                       String from = m.getFrom().substring(0, m.getFrom().indexOf(''/''));
                       String por = m.getBody();
                       if(por != null) {
                            if(por.charAt(0) == ''#'')
                                 obradiKomandu(from, por.substring(1));
                            else
                                 odgovori(from, por);
                       }
                  }
              };
              con.addPacketListener(osluskivac, filter);
          } catch (Exception e) { e.printStackTrace(); }
     }      private void obradiKomandu(String posiljalac, String komanda) {
          try {
               if(!smerovi.containsKey(posiljalac))
                    smerovi.put(posiljalac, true); // default smer en > sr
               if(komanda.equals("smer")) {
                    smerovi.put(posiljalac, !smerovi.get(posiljalac)); // Obrce smer!
                    con.createChat(posiljalac).sendMessage("Pode?en je smer: " + (smerovi.get(posiljalac) ? "en-sr!" : "sr-en!"));
               }
               else if(komanda.equals("korisnici") && jeAdmin(posiljalac)) {
                    Roster roster = con.getRoster();
                    StringBuilder s = new StringBuilder("Broj korisnika recnika je " + roster.getEntryCount() + " i to su:\n");
                    for (Iterator i = roster.getEntries(); i.hasNext(); ) {
                         s.append(i.next() + "\n");
                    }
                    con.createChat(posiljalac).sendMessage(s.toString());
               }
               else
                    con.createChat(posiljalac).sendMessage("Ta komanda ne postoji!");
          } catch (Exception e) { e.printStackTrace(); }
     }
     
     private void odgovori(String posiljalac, String poruka) {
          try {
               if(!smerovi.containsKey(posiljalac))
                    smerovi.put(posiljalac, true); // default smer en > sr
               con.createChat(posiljalac).sendMessage(smerovi.get(posiljalac) ? recnikEnSr.prevedi(poruka) : recnikSrEn.prevedi(poruka));
          } catch (Exception e) { e.printStackTrace(); }
     }
     
     private void dodajAdmine() {
          admini.add(vanja);
     }
     
     private boolean jeAdmin(String posiljalac) {
          return admini.contains(posiljalac);
     }
     
     public void run() {
          while(true) {
               Scanner scan = new Scanner(System.in);
               scan.next();
          }
     }
     
     public static void main(String[] args) {
          
          if(args.length != 3) {
               System.out.println("Usage: java -jar BotRecnik.jar user pass server");
               System.exit(1);
          }
          
          Bot b = new Bot(args[0], args[1], args[2]);
          b.start();
          
     } }

Thanx,

Vanja

Btw, why don’'t you have the ‘‘code’’ tag?

Vanja

Hi Vanja,

To answer your second question first, to have your code nicely formatted wrap it in and tags (without the spaces).

As for your google talk question, it sounds like maybe you’'re not creating the XMPPConnection properly. Take a look at this post for more details.

Hope that helps,

Ryan

ryang,

thank you 4 the reply. I am creating my connection properly, indeed. My bot is on the jabber.org server, not Google’'s! So I made the standard connection. The problem occurs when the other side is a Google account!

V

This might be a s2s issue and not related to your bot at all.

Hi Vanja,

To expand on what anlumo said, are you seeing any traffic in the debug window?

Thanks,

Ryan

Hm, I don’‘t see any messages in the debug window when the bot doesn’‘t answer. It seems that Google doesn’'t send anything.

But, I don’'t think this is s2s problem because 2 people (one from Google, one from jabber.org) can regularly chat.

V