Wednesday, October 15, 2014

Send a message to Google chat via Java Application


Send a message to Google chat via Java Application.


Scope : Through the Java application send the message to Google chat.


Objective : Once execute the java application, the predefined message send as a Google chat message.

Requirements:
  • Eclipse JAVA EE IDE
  • JDK 1.6
  • Smack java library (download)
  • Need a Google account. If does not have please create an google talk account from here.

Step 1: Create a java project in eclipse and named as "GoogleChat".
             File à New à Java Project. 

            Add the downloaded smack.jar and smackx.jar to the project build path.

Step 2: Create a "SendChatMsg" java class under "com.prem.google.chat" package.

             /GoogleChat/src/com/prem/google/chat/SendChatMsg.java

package com.prem.google.chat;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

public class SendChatMsg {

 private static String username = "xxxxxxx@gmail.com";
 private static String password = "xxxxxxxxx";
 ConnectionConfiguration connConfig;
 XMPPConnection connection;

 public SendChatMsg() {
  createConnection();
 }

 private void createConnection() {
  try {
   connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
   connection = new XMPPConnection(connConfig);
   connection.connect();
   connection.login(username, password);
  } catch (XMPPException e) {
   e.printStackTrace();
  }
 }

 public void sendMessage(String to, String message) {
  Message msg = new Message(to, Message.Type.chat);
  msg.setBody(message);
  connection.sendPacket(msg);
 }
 
 public static void main(String[] args) {
  SendChatMsg chatMsg = new SendChatMsg();
  String sendMailAddress = args[0];
  String message = "";
  int counter = 0;
  for (String string : args) {
   if(counter != 0){
    message = message + " " +  string;
   }
   counter++;
  }
  chatMsg.main(sendMailAddress, message);
  System.out.println("Message send to successfully.");
  System.out.println("Receivers : " + sendMailAddress);
 }

 public void disconnect() {
  connection.disconnect();
 }

 public void main(String sendMailAddress, String msg){
  SendChatMsg gtalkChat = new SendChatMsg();

  String[] address = sendMailAddress.split(",");
  
  for (String mailAddress : address) {
   gtalkChat.sendMessage(mailAddress, msg);
  }
  gtalkChat.disconnect();
 }

}

Specify the correct gmail id and password to the username & password properties respectively.
The chat message will send through the above mentioned gmail account.


That's all..

Step 2: Now the time to test the application.
Create the "Test.java" class under the same "com.prem.google.chat" package.

Sample test class as below.


package com.prem.google.chat;

public class Test {
 public static void main(String[] args) {
  SendChatMsg chatMsg = new SendChatMsg();
  chatMsg.main("receiver1@gmail.com,receiver2@gmail.com,receiver3@gmail.com", "Test Chat Message .... ");
  System.out.println("Done!!");
 }
}

Specify the correct receiver gmail address (with comma separated) as a first parameter and the message need to send.

Right click on the "Test" java file and go to Run As - Run Java application.

The receivers got an chat message from Google talk API.
E.E

1. Make it sure the port 5222 is not block from the fire wall.


No comments:

Post a Comment