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

ജാവാ വളരെ സിമ്പിൾ ആണ് , പവർഫുൾ ഭയങ്കര പവർഫുൾ ആണ്.

ജാവ യെ  കുറിച്ച്  മലയാളത്തിൽ  ഡോകുമെന്റുകളോ  ബ്ലോഗ്‌കളോ  കൂടുതൽ ഇല്ലാത്തതുകൊണ്ട് എനികറിയവുന്നത് ഷെയർ ചെയ്യാം എന്ന് വിചാരിച്ചു .  ജവയെ കുറിച്ച് പറയുകയാണെങ്കിൽ  ജാവാ വളരെ സിമ്പിൾ ആണ് , പവർഫുൾ  ഭയങ്കര പവർഫുൾ ആണ്. :) 1. നമ്മടെ Sun MicroSystems 1995 ൽ തുടങ്ങി വച്ചാ സംഭവമാണിത് . 2. ഇപ്പൊ ജാവയുടെ എട്ടാമത്തെ വെർഷൻ  വരെ ഇറങ്ങികഴിഞ്ഞു . ജാവയുടെ തിയറിയെ കുറിച്ച് എനിക്ക് പറയാൻ താല്പര്യം  ഇല്ലാ, അത് അറിവിള്ളവർ നല്ലപോലെ പറഞ്ഞിടുണ്ട്. നമുക്ക് ഡയറക്റ്റ്  ആയി കോഡിംഗ് പാർട്ടിലേക്ക്  പോകാം. എങ്ങനെയാ ജാവ ഇൻസ്റ്റോൾ ചെയണ്ടേ എന്നൊക്കെ ഒന്ന് ഗൂഗിൾനോട് ചോദിച്ചാ മതി പുള്ളി പറഞ്ഞുതരും. പിന്നെ എല്ലാവരും പറയുന്നു ജാവാ Object  Oriented ആണെന്ന് . അതെ ജാവാ Object Oriented ആണ് . എന്താണ് Object ?, Object  നു വച്ചാ ഒരു സാധനം അത്രതന്നെ. ഇപ്പൊ example പറയുകയാണെങ്കിൽ Book ഒരു സാധനമാണ് . Book റ്റെ പ്രതേകത എന്തൊക്കെ അന്നെന്നുവച്ചാ ആതിനൊരു Auther ഉണ്ടാകും, അതൊരു Category ൽ പെട്ട book ആയിരിക്കും (Novel / Academic ) പിന്നെ ഓരോ bookനും അ...

Angular (web framework)

 Angular is a TypeScript-based free and open-source web application framework lead by the Angular Team at Google and by a community of individuals and corporations. Angular is a complete rewrite from the same team that built AngularJS. Google designed Angular as a ground-up rewrite of AngularJS. Angular 14 was released on June 02, 2022. Some new features include typed forms, standalone components, and new primitives in the Angular CDK (component dev kit). All the major releases are supported for 18 months.  This consists of 6 months of active support, during which regularly-scheduled updates and patches are released. It is then followed by 12 months of long-term support (LTS), during which only critical fixes and security patches are released. Official website :- https://angular.io/

Java String Token

Problem    Given a string, find number of words in that string.     For this problem a word is defined by a string of one or more english alphabets. Input Format   A string not more than 400000 characters long.    The string can be defined by following regular expression:       [A-Za-z !,?.\_'@]+   That means the string will only contain english alphabets,    blank spaces and this characters: "!,?._'@". Output Format   In the first line, print number of words nn in the string.    The words don't need to be unique. In the next nn lines,    print all the words you found in the order they appeared in the input. Sample Input   He is a very very good boy, isn't he? Sample Output 10 He is a very very good boy isn t he Solution import java.io.* ; import java.util.* ; public class Solution { ...