Hello everyone , I meet you again , I'm your friend, Quan Jun .
Preface : current time 2022-4-24 There has been no water article for five months !personally Technology does not increase but regresses , Salty bullshit !
Have a fun today , use “ Ghost hand ” A free version of wechat pc End robot + Crawlers are used to forward articles or news in real time !
thank “ Ghost hand ” Free to share source code !( I just like exclamation marks ! Nothing else ! Not to emphasize !)
First dump github link :https://github.com/cixingguangming55555/wechat-bot
There are tutorials in it , But for convenience and in line with the principle of speaking in detail, let's talk about it .
All versions of wechat Extraction code :ha4a
The use of websocket Connect ,websocket It is also divided into long and short connections , We use short connections for convenience !
The following code implements three functions : Query groups and users id、@ Group members send messages 、 Send pictures or files . The code is together , It is split for convenience !
1、 Query groups and users id
# -*- coding:utf-8 -*-
# author:BC
import websocket
import time
import json
SERVER = 'ws://127.0.0.1:5555'
AT_MSG = 550
USER_LIST = 5000
PIC_MSG = 500
ATTATCH_FILE = 5003
def getid():
id = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time()))
return id
# Get the user name and... Of wechat address book wxid
def send_wxuser_list():
qs = {
'id': getid(),
'type': USER_LIST,
'content': 'user list',
'wxid': 'null',
}
s = json.dumps(qs)
return s
# Print group name
def print_wxuser():
ws = websocket.create_connection(SERVER)
ws.send(send_wxuser_list())
result = json.loads(ws.recv())
content = result['content']
for item in content:
id = item['wxid']
m = id.find('@')
if m != -1:
print(f' Wechat group :---->roomid:{id}----name:{item["name"]}')
else:
print(f' user :---->roomid:{id}----name:{item["name"]}')
if __name__ == '__main__':
ws = websocket.create_connection(SERVER)
print_wxuser()
2、@ Group members send messages
# Aite group members The following are required parameters
def send_at_msg(roomid, content, nickname):
j = {
'id': getid(),
'type': AT_MSG,
'roomid': roomid,
'wxid': 'your wxid',
'content': content,
'nickname': nickname,
'ext': 'null'
}
s = json.dumps(j)
return s
if __name__ == '__main__':
roomid = input('Roomid:') # Group id Or user id
content = input('send Content:') # What to send
nickname = input('@ name required:') # By @ The person's name
ws.send(send_at_msg(roomid, content, nickname))
print('[**] Aite succeeded !!!')
3、 Send pictures or files
# Send pictures or files
def send_pic_msg(type, wxid, path):
j = {
'id': getid(),
'type': type,
'wxid': wxid,
'roomid': 'null',
'content': path,
'nickname': "null",
'ext': 'null'
}
s = json.dumps(j)
return s
if __name__ == '__main__':
type = input('[***] Function options ----A、 Send pictures \tB、 Send a file ( Enter upper and lower case letters ):')
wxid = input('wxid:') # Group id Or user id
path = input('path:') # File path to be sent
if type == 'A' or type == 'a':
ws.send(send_pic_msg(PIC_MSG, wxid, path))
print('[**] Picture sent successfully !!!')
elif type == 'B' or type == 'b':
ws.send(send_pic_msg(ATTATCH_FILE, wxid, path))
print('[**] File sent successfully !!!')
Aha, AHA …( I'm going to make a template for the knowledge planet crawler ! I think it's unnecessary to omit !)
ws = websocket.create_connection(SERVER)
ws.send(send_at_msg(roomid, str(content), nickname)) # Group id、 Content 、@ The person's name
ws.close()
Write a loop , You can send wechat messages all the time !( She or he ignores you , Then bomb him ) Hey, hey, evil ! Don't try easily , Consequence conceit !
Conclusion :
For a long time , I feel like I haven't written ! A lot of rubbish , It's true that water spans ! be it so !
The lonely Wutong spring courtyard locks the clear autumn , Shear constantly , Richard also disorderly …
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/150698.html Link to the original text :https://javaforall.cn
Preface : It is said that “ Ho