Creating a Simple Yahoo Messenger Auto Response with Java and OpenYMSG Library
Hi, several days ago someone asked me to help him on his assignment related to Yahoo Messenger Application with Java. That’s why today im trying to help him by creating a simple example on auto response and message sending using one of the best open source Yahoo Messenger API implementation, OpenYMSG.
It’s actually quite easy, only several lines of codes. This is how i do it
package com.edw.yahoo; import java.util.Scanner; import org.apache.log4j.Logger; import org.openymsg.network.FireEvent; import org.openymsg.network.ServiceType; import org.openymsg.network.Session; import org.openymsg.network.event.SessionEvent; import org.openymsg.network.event.SessionListener; /** * com.edw.yahoo.YahooLogin * * @author edw */ public class YahooLogin implements SessionListener { private Logger logger = Logger.getLogger(YahooLogin.class); private Session session = new Session(); public static void main(String[] args) { YahooLogin yahooLogin = new YahooLogin(); yahooLogin.doLogin(); } public YahooLogin() { } private void doLogin() { try { // insert your yahoo id // as for this example, im using my yahoo ID "dombaganas" session.login("dombaganas", "MyYahooPassword", true); session.addSessionListener(this); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("please insert your message : "); String message = scanner.nextLine(); // logout if message equals to "bye" if (message.equalsIgnoreCase("bye")) { break; } // send message to targeted yahoo id session.sendMessage("TargetYahooID", message); } // logout from YM session.logout(); } catch (Exception e) { logger.error(e, e); } } /* * this is my listener method * it listen for YM message request */ @Override public void dispatch(FireEvent fe) { ServiceType type = fe.getType(); SessionEvent sessionEvent = fe.getEvent(); if (type == ServiceType.MESSAGE) { try { // log request message logger.debug("message from " + sessionEvent.getFrom() + " \nmessage " + sessionEvent.getMessage()); // give an automatic response session.sendMessage(sessionEvent.getFrom(), "hi, you are sending " + sessionEvent.getMessage()); } catch (Exception e) { logger.error(e, e); } } } }
These are some screenshots related to this application.
To my friend, i hope this tutorial can help you getting started with your assignment. As the wise man once said “Answering one good question is like feeding a hungry person one meal, but teaching them research skills by example is showing them how to grow food for a lifetime.”
YNWA
(I)
ps. if somehow you found a NoClassDefFoundError, such as :
Exception in thread βmainβ java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
you need to include apache common logging lib into your project classpath.