Skip to main content

GoogleTalk Chatting through JAVA


The following code helps you to do chat by running a java program. We can maintain groups, and can send messages for all the members in the group. There are 3 classes:


1.SendChatMsg.java
2.Globals.java
3.ConfigMessage.java

SendChatMsg.java is the major classes which do the process of sending chat message. You can configure your gmail userName and password here.

Code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.rakesh.chat;

import java.util.List;

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

/**
 * @author Rakesh KR
 * @since  July 2k14
 */
public class SendChatMsg {

 private static String username = "2krrakesh2@gmail.com";
 private static String password = "***************";
 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();
  }
 }

 private void sendMessage(String to, String message) {
  
  Message msg = new Message(to, Message.Type.chat);
  msg.setBody(message);
  connection.sendPacket(msg);
  System.out.println("Sending Msg to : "+to);
  
 }

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

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

  for (String mailAddress : sendMailAddress) {
   gtalkChat.sendMessage(mailAddress, msg);
  }
  
  gtalkChat.disconnect();
  
 }

}

Globals.java , here you can configure the group details with groupName as the key of the map.
Code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.rakesh.chat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Rakesh KR
 * @since  July 2k14
 */
public class Globals {
 
 public static Map<String, List<String>> groupMap = groupMap();
 
 public static List<String> memberList = null;
 
 
 private static  Map<String, List<String>> groupMap(){
  
  groupMap = new HashMap<String, List<String>>();
  groupMap.put("Food",foodMemberList());
  groupMap.put("MrngZ",mrngzMemberList());
  groupMap.put("Test",testList());
  return groupMap;
  
 }
 
 private static List<String> foodMemberList(){
  
  memberList = new ArrayList<String>();
  
  memberList.add("speethambaran8@gmail.com");
  memberList.add("ajith.narayanan99@gmail.com");
  memberList.add("remeezkr@gmail.com");
  memberList.add("subin167@gmail.com");
  memberList.add("postforhari@gmail.com");
  memberList.add("binoyarun@gmail.com");
  memberList.add("riyass.ayath@gmail.com");
  
  return memberList;
  
 }
 
 private static List<String> mrngzMemberList(){
  
  memberList = new ArrayList<String>();
  
  memberList.add("speethambaran8@gmail.com");
  memberList.add("ajith.narayanan99@gmail.com");
  memberList.add("remeezkr@gmail.com");
  memberList.add("subin167@gmail.com");
  memberList.add("postforhari@gmail.com");
  memberList.add("binoyarun@gmail.com");
  memberList.add("riyass.ayath@gmail.com");
  
  return memberList;
  
 }
 
 private static List<String> testList(){
  
  memberList = new ArrayList<String>();
  
  memberList.add("binoyarun@gmail.com");
  memberList.add("yourrahulkr@gmail.com");
  memberList.add("adarshj001@gmail.com");
  
  
  return memberList;
  
 }

}

ConfigMessage.java is the class you want to run, here you can mention to which group you want to send message. Run this class and enjoy chatting.

Code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.rakesh.chat;

import java.util.List;
import java.util.Map.Entry;

/**
 * @author Rakesh KR
 * @since  July 2k14
 */
public class ConfigMessage {
 
 public static void main(String[] args) {
  
  SendChatMsg chatMsg = new SendChatMsg();
  
  for(Entry <String, List<String>> entry : Globals.groupMap.entrySet()){
   
   if(entry.getKey().equals("Food")){
    chatMsg.main(entry.getValue(), "Hi, Time For Lunch . :) ");
   }
   
  }
 
 }
 
}

Note: Needed jar's

kxml2-2.2.3.jar
smack.jar
smackx.jar
xmlpull-xpp3-1.1.4c.jar

Comments

Post a Comment

Popular posts from this blog

Transform Ubuntu into Xubuntu

Installation 1. Open the terminal by Ctrl+Alt+T     and type    sudo apt-get install xubuntu-desktop gksu leafpad synaptic         2. Type your password & Press Enter. Now an intensive operation is being launched. Simply wait to complete the whole process. Login To Xubuntu 1. After completing the installation logout ubuntu. Note: logout not restart or shutdown. 2. In the login window click on the ubuntu logo, next to your userName & select Xubuntu Sesion 3. Enter your password and Now the Xubuntu desktop appears. :) The next thing is to clean up. Clean Up 1. Now its time to clean up, inorder to prevent system pollution problems. Note: The clean up will remove as much as possible ubuntu's desktop environment Unity. So after that you can't use Unity. 2. Open terminal by Ctrl+Alt+T and type the following  sudo apt-get remove nautilus gnome-power-manager gnome-screensaver gnome-termina*...

PostgreSQL :: open-source relational database management system (RDBMS)

 PostgreSQL also known as Postgres, is a free and open-source relational database management system (RDBMS) emphasizing extensibility and SQL compliance.  PostgreSQL features transactions with ACID properties, automatically updatable views, materialized views, triggers, foreign keys, and stored procedures. It is designed to handle a range of workloads, from single machines to data warehouses or Web services with many concurrent users. PostgreSQL manages concurrency through multiversion concurrency control (MVCC), which gives each transaction a "snapshot" of the database, allowing changes to be made without affecting other transactions.  PostgreSQL provides an asynchronous messaging system that is accessed through the NOTIFY, LISTEN and UNLISTEN commands. PostgreSQL includes built-in support for regular B-tree and hash table indexes, and four index access methods: generalized search trees (GiST), generalized inverted indexes (GIN), Space-Partitioned GiST (SP-GiST) and Bloc...

15 Free Feature Rich Bootstrap Admin Templates

1. Charisma Charisma  comes with 9 different skins/themes to suit your style and application type. It is clean, easy to use and contains over 1000 icons and 15 plugins. It is also filled with numerous UI elements like star rating, pop over, custom tooltip, alerts, Ajax loaders, notifications and much more.  2. Siminta Siminta  comes with a large collection of plugins and UI components that help you make your work easy. All codes are self-explanatory (with comments) and the overall structure is easy to customize. It is free for personal as well as commercial use. 3. Metis Metis  is a free Twitter Bootstrap template with various layouts, components, forms, tables, maps, charts and menu level. It is also equipped with file manager, maps, error page structure, grid system and login page.  4. Hierapolis Yet another private  admin template  based on Twitter Bootstrap 3. It consists of beautiful fo...