Write your own MSN robot

zhaozj2021-02-16  57

Overview

Now all types of instant messaging software is dazzling, and it is believed that people who have exposed to Internet will use some of them. In China, for the current users, Tencent's QQ, Microsoft's MSN Messenger and NetEase's POPO ranked in the top three. However, it is more unfortunate that Tencent and Netease have not disclosed the outside world for QQ and POPO, which is because such technical barriers have made some instant messages to open up. New customer service channels.

Fortunately, the MSN instant messaging software developed by Microsoft Company, its client is closely integrated with the Windows system, and its operation is stable, easy to use, and the world's versatility. Once the simplified Chinese version is launched, the number of users is increased in geometries. What makes you cheerful is the development API of Microsoft's MSN and publish MSN communication protocols on its website. This makes many developers to expand their MSN and more personal services to customers. For example, the famous MSN Plus is a MSN feature expansion plugin developed by MSN API, which has been received for many MSN users.

At the same time, through the MSN communication protocol, there are many MSN robots using the MSN communication protocol on the network. They allow MSN users to enjoy services for all kinds of public information by receiving user's messages and automated processing.

Technology Introduction

Since the MSN communication protocol is based on the TCP / IP ASCII text protocol, the MSN server with the microsoft frame with Microsoft frames is communicated in the global MSN user through the established protocol, and the microsoft transmission ( The specific MSN protocol can get http://www.hypothetic.org/docs/msn/index.php) through this web page. However, MSN's protocol is quite complicated, and all communication commands are abbreviated. For developers who first read the agreement, they are familiar with and realize the agreement.

There are some development packages that implement the MSN P8 protocol, so that you can make energy more focused on logical implementation.

There are two popular development packages:

1. JMSN is a Java API that encapsulates the MSN Messenger open communication protocol. Through this API developer, you can use the Java language to simulate MSN Messenger software. The author of the API also provides an aspect of the Java language in a certain aspect function and even MSN. Messenger is also a powerful MSN client software. Due to the use of cross-platform Java language development, the software can also run in other operating systems simultaneously, and have a variety of Linux systems and Mac OS that have been tested, and of course there is a window operating system. JMSN is an open source API developed by Koreans. You can download from http://sourceforge.net/projects/jmsn / site. The home page of the project is basically Korean, including its API documentation. Korean.

2. DotMSN is based on .NET-based development kit developed by a Danish master, uses the MSN P8 protocol, although it does not support the currently launched MSN P10. However, the power of its development package is the best in the MSN development package I have used. It is especially true that the authors provide very detailed document support and sample code for DotMSN (http://members.home.nl/b.geertsema/dotmsn/example/example.zip). With this development kit, you can easily implement your own MSN robot.

Now, I will lead you to build my own MSN robot.

Technical preparations

Before using dotmsn, I will introduce you to the general situation of the .NET framework and the VS.NET integrate development environment, so that everyone is more convenient and fast in development. .NET framework

The .NET framework is a programming model for Windows Server System builds, deployed and running web services and applications. The frame hosts most deep structures that allow developers to focus on the business logic code of the application. The basic composition of the .NET framework is as follows:

l Public Language Operations (CLR)

l .NET FRMEWORK class

l asp.net

l Web service

l.NET remote processing

l Windows Forms

Where the CLR and .NET framework are all based on .NET programs.

The CLR can be a variety of different applications (ASP.NET programs, mobile programs, windows form programs), etc. are based on the same runtime. It is easy to block DLL duplicate version issues that become "DLL nightmare" in the deployment and product release of the VS 6.0.

The .NET framework includes a large number of classes, interfaces, and value types that allow the development process to accelerate and optimize while providing the access pathway for system functions. In order to achieve cross-language operability, .NET Framework Type Compliance with Public Language Norms (CLS), therefore can use any language supported by the compiler. This allows the company's IT department to expand, start the development of existing developers, and quickly start the .NET application, or use full interoperability to improve existing applications with .NET characteristics.

Other basic components are mostly related to different types of applications.

ASP.NET is a revolutionary programming framework that makes it possible to rapidly develop powerful web applications and services. It provides the most convenient, flexible way to build, deploy, and run web applications, facing any browser or devices.

XML Web Services is a series of function sets that are packaged into a single entity and publish data and services to other applications. Applications Access the Web Services through industry-standard web protocols, such as HTTP, XML, and Simple Object Access Protocol (Simple Object Access Protocol, SOAP), and do not need to be concerned with specific implementation methods for Web services. The Web service is the cornerstone of the establishment of an open distributed system.

Communication is a key factor across network or cross-care program domain objects, which are also called remote objects. .NET remote processing provides a powerful mechanism that allows objects to interact across the application domain.

The communication between the .NET application and the application domain is generated by the channel responsible for transmitting messages between remote objects. A message is first formatted (encoded or decoded) before being transmitted by the channel. The .NET Framework supports the HTTP and Transmission Control Protocol (TCP) channels, as well as SOAP and binary formatting procedures. The HTTP channel uses the SOAP formatting program to transmit messages, while TCP channels use binary formatting programs. Both channels can customize the use of other formatting programs. The channel provided by the third party is also possible. The HTTP channel uses the SOAP formatting program to transmit messages, while TCP channels use binary formatting programs. Both channels can customize the use of other formatting programs. The channel provided by the third party is also possible.

The Windows Form expands the traditional full desktop Windows application, and the development experience brought by Visual Studio will bring us the world of managed code and the public language runtime.

VS.NET integration development environment

To create a .NET application that runs managed code in the .NET Public Language Runture and .NET Framework class, Visual Studio .NET is the preferred integrated development environment (IDE). It continues the tradition of Visual Studio, allowing developers to compile the previous unmanaged C or C # applications. Visual Studio .NET allows the Rapid Development Application (RAD) to be implemented by efficient development tools. A closely integrated and unified visual environment simplifies the process of developing a web application, reducing the learning curve. Shared Hypertext Markup Language (HTML), XML, and Style Table Editor Lets Use any Visual Studio language to develop web applications that make it easy, including using new C # programming languages.

Visual Studio .NET allows developers to quickly build new managed code applications, and a rich features and class libraries for .NET framework. Visual Studio .NET also allows developers to produce traditional unmanaged C applications such as Microsoft Word or Microsoft Excel.

Program development

Let's start with me to make your own robot.

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.conversationCreatedCreated; // Restart 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); // Processing message receives a message

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

// 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.!

end

Ok, write here about the MSN robot. At present, I am also more in-depth agreement, I hope to communicate with friends who have the same interest. Looking forward to your letter,

My email address is sxfnrj@163.com

The above code is debugged in Microsoft Visual Studio .NET 2003

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

New Post(0)