MSN Robot DreamWorks

xiaoxiao2021-03-06  74

A few days ago, I saw a BLOG article on the MSN robot. Then search on Google, it turns out, now popular MSN robots!

Since MSN Messenger discloses its communication protocol, many people join to write their own robots. The full text of the MSN protocol has a total of 20 pages, which is much less than the SMTP protocol. But for most developers, it is still a difficult thing to write robot procedures based on the original protocol (more way to mention the content of the agreement).

Also on Google, I found in the Java field, the popular robot development tool is a JMSN development kit written by Koreans. It encapsulates complex protocols into simple and easy-to-use Java APIs for users to call. (Download address: http://sourceforge.net/projects/jmsn)

Although it is said that it is still a bit hindering. All JMSN development kits have been written in Korean. For those who are very tasty in our English, this is simply a book!

But don't matter, you are welcome to enter the robot DreamWorks, together with me to explore the manufacturing process of the MSN robot.

Now let's start to achieve a homemade MSN robot dream.

Don't study the development documentation of Korean. First follow the author to study an interesting robot to see how it works. Then go further by step to study the working principle of the robot.

This is an interesting robot, what do you say about it, what should it be. I called it to be a syndrome robot. Let's take a look at its structure:

Import java.util.properties;

import rath.msnm.MSNMessenger; import rath.msnm.SwitchboardSession; import rath.msnm.UserStatus; import rath.msnm.entity.MsnFriend; import rath.msnm.event.MsnListener; import rath.msnm.ftp.VolatileDownloader; import rath .msnm.ftp.VolatileTransferServer; Import Rath.msnm.msg.mimeMessage;

/ *** MSN should be insect robot. * @Author Turbo Chen * @create 2004-7-29 * / public class YesmanRobot {public static void main (String [] args) {MSNMessenger msn = new MSNMessenger ( "yourname@hotmail.com", "xxxxxxxx"); msn .SetinitialStatus (UserStatus.online); Msn.addmsnlistener (MSN)); msn.login ();}}

Class YesmanRobotadapter Implements Msnlistener {

MsnMessenger MSN;

Public YESMANROBOTADAPTER (MsnMessenger MSN) {this.msn = msn;}

/ *** Receive a message event. This method is automatically called when you receive a message. * / Public void instantMessageReceived (SwitchboardSession ss, MsnFriend friend, MimeMessage mmsg) {try {// send the same reply message to the sender MimeMessage newMsg = new MimeMessage ( "I am the MSN yes-man, what should I beg your pardon:" mmsg .getMESSAGE (); newmsg.kind_message; system.out.println (newmsg.getMessage ()); msn.sendMessage (Friend.getLoginName (), newmsg);} catch (Exception E) {e. PRINTSTACKTRACE ();}} ....

Among them, YesmanRobot is the main class of the robot. In order to make the robot work, let it log in to MSN, the relevant code is as follows:

MsnMessenger msn = new msnmessenger ("Yourname@hotmail.com", "xxxxxxxx"); msn.setinitialstatus (userStatus.onLine); Msn.AddmsnListener (new YesmanRobotadapter (MSN)); msn.login ();

A MSNMessenger object is created here, which is sent to the login account and password. Use the setInitialStatus method to set the status of its login to 'online', and finally the login method is logged in.

In order to make the robot can reach the function of "Synchrony", we add a listener to it before logging in. This listener is an implementation class of MsnListener. Here we implement a YesmanRobotadapter class, which only implements an InstantMessageReceiveD method. When there is a message to the robot, this method is triggered. In this method, our robot will send the news that the other party sent back to each other. . This is the function of the responent insect.

In the actual complete example, you will find that MsnListener has up to 28 interfaces, which means that in addition to the event that the message is received, many other events are provided for us to use. In future articles, we will slowly come into contact with these events.

Through this informed robot, we know that the robot to realize its function is not difficult. Just need to process the received message in the InstantMessageReceiveD method and respond, turn your own robot. In the later content, we will deepen the internal roots of the robot and see its working principle.

Without further ado. Connected back.

An example of a response robot by the last, we initially learned the basic usage of the JMSN development package, I first draw a working principle to everyone:

MsnMessenger is the main program. It contains a NOTIFICATIONPROCESSOR, the processor of this communication event, in nature it is a stand-running thread. MsnMessenger offers many name-friendly ways to send a communication command to other contacts (through servers). Sending a communication command is processed through the NotificationProcessor processor in the background (standalone thread).

All communication commands are encapsulated in the name-friendly method, so you don't need to understand the underlying communication protocol of MsnMessenger. MsnMessenger provides many practical methods to call. For example, login, logout, add or delete friends (AddFriend, RemoveFriend), establish and delete groups (AddGroup, RemoveGroup), invite friends to chat, or invite someone to join existing chat medium Wait. With these ready-made methods, you can make your robots become very powerful, of course, you can use it to make a chat tool in place Microsoft Messenger. The command is not immediately returned after sending out the processor. It is responsible for running by the processor in the background. That is, after you call the login command, if you want to add someone as a friend, then the program will report an error. The login process may be a slow process, it is necessary to communicate with the server, waiting for the server to return to the response information. The processor does these things in the background. After completing, it will notify you by the MsnListener listener you registered in the MsnMessenger class.

So don't truly provide some methods in MsnMessenger in MsNMessenger (mainly to those similar methods mentioned earlier), you should perform the corresponding command after waiting for the event in your own listener.

MsnListener is an interface that provides up to 28 event notification methods. Including login completion, friends online or offline, start sessions or end sessions, receive new messages, who adds yourself for friends.

This time, this time is here. Finally, we will improve the last response process, let it send a message to him after someone on the line (otherwise knowing you is a candidate: d).

The code is the same, just two ways to achieve in YesmanRobotadapter:

/ *** The friend starts with its conversation when online. * / public void useronline (msnfriend friends) {try {system.out.println ("Start Invitation" Friend.getLoginName () "Session"); msn.docall (Friend.getloginname ());

} catch (ioexception e) {// TODO processing exception E.PrintStackTrace ();}}

/ *** The session begins. * / public void switchboardsessionStarted (SwitchboardSession SS) {Try {

IF (ss == null) return; string Tofriend = ss.getmsnfriend (). getloginname ();

IF (MSN.FindswitchboardSession (TOFRIEND)! = null) {mimeMessage msg = new mimeMessage ("Hello, this is the MSN should be called with you. Welcome to ask questions); msg.setkind (MimeMessage.kind_Message); Boolean Success = Msn.sendMessage (Tofriend, MSG); if (success) System.Out.println ("Say hello.");

} catch (ioException ex) {// TODO processing exception ex.printstacktrace ();}}

转载请注明原文地址:https://www.9cbs.com/read-93001.html

New Post(0)