示例中
python:3.9.5
微信桌面端3.6
1、wechat/api/ message.py
import os
import regex
class Message:
def __init__(self):
pass
def send_text(self, wechat, wid, text):
if wechat is None or wid is None or text is None:
raise Exception('the value of args is not correct')
else:
wechat.send_text(wid, text)
"""
:parameter
filepath:
{
dir:示例../img,发送img目录下所有图片
absolute path:D:/workspace/pycharm/py_tools/wechat/img/img.png
relative path:../img/img.png
}
flag:
{
0:图片 发送img目录下的
1:文件 发送file目录下的
}
"""
def send_file(self, wechat, wid, filepath, flag=0):
if wechat is None or wid is None or filepath is None:
raise Exception('the value of args is not correct')
else:
if os.path.isdir(filepath):
files = os.listdir(filepath)
for j in files:
path = os.path.join(os.path.abspath(filepath), j)
if flag == 0:
if os.path.getsize(path) == 0:
print("image size can't be 0")
continue
else:
wechat.send_image(wid, path)
elif flag == 1:
if os.path.getsize(path) == 0:
print("file size can't be 0")
continue
else:
wechat.send_file(wid, path)
else:
tp = os.path.split(filepath)
path = os.path.join(os.path.abspath(tp[0]), tp[1])
if flag == 0:
wechat.send_image(wid, path)
elif flag == 1:
wechat.send_file(wid, path)
"""
:parameter
flag:
{
None:精确找人
!None:正则找群
}
"""
def get_person_wxid(self, wechat, pattern, flag=None):
list_wx = []
persons = wechat.get_contacts()
if flag is None:
for p in persons:
if p['nickname'] == pattern:
return p['wxid']
else:
group = wechat.get_rooms()
for g in group:
if regex.search(pattern, g['nickname'], regex.M) is not None:
list_wx.append(g['wxid'])
return list_wx
2、wechat/api/resolve_yaml.py
注:pip install pyyaml
__author__ = 'xiao'
import yaml
# parsing yaml files
def resolve(path):
with open(path, encoding='UTF-8') as file:
result = yaml.safe_load(file.read())
return result
3、wechat/config/setting.yaml
case: person
person:
name: 靜寶兒° # wechat's nickname
type: text # value(text、image、file)
file_path: # you can specify the absolute path or relative path of the file, or specify a directory
text_content: test # text message to be sent
group:
pattern: 不具名 # the group name of wechat
type: image
file_path: img
text_content: test send message
times: 8 #循环次数,为空则代表发送1次
interval: 3 #间隔时间
answer_content:
4、wechat/main.py
# # -*- coding: utf-8 -*-
import time
from api.message import Message
from api.resolve_yml import resolve
import ntchat
config = resolve('config/setting.yaml')
wechat = ntchat.WeChat()
wechat.open(smart=True)
wechat.wait_login()
if wechat.login_status:
print('login successed')
def r():
me = Message()
if config['case'] == 'person':
wxid = me.get_person_wxid(wechat, config['person']['name'])
if config['person']['type'] == 'text':
me.send_text(wechat, wxid, config['person']['text_content'])
elif config['person']['type'] == 'image':
me.send_file(wechat, wxid, config['person']['file_path'])
elif config['person']['type'] == 'file':
me.send_file(wechat, wxid, config['person']['file_path'], flag=1)
else:
print('config.person.type is not correct')
elif config['case'] == 'group':
wxid_list = me.get_person_wxid(wechat, config['group']['pattern'], flag=0)
for i in wxid_list:
if config['group']['type'] == 'text':
me.send_text(wechat, i, config['group']['text_content'])
elif config['group']['type'] == 'image':
me.send_file(wechat, i, config['group']['file_path'])
elif config['group']['type'] == 'file':
me.send_file(wechat, i, config['group']['file_path'], flag=1)
else:
print('config.group.type is not correct')
if __name__ == '__main__':
try:
t = 0
if config['times'] is not None:
while t < config['times']:
r()
time.sleep(config['interval'])
t += 1
else:
r()
except Exception as e:
print(e)
finally:
ntchat.exit_()
5、示例截图