Use Qqbot to make a simple text message auto-reply based on Python

Source: Internet
Author: User

Qqbotis a free open source based onSMARTQQof thepythonplug-in if the default installation hasPip, you can execute it directly at the command line:pip Install QqbotinstallationQqbot, you can enter it at the command line after successful installationQqbot Helpview the Help documentation. If you enter directlyQqbotwill automatically pop up a two-dimensional code image, using the phoneQQScan code can be started successfully, will automatically save the login information to the local file, the next time you can enterqqbot-q QQnumber to start, if the login information is not expired, you can not scan the code landing. Qqbotafter starting, you can open another terminal to operateQqbot,Input:QQ Help|stop|restart, these three commands are help documentation, shutdown, and restart, respectively.

the following implementation of their own text message automatic reply. Qqbot provides a message response function, we only need to register this message response function to receive QQ message, this message response function is written as follows:

 fromQqbotImportQqbotslot as Qqbotslot,runbot@qqbotslotdefonqqmessage (bot,contact,member,content):ifContent = ='-hello': Bot. SendTo (Contact,'Hello')    elifcontent=='-stop': Bot. SendTo (Contact,'The robot is off .') bot. Stop ()if __name__=='__main__': RunBot ()

Note that the message response function name must be Onqqmessage, and the function arguments must be consistent.

after writing the message response function, Save asxx.py, ifQqbothas started please closeQqbotprocess, and then at the command line, enterpython xx.py-q QQnumber so that it can be startedQqbot, and will putxx.pyautomatically register toQqboton the message response function, whenQQwhen you receive a message, you see it on the command line. There is also a runningxx.pyis to treat this file as a plug-in,Qqbotloads the unload dynamically during the run. The first thing you need to do is save this file in your system.Importto the directory, such aspythonthe installation directory of thelib/site-packagesdirectory, inQqbotafter booting, enter in another consoleQQ plug XXYou will be able toOnqqmessagefunction is registered toQqbotgo in, enterQQ Unplugin XX. If the plug-in form loadsxx.py, the contents of the file can be simplified and retained onlydef onMessagefunction, the other can be deleted.

Here is a brief introduction to incomingOnqqmessagethe four parameters,Botis aQqbotobject that provides theList/send/stop/restartfour interfaces,Listinterface is a query interface, you can actively query the contact list and other information, specific details also need to consult the relevant documents, this case is useless to this interface,Sendis an interface that sends messages.Stopis to stopQqbotthe interface,Restartis rebootQqbotthe interface. Contactis the sender object of the message,memberThis message is only valid when the message is a group or discussion group, and represents the member that sent the message. contentis aStrthe contents of the object, message.

QqbotSeveral methods are provided to send two-dimensional code login verification.,respectively haveGUImode, mailbox mode, server mode, text mode. Default isGUImode, when other modes are turned on,GUIthe mode is off. Generally used isGUIand Mailbox mode. On the first startQqbotafter the user directory on the computer system diskC:\Users\PC account name (which may beC +User\computer's account name) there will be a. Qqbot-tmpfolder, in this folder, there is av2.3.conffile, this file is the login authentication configuration file, open the configuration file, there is a description of the role of configuration, you can copy oneSomebodychange to the landing mode you want, assuming I copied a copy of theSomebodyadded inSomebodyback, and namedsome, modify Customsomecontent, you can enter it the next time you startQqbot-u someorpython xx.py-u someStartQqbot.

The following gives a personal implementation of the QQ on the "joke" message when automatically reply to a joke, joke is the last blog post crawl storage, from the MySQL database to get jokes from the python The code is also in the previous blog post. The specific code is as follows.

 fromQqbotImportQqbotslot as Qqbotslot,runbotImportMysql@qqbotslotdefonqqmessage (bot,contact,member,content):ifContent = ='Jokes': Bot. SendTo (Contact,mysql.get_one ())elifcontent=='-stop': Bot. SendTo (Contact,'The robot is off .') bot. Stop ()if __name__=='__main__': RunBot ()

A brief description of the contact and member objects is given below . You can use contact.__dict__ to get the properties of an object, note that if it's a one-to-a-private chat, the member object will be nonetype . Due to the limitations of SMARTQQ, many functions are missing, such as unable to obtain the real QQ number.

The contact object __dict__ is roughly as follows:

# {' QQ ': ' #NULL ', ' uin ': ' 3285709011 ', ' Nick ': ' Mytest2 ', ' Mark ': ',#  ' name ': ' Mytest2 ', ' Gcode ': ' 3285709011 ', ' CType ': ' Group '}

UIn unique identity, group chat Identity Group, one-to-one private chat when the identity of QQ. Nick is a group name, CType explains what type of message this is, group says it is a group, and Buddy says it is a private conversation.

The member object __dict__ is roughly as follows:

#{' QQ ': ' #NULL ', ' uin ': ' 3012699167 ', ' Nick ': ' XX ', ' Mark ': ' #NULL ', # ' card ': ' xxxx ', ' name ': ' xxxx ', ' join_time ':-1, ' LA St_speak_time ': -1,# ' role ': ' #NULL ', ' role_id ':-1, ' Is_buddy ':-1, ' Level ':-1, ' levelname ': ' #NULL ', # ' point ':-1, ' Cty PE ': ' Group-member '}

When the private chat this object is Nonetype,uin unique identity group members, Nick logo QQ nickname, card for the group chat message that QQ nickname remarks.

In fact, through the onqqmessage we can do a lot of things, Qqbot also provides some other features, you can refer to Baidu related information to get. The next article will introduce the access Turing person to realize intelligent chat.

Use Qqbot to make a simple text message auto-reply based on Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.