Write your own MSN robot

xiaoxiao2021-03-13  188

Introducing dotmsn development kits

First we must first create a solution, because the .NET Framework CLS (General Language Specification) can support a variety of different development languages, you can use the current .NET supported VB.NET, C #, J #, J #, J # , C and other languages.

After the solution is established, the first step is to do is to reference the DotMSN development package to the solution so that we can use it in the future development process.

You can see the DotMSN development kit in the referenced file list. Below we can start using DotMSN to communicate with the MSN server after we design the user interface of the robot program through the IDE.

In dotmsn, there is a Messenger class, which is all of your programming, so you have to declare an object of such an object in the program and apply for memory and initialization:

Private dotmsn.Messenger Messenger = new Messenger ();

After you entered the MSN robot's email address, authentication password, and displayed name, the first thing to do is to log in to the MSN server, I have written a StartMSN function here to include login action, of course, this function contains more logins, Some message events of MSN after logging in have also been an overloaded statement:

// Start the MSN program

Private void startmsn ()

{

Try

{

/ / Confirm that the input Passport account is not the default setting, otherwise an error!

IF (txtmail.text == "YourMail@hotmail.com")

MessageBox.show (this, "Type your Passport to log in to MSN servers!");

Else

{

// Event Overload code segment

// Re-load the online function

Messenger.Contactonline = new Messenger.contactonlineHandler (Contactonline);

// Restart the lower line function

Messenger.contactoffline = new messenger.contactofflinehandler (Contactoffline);

/ / Re-serve the contact join function

Messenger.contactadded = new Messenger.contactaddedHandldDed;

// Overreload session creation functions

Messenger.conversationCreated = New Messenger.conversationCreated;

// Heavy duty synchronous processing function

Messenger.synchronizationCompleted = New Messenger.synchronizationCompletedHandler (onsynchronization ";

Messenger.Connect (txtmail.text, txtpass.text);

Log (this.txtmail.text "Login Success!");

Messenger.synchronizelist ();

}

}

Catch (MsNexception E)

{

Messagebox.show (this, "connection failed:" E.toTOString ());

LOG ("Connection Failure:" E.TOString ());

}

}

Where the Messenger's Connect method is the main method of logging in to the MSN server. We need to perform overload processing for some message events of MSN before logging in to trigger the logic processing code we have written in the future. After logging in to the MSN server, the method that immediately needs to be called is Synchronizelist (), that is, synchronous contact with the server, the list is divided into the following:

l ForwardList: All valid contact lists can be sent to each other.

l ReverseList: All your contact list in the other contact list.

l BlockedList: All you block the contact list.

l AllowedList: All contact lists that can see your status.

Through the operation of the above four lists, the robot can be made to understand the relevant state of all contacts and implement the corresponding operation.

After the completion of the sync contact list, you need to set the state of the Messenger. Settings allows the contact to see different online conditions of the robot. At the same time, the settings through Messenger.OWNER.NAME allow the robot to display the name you need in the contact.

Next, the main job is to process the overloading of MSN's various events. I will give an example of two event processing, and other events can be found from the Dotmsn's help file. The various types of robots are implemented by overloading of these events.

// Session creation time handler

Private Void ConversationCreated (Messenger Sender, ConversationEventArgs E)

{

// Record the session creation log

TXTLOG.APPENDText ("Session Create Success! / R / N");

// Process function when the contact is added to the dialog

E.Conversation.contactJoin = new conversation.contactjoinhandler (contactJoad); / / Connection Establishment Processing function

E.conversation.connectionestablished.Connectionestablished; CONNECTIONESTABLISHED DISTAL

/ / Contact Enter the message to handle functions

E.Conversation.UserTyping = new conversation.UserTypingHandler (ContactTyping);

/ / Handling Messages Received Messages

E.Conversation.MessageReceived = new conversation.MessageReceiveDHandler (MessageReceIved);

// Receive file transmission time processing function

E.conversation.fileTransferHandler.invitationReceived = new dotmsn.fileTransferHandler.FileTransferinvitationHandler (FileTransferHandler_FileTransferInvitation);

}

The first thing to handle is a session creation event, that is, the robot is starting with the connection, and 5 related events are overloaded in the code. From the code, they have been able to conject these events. The overloading of the session creation event itself is declared in STARTMSN (). Below is the overload code of these events:

/ / Contact Enter the message to handle functions

Private Void ContactTyping (Conversation Sender, ContactEventArgs E) {

TXTLOG.APpendText (E.Contact.name "Enter messages ... / r / n");

Log (E.Contact.name "Enter Messages ...");

}

/ / Contact Add to Dialog Processing Function

Private Void ContactJoad (Conversation Sender, ContactEventArgs E)

{

/ / Contact Add to Dialogue, Record the contact name to the log

TXTLOG.APPENDText (E.Contact.name "Add Dial! / R / N");

Log (E.Contact.name "Add to Dial!");

// Enter welcome words

Sender.sendMessage ("Hello! I am" Messenger.Owner.name);

Sender.sendMessage ("Send SMS Please use the following command: mobile phone number # To send content");

//sender.sendMessage ("If you need help, type get_help_file");

}

/ / Connection establishment process function

Private Void Connectionestablished (Conversation Sender, Eventargs E)

{

TXTLOG.APPENDText ("Connection Complete! / R / N");

}

/ / Processing function when receiving contact messages

Private void MessageReceived (Conversation Sender, MessageEventArgs E)

{

String msg_rcv = E.MESSAGE.TEXT;

Msg_rcv = E.Message.Text.Replace ("/ 0", "");

TXTLOG.APPENDText (E.Sender.name "said:" MSG_RCV "/ R / N");

LOG (E.Sender.name "said:" MSG_RCV);

String msg_dnt = messagehandle (msg_rcv, e.sender.name, e.sender.mail);

Sender.sendMessage (MSG_DNT);

LOG ("Robot Send Message:" MSG_DNT);

}

// File reception action processing function

Private Void FileTransferHandler_FileTransferInvitation (FileTransferHandler Sender, FileTransferinvitationEventArgs E)

{

// Receive action is true

e.accept = true;

// Received files Save under D: / MSN_BOT_FILES

String tmpfile = "d: // fileofbot //" sender.conversation.Messenger.Owner.name "@" E.FileTransfer.FileName;

E.FileTransfer.ReceiveStream = New FileStream (TmpFile, FileMode.create, FileAccess.Readwrite);

TXTLOG.APPENDText ("Receive files and saves:" TMPFILE "/ r / n");}

Among these incidents, the MessageReceived event is more important, which means that the processing logic when the robot receives the text message sent by the user, it is where you can freely play! I have written independent MessageHandle functions here to process all received messages. In this processing function, all robots intelligence and related business functions can be implemented according to your own positioning of robots.

The robot here is to decode the message command sent by the user and send the mobile SMS according to the target mobile interrupt input by the user. Of course, this is a support for the back-end to have a SMS gateway or a related SMS sender. You see, you easily have your own SMS service robot.

With the support of the rear end, the robot can also query the relevant information of the database according to the user's input and return to the user.

There is a way to expand users, customer service, product promotion, etc.!

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

New Post(0)