public class Main { public static void main(String[] args) { try { String botNick = "nickname"; String botPassword = "password"; String botDomain = "jabber.org"; String botServer = "jabber.org"; int botPort = 5222; JabberBot bot = new JabberBot(botNick, botPassword, botDomain, botServer, botPort); Thread botThread = new Thread(bot); botThread.start(); } catch(Exception e) { System.out.printLn(e.getMessage()); } } } /** * , . * Runnable, * , .<hr> * * smack.jar smackx.jar:<br> * org.jivesoftware.smack<hr> * * @author esin * */ public class JabberBot implements Runnable { private String nick; private String password; private String domain; private String server; private int port; private ConnectionConfiguration connConfig; private XMPPConnection connection; /** * , - * @param nick - * @param password - * @param domain - * @param server - * @param port - */ public JabberBot (String nick, String password, String domain, String server, int port) { this.nick = nick; this.password = password; this.domain = domain; this.server = server; this.port = port; } @Override public void run() { connConfig = new ConnectionConfiguration(server, port, domain); connection = new XMPPConnection(connConfig); try { int priority = 10; SASLAuthentication.supportSASLMechanism("PLAIN", 0); connection.connect(); connection.login(nick, password); Presence presence = new Presence(Presence.Type.available); presence.setStatus(" "); connection.sendPacket(presence); presence.setPriority(priority); PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class)); PacketListener myListener = new PacketListener() { public void processPacket(Packet packet) { if (packet instanceof Message) { Message message = (Message) packet; // processMessage(message); } } }; connection.addPacketListener(myListener, filter); // , while(connection.isConnected()) { Thread.sleep(60000); } } catch (Exception e) { System.out.printLn(e.getMessage()); } } /** * <hr> * @param message */ private void processMessage(Message message) { String messageBody = message.getBody(); String JID = message.getFrom(); // . :) // - sendMessage(JID, messageBody); } /** * <hr> * @param to JID , <br> * @param message */ private void sendMessage(String to, String message) { if(!message.equals("")) { ChatManager chatmanager = connection.getChatManager(); Chat newChat = chatmanager.createChat(to, null); try { newChat.sendMessage(message); } catch (XMPPException e) { System.out.printLn(e.getMessage()); } } } }
public class Main { public static void main(String[] args) { try { String botNick = "nickname"; String botPassword = "password"; String botDomain = "gmail.com"; // String botServer = "talk.google.com"; int botPort = 5222; GoogleTalkBot bot = new GoogleTalkBot(botNick, botPassword, botDomain, botServer, botPort); Thread botThread = new Thread(bot); botThread.start(); } catch(Exception e) { System.out.printLn(e.getMessage()); } } } /** * , . * Runnable, * , .<hr> * * smack.jar smackx.jar:<br> * org.jivesoftware.smack<hr> * * @author esin * */ public class GoogleTalkBot implements Runnable { private String nick; private String password; private String domain; private String server; private int port; private ConnectionConfiguration connConfig; private XMPPConnection connection; /** * , * @param nick - * @param password - * @param domain - * @param server - * @param port - */ public GoogleTalkBot (String nick, String password, String domain, String server, int port) { this.nick = nick; this.password = password; this.domain = domain; this.server = server; this.port = port; } @Override public void run() { connConfig = new ConnectionConfiguration(server, port, domain); connection = new XMPPConnection(connConfig); try { int priority = 10; SASLAuthentication.supportSASLMechanism("PLAIN", 0); connection.connect(); connection.login(nick + "@" + domain, password); // nickname@gmail.com Presence presence = new Presence(Presence.Type.available); presence.setStatus(" "); connection.sendPacket(presence); presence.setPriority(priority); PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class)); PacketListener myListener = new PacketListener() { public void processPacket(Packet packet) { if (packet instanceof Message) { Message message = (Message) packet; if(message.getType() == Type.chat) // , { // Process message processMessage(message); } } } }; // Register the listener. connection.addPacketListener(myListener, filter); while(connection.isConnected()) { Thread.sleep(60000); } } catch (Exception e) { System.out.printLn(e.getMessage()); } } /** * <hr> * @param message - */ private void processMessage(Message message) { String messageBody = message.getBody(); String JID = message.getFrom(); sendMessage(JID, messageBody); } /** * <hr> * @param to - JID , <br> * @param message - */ private void sendMessage(String to, String message) { if(!message.equals("")) { ChatManager chatmanager = connection.getChatManager(); Chat newChat = chatmanager.createChat(to, null); try { newChat.sendMessage(message); } catch (XMPPException e) { System.out.printLn(e.getMessage()); } } } }
Source: https://habr.com/ru/post/127535/
All Articles