吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6837|回复: 56
收起左侧

[Python 转载] Python发送微信消息(文字、图片、文件)给指定好友和微信群(调用Win32 API模拟...

 关闭 [复制链接]
yuupuu 发表于 2021-8-20 18:15
本帖最后由 yuupuu 于 2021-9-2 15:01 编辑

本示例是调用Windows API模拟发送,用Python调用win32api这个库来调用Windows API模拟人的手动操作来发送消息。

在使用前,请将你微信的窗口设置为在最前面,这样就便于程序找到你的窗口!


微信截图_20210902145721.png

[Python] 纯文本查看 复制代码
import pyautogui
import time
import win32api
import win32con
import win32gui
import win32clipboard as w

def FindWindow(chatroom):
    win = win32gui.FindWindow(None, chatroom)
    print("找到窗口句柄:%x" % win)
    if win != 0:
        win32gui.ShowWindow(win, win32con.SW_SHOWMINIMIZED)
        win32gui.ShowWindow(win, win32con.SW_SHOWNORMAL)
        win32gui.ShowWindow(win, win32con.SW_SHOW)
        win32gui.SetWindowPos(win, win32con.HWND_TOPMOST, 0, 0, 300, 500, win32con.SWP_SHOWWINDOW)
        win32gui.SetForegroundWindow(win)  # 获取控制
        time.sleep(1)
        tit = win32gui.GetWindowText(win)
        print('已启动【'+str(tit)+'】窗口')
    else:
        print('找不到【%s】窗口' % chatroom)
        exit()

# 设置和粘贴剪贴板
def ClipboardText(ClipboardText):
        w.OpenClipboard()
        w.EmptyClipboard()
        w.SetClipboardData(win32con.CF_UNICODETEXT, ClipboardText)
        w.CloseClipboard()
        time.sleep(1)
        win32api.keybd_event(17,0,0,0)
        win32api.keybd_event(86,0,0,0)
        win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0)
        win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)

# 模拟发送动作
def SendMsg():
    win32api.keybd_event(18, 0, 0, 0)
    win32api.keybd_event(83,0,0,0)
    win32api.keybd_event(18,0,win32con.KEYEVENTF_KEYUP,0)
    win32api.keybd_event(83,0,win32con.KEYEVENTF_KEYUP,0)

# 模拟发送微信消息
def SendWxMsg(wxid,sendtext):
        # 先启动微信
        FindWindow('微信')
        time.sleep(1)
        # 定位到搜索框
        pyautogui.moveTo(143, 39)
        pyautogui.click()
        # 搜索窗口
        ClipboardText(wxid)
        time.sleep(1)
        # 进入窗口
        pyautogui.moveTo(155, 120)
        pyautogui.click()
        # 粘贴文本内容
        ClipboardText(sendtext)
        SendMsg()
        print('已发送')

# 调用函数(微信号或微信昵称或备注,需要发送的文本消息)
SendWxMsg('face6009','Python发送微信消息')


动图演示


GIF 2021-8-20 18-11-26.gif

经过学习,我在想要不要用这个方法实现一套简单的微信操作脚本,然后写成HttpApi,然后通过Http进行调用,在网页实现简单的远程控制微信,或者做个机器人等,都可以,但这个目前应该还会有不少的问题,例如延迟,精准度,这些都是一个问题,所以市面上没有人做这类型的,目前我已经实现了部分功能。

1、发送消息给好友
2、发送消息到微信群
3、邀请微信进群
4、将微信移出群聊
5、发布群公告
6、转发一篇图文


2021-09-02新增发送文件和转发群消息


[Python] 纯文本查看 复制代码
import pyautogui
import time
import win32api
import win32con
import win32gui
import win32clipboard as w

def FindWindow(chatroom):
    win = win32gui.FindWindow(None, chatroom)
    print("找到窗口句柄:%x" % win)
    if win != 0:
        win32gui.ShowWindow(win, win32con.SW_SHOWMINIMIZED)
        win32gui.ShowWindow(win, win32con.SW_SHOWNORMAL)
        win32gui.ShowWindow(win, win32con.SW_SHOW)
        win32gui.SetWindowPos(win, win32con.HWND_TOP, 0, 0, 500, 700, win32con.SWP_SHOWWINDOW)
        win32gui.SetForegroundWindow(win)  # 获取控制
        time.sleep(1)
        tit = win32gui.GetWindowText(win)
        print('已启动【'+str(tit)+'】窗口')
    else:
        print('找不到【%s】窗口' % chatroom)
        exit()

# 设置和粘贴剪贴板
def ClipboardText(ClipboardText):
	w.OpenClipboard()
	w.EmptyClipboard()
	w.SetClipboardData(win32con.CF_UNICODETEXT, ClipboardText)
	w.CloseClipboard()
	time.sleep(1)
	win32api.keybd_event(17,0,0,0)
	win32api.keybd_event(86,0,0,0)
	win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0)
	win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)

# 模拟发送动作
def SendMsg():
    win32api.keybd_event(18, 0, 0, 0)
    win32api.keybd_event(83,0,0,0)
    win32api.keybd_event(18,0,win32con.KEYEVENTF_KEYUP,0)
    win32api.keybd_event(83,0,win32con.KEYEVENTF_KEYUP,0)

# 模拟发送微信文本消息
def SendWxMsg(wxid,sendtext):
	# 先启动微信
	FindWindow('微信')
	time.sleep(1)
	# 定位到搜索框
	pyautogui.moveTo(143, 39)
	pyautogui.click()
	# 搜索微信
	ClipboardText(wxid)
	time.sleep(1)
	# 进入聊天窗口
	pyautogui.moveTo(155, 120)
	pyautogui.click()
	# 粘贴文本内容
	ClipboardText(sendtext)
	# 发送
	SendMsg()
	print('已发送')
	# 关闭微信窗口
	time.sleep(1)
	pyautogui.moveTo(683, 16)
	pyautogui.click()

# 模拟发送文件消息(图片、文档、压缩包等)
def SendWxFileMsg(wxid,imgpath):
	# 先启动微信
	FindWindow('微信')
	time.sleep(1)
	# 定位到搜索框
	pyautogui.moveTo(143, 39)
	pyautogui.click()
	# 搜索微信
	ClipboardText(wxid)
	time.sleep(1)
	# 进入聊天窗口
	pyautogui.moveTo(155, 120)
	pyautogui.click()
	# 选择文件
	pyautogui.moveTo(373, 570)
	pyautogui.click()
	ClipboardText(imgpath)
	time.sleep(1)
	pyautogui.moveTo(784, 509)
	pyautogui.click()
	# 发送
	SendMsg()
	print('已发送')
	# 关闭微信窗口
	time.sleep(1)
	pyautogui.moveTo(683, 16)
	pyautogui.click()

# 转发群里最新的一条消息
def ZhuanfaMsg(wxid,groupname):
	# 先启动微信
	FindWindow('微信')
	time.sleep(1)
	# 定位到搜索框
	pyautogui.moveTo(143, 39)
	pyautogui.click()
	# 搜索群
	ClipboardText(groupname)
	time.sleep(1)
	# 进入群窗口
	pyautogui.moveTo(155, 120)
	pyautogui.click()
	# 开始转发
	pyautogui.moveTo(484, 439)
	time.sleep(1)
	pyautogui.rightClick()
	pyautogui.moveTo(543, 454)
	time.sleep(1)
	pyautogui.click()
	# 搜索用户
	ClipboardText(wxid)
	time.sleep(1)
	pyautogui.moveTo(828, 406)
	pyautogui.click()
	time.sleep(1)
	# 确定转发
	pyautogui.moveTo(1108, 755)
	pyautogui.click()


# 发送文本消息(微信号或微信昵称或备注,需要发送的文本消息)
SendWxMsg('sansure2016','Python发送微信消息')

# 发送文件消息(图片、文档、压缩包等)
SendWxFileMsg('sansure2016',r"C:\Users\TANKING\Desktop\cbzqx77.jpg")

# 转发群里最新的一条消息(微信号或微信昵称或备注,群名称)
ZhuanfaMsg('sansure2016','里客云科技')

免费评分

参与人数 18吾爱币 +15 热心值 +11 收起 理由
雾雨 + 1 + 1 用心讨论,共获提升!
Tauruslsj + 1 我很赞同!
ahuangma + 1 + 1 我很赞同!
脚大怎么了 + 1 + 1 共同学习~
林亖 + 1 谢谢@Thanks!
306966576 + 1 + 1 我很赞同!
lanwd + 1 热心回复!
passtoworld + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
alidev + 1 + 1 用心讨论,共获提升!
change888 + 1 占位置,等出品。
Atozye + 1 + 1 用心讨论,共获提升!
sgod + 1 用心讨论,共获提升!
tata190711 + 1 + 1 热心回复!
闪的好快啊 + 1 用心讨论,共获提升!
beyond1994 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
景寒杏 + 1 我很赞同!
muzb + 1 鼓励转贴优秀软件安全工具和文档!
Idealismbeiming + 1 + 1 鼓励转贴优秀软件安全工具和文档!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Bds1r 发表于 2021-8-20 18:33
如果是机器人的话可能还是需要hook call来进行一系列关键操作
 楼主| yuupuu 发表于 2021-8-20 23:38

可以打包:https://www.cnblogs.com/maoxianfei/p/6005365.html
Wapj_Wolf 发表于 2021-8-20 18:51
你们的小宇哥 发表于 2021-8-20 18:52
所以能转成exe的吗。
Idealismbeiming 发表于 2021-8-20 18:57
这个真是好东西,学习了,这样以后就可以减少一些重复的劳作
头像被屏蔽
Sashka 发表于 2021-8-20 19:38
提示: 作者被禁止或删除 内容自动屏蔽
头像被屏蔽
Sashka 发表于 2021-8-20 19:39
提示: 作者被禁止或删除 内容自动屏蔽
ytlk0535 发表于 2021-8-20 19:40
感谢楼主分享,学习了
头像被屏蔽
Sashka 发表于 2021-8-20 19:40
提示: 作者被禁止或删除 内容自动屏蔽
haliluyadada 发表于 2021-8-20 20:03
这个很实用
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-16 22:57

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表