使用微信wxpy 模块进行 消息发送
代码需要使用 python 3.6 版本 高版本python 不支持 wxpy 模块
使用微信号需要可以正常登录微信网页版
将需要发送消息 以一定格式写入文件中 有程序进行发送给对应好友
代码功能
实时检测 同目录下 send.txt 文件 内容 当send.txt 文件有写入时 符合格式 message_type message_search message 发送消息给对应 好友或群组 例如
friend 好友名称 消息内容发送消息给对应好友因分割符为空格消息内容中不能带空格
或者
group 群组名称 消息内容发送消息给对应群组因分割符为空格消息内容中不能带空格
send_wxpy.py
import time
import wxpy
bot = wxpy.Bot(cache_path=True)
bot.enable_puid('wxpy_puid.pkl')
print('登录成功')
while True:
with open('send.txt', 'r', encoding='UTF-8') as f:
data = f.read()
print(data)
if not data == '':
message_type = data.split()[0]
message_search = data.split()[1]
message = data.split()[2]
print('message_type = %s' % message_type)
print('message_search = %s' % message_search)
print('message = %s' % message)
if message_type == 'group':
print('给 %s 群发送消息' % message_search)
send_groups = bot.groups().search(message_search)[0]
send_groups.send(message)
print('发送消息 成功')
elif message_type == 'friend':
print('给 %s 好友发送消息' % message_search)
send_friends = bot.friends().search(message_search)[0]
send_friends.send(message)
print('发送消息 成功')
else:
print('message_type 错误')
with open('send.txt', 'w') as f:
f.write('')
time.sleep(1)