程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The boy wrote QQ in Python. The robot talked with his girlfriend all afternoon and was not found?

編輯:Python

1 Technology selection

1.1 QQBot

The first thing Baidu learned is QQBot, It's a use. python Realized 、 Based on Tencent SmartQQ The simplicity of the agreement QQ robot , Can run in Linux and Windows Under the platform , All code is integrated in one qqbot.py In file , You can use it. To achieve :

  • monitor 、 collect QQ news
  • Automatic message push
  • chatbot

What's not enough is , You need to scan manually every time you log in , Very inconvenient ; There are few functions of support , Do not meet the needs after .

1.2 cool Q

cool Q It is an intelligent robot software , It contains powerful plug-in mechanism , Various plug-ins can be installed , Extend cool Q All the functions of , There's a free version , Fee version , Here are the functions that the free version of the game can support

  • Login support account password login
  • A private chat , Group news , Discussion group messages , Group management operation
  • news : Text message ,At Others ,QQ expression ,Emoji expression

The basic free function can already support my needs , I chose cool Q. cool Q It just encapsulates SmartQQ The agreement , It has no function , Functions can be extended through plug-ins .

1.3 Universal Taobao

Search all kinds of software for mass messages on universal Taobao , A lot of , The price is very cheap . Why do we need to get one by ourselves .

  • Security , In case you use other software ,qq Stolen
  • Developing a similar function on Taobao is not complicated
  • Self developed can be changed according to needs , More flexible , For example, I will connect a Turing robot today , It's convenient to connect other robots tomorrow

2 Start tossing about

2.1 Install the required software

cool Q After downloading directly , Double click the installation directly .

2.1.1CoolQ HTTP API plug-in unit

adopt HTTP Yeah, cool Q To report and receive events HTTP Request to call cool Q Of DLL Interface , So you can use other languages to write cool Q plug-in unit . Now support WebSocket.

Download the latest cpk Put the file in cool Q Of app Folder , Then enable it . After enabling, the plug-in will open a HTTP Server to receive requests , Default listening 0.0.0.0:5700, The first time you enable it, a default configuration file will be generated , In cool Q Of app
io.github.richardchien.coolqhttpapi\config In the folder , The file named <user_id>.json

At this point through
http://192.168.1.123:5700/ You can call cool Q Function of , for example http://192.168.1.123:5700/send_private_msg?user_id=123456&message= Hello , Notice the 192.168.1.123 Change to your own computer IP, If you run locally , It can be used 127.0.0.1,user_id Also replace it with the one you want to send to QQ Number .

2.1.2 NoneBot

NoneBot It's based on cool Q Of Python asynchronous QQ Robot frame , It will be right QQ The message received by the robot is parsed and processed , And in the form of plug-ins , To the command processor and natural language processor corresponding to the message , To complete specific functions .

In addition to parsing messages ,NoneBot It also provides a large number of practical preset operations and permission control mechanisms for plug-ins , Especially for command processors , It also provides a perfect and easy-to-use session mechanism and internal call mechanism , To meet the requirements of continuous interaction of commands and reuse of internal functions of plug-ins .

NoneBot At its bottom with cool Q The interactive part uses python-aiocqhttp library , The latter is CoolQ HTTP API One of the plug-ins Python asynchronous SDK, stay Quart On the basis of the package and CoolQ HTTP API Network interaction of plug-ins

Install... With the following command

pip install none-bot

2.2 Start writing

2.2.1 Set the configuration of the plug-in

cool Q Of app
io.github.richardchien.coolqhttpapi\config\ There should already be a directory named .json The file of ( Login for you QQ account number ). Modify this file , Add the following configuration items , And restart cool Q

{

"ws_reverse_api_url": "ws://127.0.0.1:8080/ws/api/",
"ws_reverse_event_url": "ws://127.0.0.1:8080/ws/event/",
"ws_reverse_reconnect_on_code_1000": true,
"use_ws_reverse": true
}

2.2.2 start-up NoneBot

After starting , You can see the information of the console as shown in the figure below , It means it has been started successfully .

import none
if __name__ == '__main__':
none.init()
none.load_builtin_plugins()
none.run(host='127.0.0.1', port=8080)

After starting , Console information


2.2.3 Develop the function of sending messages in groups

Through another qq Send information to the current qq The function of sending messages in groups , Just need the following lines of code . Of course, it can also be changed to obtain user information , Send users' information in groups

# Monitor the messages sent “ Mass hair ” Events
@on_command('send_msg', aliases=(' Mass hair ',))
async def send_msg(session: CommandSession):
message_type=session.ctx['message_type']
user_id=session.ctx['user_id']
# Judge whether the message sent is private , And sent qq The number is 12345678
if message_type=='private' and user_id==12345678:
# obtain qq Group information
group_list = await session.bot.get_group_list()
for group in group_list:
# To someone qq Group to send messages
await session.bot.send_group_msg(group_id=group['group_id'],message=' Messages to be sent in groups ')

3 Application extension , matters needing attention

What I mentioned above is only the most basic function of sending information , There are also those that can be played ?

  • The function of group management , Automatically detect new members , You can send group rules to new members , guide . It can detect that the information sent by group members does not conform to the group rules , Automatically disable , Kick out and other operations
  • Realize swarm robots , for example @ Group robots , Automatic recovery , Realize the interaction between members and robots ; Advanced points can be connected to external interfaces such as Turing robots .

matters needing attention : Don't send content that others don't need , Once reported, it will be blocked ; Don't send too fast , Don't send too much information , Will be detected , Will also be sealed .

About Python Technology reserve

Learn from good examples Python Whether it's employment or sideline, it's good to make money , But learn to Python Still have a learning plan . Finally, let's share a complete set of Python Learning materials , For those who want to learn Python Let's have a little help !

One 、Python Learning routes in all directions

Python All directions are Python Sort out the common technical points , Form a summary of knowledge points in various fields , The use of it is , You can find the corresponding learning resources according to the above knowledge points , Make sure you learn more comprehensively .

Two 、 Learning software

If a worker wants to do a good job, he must sharpen his tools first . Study Python Common development software is here , It saves you a lot of time .

3、 ... and 、 Getting started video

When we were watching videos to learn , You can't just move your eyes and brain without hands , A more scientific way to learn is to use them after understanding , At this time, the hand training program is very suitable .

Four 、 Practical cases

Optical theory is useless , Learn to knock together , Do it , Can you apply what you have learned to practice , At this time, we can make some practical cases to learn .

5、 ... and 、 Interview information

We learn Python Must be to find a well paid job , The following interview questions are from Ali 、 tencent 、 The latest interview materials of big Internet companies such as byte , And the leader Ali gave an authoritative answer , After brushing this set of interview materials, I believe everyone can find a satisfactory job .


This full version of Python A full set of learning materials has been uploaded CSDN, Friends can scan the bottom of wechat if necessary CSDN The official two-dimensional code is free 【 Guarantee 100% free


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved