当前位置:   article > 正文

python 微信发送消息_python发送微信消息

python发送微信消息

目录

pyautogui 模拟点击发消息

微信wxauto 不错

可执行代码:

WeRoBot 还没测

pyautogui 免费不错


pyautogui 模拟点击发消息

Python发送微信消息(文字、图片、文件)给指定好友和微信群,零基础可看懂(附源码和教程)_python 本地文件发送到微信群-CSDN博客

微信wxauto 不错

GitHub - cluic/wxauto: Windows版本微信客户端(非网页版)自动化,可实现简单的发送、接收微信消息,简单微信机器人

可执行代码:

打开pc上的微信并登录,这个代码就可以自动和指定联系人发消息并获取聊天记录

  1. from wxauto import WeChat
  2. wx = WeChat()
  3. # 发送消息
  4. who = '辰辰'
  5. for i in range(3):
  6. wx.SendMsg(f'wxauto测试{i+1}', who)
  7. # 获取当前聊天页面(文件传输助手)消息,并自动保存聊天图片
  8. msgs = wx.GetAllMessage(savepic=True)
  9. for msg in msgs:
  10. print(f"{msg[0]}: {msg[1]}")
  11. print('wxauto测试完成!')

【Python微信机器人】第六篇:优化使用方式,可pip安装_pip install wechathelper-CSDN博客

WeRoBot 还没测

探索WeRoBot:一个强大的微信机器人框架-CSDN博客

pyautogui 免费不错

参考博文

  1. import pyautogui
  2. import time
  3. import win32api
  4. import win32con
  5. import win32gui
  6. import win32clipboard as w
  7. from PIL import ImageGrab
  8. import requests
  9. import base64
  10. def FindWindow(chatroom):
  11. win = win32gui.FindWindow('WeChatMainWndForPC', chatroom)
  12. print("找到窗口句柄:%x" % win)
  13. if win != 0:
  14. win32gui.ShowWindow(win, win32con.SW_SHOWMINIMIZED)
  15. win32gui.ShowWindow(win, win32con.SW_SHOWNORMAL)
  16. win32gui.ShowWindow(win, win32con.SW_SHOW)
  17. win32gui.SetWindowPos(win, win32con.HWND_TOP, 0, 0, 500, 700, win32con.SWP_SHOWWINDOW)
  18. win32gui.SetForegroundWindow(win) # 获取控制
  19. time.sleep(1)
  20. tit = win32gui.GetWindowText(win)
  21. print('已启动【' + str(tit) + '】窗口')
  22. else:
  23. print('找不到【%s】窗口' % chatroom)
  24. exit()
  25. # 设置和粘贴剪贴板
  26. def ClipboardText(ClipboardText):
  27. w.OpenClipboard()
  28. w.EmptyClipboard()
  29. w.SetClipboardData(win32con.CF_UNICODETEXT, ClipboardText)
  30. w.CloseClipboard()
  31. time.sleep(1)
  32. win32api.keybd_event(17, 0, 0, 0)
  33. win32api.keybd_event(86, 0, 0, 0)
  34. win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
  35. win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
  36. # 模拟发送动作
  37. def SendMsg():
  38. win32api.keybd_event(18, 0, 0, 0)
  39. win32api.keybd_event(83, 0, 0, 0)
  40. win32api.keybd_event(18, 0, win32con.KEYEVENTF_KEYUP, 0)
  41. win32api.keybd_event(83, 0, win32con.KEYEVENTF_KEYUP, 0)
  42. # 模拟发送微信文本消息
  43. def SendWxMsg(wxid, sendtext):
  44. # 先启动微信
  45. FindWindow('微信')
  46. time.sleep(1)
  47. # 定位到搜索框
  48. pyautogui.moveTo(143, 39)
  49. pyautogui.click()
  50. # 搜索微信
  51. ClipboardText(wxid)
  52. time.sleep(1)
  53. # 进入聊天窗口
  54. pyautogui.moveTo(155, 120)
  55. pyautogui.click()
  56. # 粘贴文本内容
  57. ClipboardText(sendtext)
  58. # 发送
  59. SendMsg()
  60. print('已发送')
  61. # 关闭微信窗口
  62. time.sleep(1)
  63. pyautogui.moveTo(683, 16)
  64. pyautogui.click()
  65. # 模拟发送文件消息(图片、文档、压缩包等)
  66. def SendWxFileMsg(wxid, imgpath):
  67. # 先启动微信
  68. FindWindow('微信')
  69. time.sleep(1)
  70. # 定位到搜索框
  71. pyautogui.moveTo(143, 39)
  72. pyautogui.click()
  73. # 搜索微信
  74. ClipboardText(wxid)
  75. time.sleep(1)
  76. # 进入聊天窗口
  77. pyautogui.moveTo(155, 120)
  78. pyautogui.click()
  79. # 选择文件
  80. pyautogui.moveTo(373, 570)
  81. pyautogui.click()
  82. ClipboardText(imgpath)
  83. time.sleep(1)
  84. pyautogui.moveTo(784, 509)
  85. pyautogui.click()
  86. # 发送
  87. SendMsg()
  88. print('已发送')
  89. # 关闭微信窗口
  90. time.sleep(1)
  91. pyautogui.moveTo(683, 16)
  92. pyautogui.click()
  93. # 转发群里最新的一条消息
  94. def ZhuanfaMsg(wxid, groupname):
  95. # 先启动微信
  96. FindWindow('微信')
  97. time.sleep(1)
  98. # 定位到搜索框
  99. pyautogui.moveTo(143, 39)
  100. pyautogui.click()
  101. # 搜索群
  102. ClipboardText(groupname)
  103. time.sleep(1)
  104. # 进入群窗口
  105. pyautogui.moveTo(155, 120)
  106. pyautogui.click()
  107. # 开始转发
  108. pyautogui.moveTo(484, 439)
  109. time.sleep(1)
  110. pyautogui.rightClick()
  111. pyautogui.moveTo(543, 454)
  112. time.sleep(1)
  113. pyautogui.click()
  114. # 搜索用户
  115. ClipboardText(wxid)
  116. time.sleep(1)
  117. pyautogui.moveTo(828, 406)
  118. pyautogui.click()
  119. time.sleep(1)
  120. # 确定转发
  121. pyautogui.moveTo(1108, 755)
  122. pyautogui.click()
  123. # 获取你的个人信息(昵称、微信号)
  124. def GetYourInfo():
  125. # 先启动微信
  126. FindWindow('微信')
  127. time.sleep(1)
  128. # 点击你的头像
  129. pyautogui.moveTo(28, 56)
  130. pyautogui.click()
  131. time.sleep(1)
  132. # 用户信息截图
  133. userinfo = (20, 60, 319, 284)
  134. userinfo_img = ImageGrab.grab(userinfo)
  135. userinfo_img.save('userinfo.png')
  136. # 识别用户信息截图
  137. request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
  138. f = open('userinfo.png', 'rb')
  139. img = base64.b64encode(f.read())
  140. params = {"image": img}
  141. access_token = '24.21008e8b243c5b2864a7cf0583d907e1.2592000.1661322017.282335-24796078'
  142. request_url = request_url + "?access_token=" + access_token
  143. headers = {'content-type': 'application/x-www-form-urlencoded'}
  144. response = requests.post(request_url, data=params, headers=headers)
  145. if response:
  146. print(response.json())
  147. time.sleep(1)
  148. # 关闭微信窗口
  149. time.sleep(1)
  150. pyautogui.moveTo(683, 16)
  151. pyautogui.click()
  152. # 获取好友微信的个人信息(昵称、微信号)
  153. def GetFriendInfo(wxid):
  154. # 先启动微信
  155. FindWindow('微信')
  156. time.sleep(1)
  157. # 定位到搜索框
  158. pyautogui.moveTo(143, 39)
  159. pyautogui.click()
  160. # 搜索微信
  161. ClipboardText(wxid)
  162. time.sleep(1)
  163. # 进入聊天窗口
  164. pyautogui.moveTo(160, 93)
  165. pyautogui.click()
  166. time.sleep(1)
  167. # 点击右上角···
  168. pyautogui.moveTo(678, 43)
  169. pyautogui.click()
  170. time.sleep(1)
  171. pyautogui.moveTo(801, 51)
  172. pyautogui.click()
  173. # 用户信息截图
  174. userinfo = (802, 54, 1085, 331)
  175. userinfo_img = ImageGrab.grab(userinfo)
  176. userinfo_img.save('userinfo.png')
  177. # 识别用户信息截图
  178. request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
  179. f = open('userinfo.png', 'rb')
  180. img = base64.b64encode(f.read())
  181. params = {"image": img}
  182. access_token = '24.21008e8b243c5b2864a7cf0583d907e1.2592000.1661322017.282335-24796078'
  183. request_url = request_url + "?access_token=" + access_token
  184. headers = {'content-type': 'application/x-www-form-urlencoded'}
  185. response = requests.post(request_url, data=params, headers=headers)
  186. if response:
  187. print(response.json())
  188. time.sleep(1)
  189. # 关闭微信窗口
  190. time.sleep(1)
  191. pyautogui.moveTo(683, 16)
  192. pyautogui.click()
  193. pyautogui.click()
  194. # 获取群人数
  195. def GetCharRoomUserNum(groupname):
  196. # 先启动微信
  197. FindWindow('微信')
  198. time.sleep(1)
  199. # 定位到搜索框
  200. pyautogui.moveTo(143, 39)
  201. pyautogui.click()
  202. # 搜索群
  203. ClipboardText(groupname)
  204. time.sleep(1)
  205. # 进入群窗口
  206. pyautogui.moveTo(155, 120)
  207. pyautogui.click()
  208. # 群人数区域截图
  209. userinfo = (310, 0, 659, 47)
  210. userinfo_img = ImageGrab.grab(userinfo)
  211. userinfo_img.save('chatroom.png')
  212. # 识别群人数截图
  213. # 开发文档:[url=https://cloud.baidu.com/doc/OCR/s/Ck3h7y2ia]https://cloud.baidu.com/doc/OCR/s/Ck3h7y2ia[/url]
  214. request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
  215. f = open('chatroom.png', 'rb')
  216. img = base64.b64encode(f.read())
  217. params = {"image": img}
  218. access_token = '24.21008e8b243c5b2864a7cf0583d907e1.2592000.1661322017.282335-24796078'
  219. request_url = request_url + "?access_token=" + access_token
  220. headers = {'content-type': 'application/x-www-form-urlencoded'}
  221. response = requests.post(request_url, data=params, headers=headers)
  222. if response:
  223. print(response.json())
  224. time.sleep(1)
  225. # 关闭微信窗口
  226. time.sleep(1)
  227. pyautogui.moveTo(683, 16)
  228. pyautogui.click()
  229. # 发布群公告
  230. def AddGorupNotice(groupname, NoticeText):
  231. # 先启动微信
  232. FindWindow('微信')
  233. time.sleep(1)
  234. # 定位到搜索框
  235. pyautogui.moveTo(143, 39)
  236. pyautogui.click()
  237. # 搜索群
  238. ClipboardText(groupname)
  239. time.sleep(1)
  240. # 进入群窗口
  241. pyautogui.moveTo(155, 120)
  242. pyautogui.click()
  243. # 定位到群名称
  244. pyautogui.moveTo(363, 33)
  245. pyautogui.click()
  246. # 定位到群公告
  247. time.sleep(1)
  248. pyautogui.moveTo(731, 509)
  249. pyautogui.click()
  250. # 粘贴群公告内容
  251. ClipboardText(NoticeText)
  252. # 确认发布群公告
  253. time.sleep(1)
  254. pyautogui.moveTo(288, 500)
  255. pyautogui.click()
  256. pyautogui.moveTo(312, 297)
  257. pyautogui.click()
  258. # 邀请好友进群
  259. def ReqFriendsToGroup(groupname, wxid):
  260. # 先启动微信
  261. FindWindow('微信')
  262. time.sleep(1)
  263. # 定位到搜索框
  264. pyautogui.moveTo(143, 39)
  265. pyautogui.click()
  266. # 搜索群
  267. ClipboardText(groupname)
  268. time.sleep(1)
  269. # 进入群窗口
  270. pyautogui.moveTo(155, 120)
  271. pyautogui.click()
  272. # 定位到群名称
  273. pyautogui.moveTo(363, 33)
  274. pyautogui.click()
  275. # 定位到邀请
  276. time.sleep(1)
  277. pyautogui.moveTo(852, 300)
  278. pyautogui.click()
  279. # 搜索好友
  280. ClipboardText(wxid)
  281. # 发出邀请
  282. time.sleep(1)
  283. pyautogui.moveTo(232, 197)
  284. pyautogui.click()
  285. time.sleep(1)
  286. pyautogui.moveTo(493, 561)
  287. pyautogui.click()
  288. time.sleep(1)
  289. pyautogui.moveTo(395, 427)
  290. pyautogui.click()
  291. # 获取好友最新的聊天记录
  292. def GetChatRecord(wxid):
  293. # 先启动微信
  294. FindWindow('微信')
  295. time.sleep(1)
  296. # 搜索微信号
  297. pyautogui.moveTo(166, 38)
  298. pyautogui.click()
  299. ClipboardText(wxid)
  300. time.sleep(1)
  301. pyautogui.moveTo(197, 123)
  302. pyautogui.click()
  303. # 聊天内容区域截图
  304. userinfo = (314, 68, 683, 549)
  305. userinfo_img = ImageGrab.grab(userinfo)
  306. userinfo_img.save('chatrecord.png')
  307. # 识别当前聊天窗口截图
  308. # 开发文档:[url=https://cloud.baidu.com/doc/OCR/s/Ck3h7y2ia]https://cloud.baidu.com/doc/OCR/s/Ck3h7y2ia[/url]
  309. request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
  310. f = open('chatrecord.png', 'rb')
  311. img = base64.b64encode(f.read())
  312. params = {"image": img}
  313. access_token = '24.21008e8b243c5b2864a7cf0583d907e1.2592000.1661322017.282335-24796078'
  314. request_url = request_url + "?access_token=" + access_token
  315. headers = {'content-type': 'application/x-www-form-urlencoded'}
  316. response = requests.post(request_url, data=params, headers=headers)
  317. if response:
  318. print(response.json())
  319. # 置顶群或好友
  320. def SetTop(groupname_wxid):
  321. # 先启动微信
  322. FindWindow('微信')
  323. time.sleep(1)
  324. # 定位到搜索框
  325. pyautogui.moveTo(143, 39)
  326. pyautogui.click()
  327. # 搜索群或好友
  328. ClipboardText(groupname_wxid)
  329. time.sleep(1)
  330. # 进入窗口
  331. pyautogui.moveTo(155, 120)
  332. pyautogui.click()
  333. # 打开设置
  334. pyautogui.moveTo(684, 38)
  335. pyautogui.click()
  336. time.sleep(1)
  337. # 置顶
  338. pyautogui.moveTo(914, 227)
  339. pyautogui.click()
  340. time.sleep(1)
  341. pyautogui.moveTo(682, 18)
  342. pyautogui.click()
  343. print('已将' + str(groupname_wxid) + '置顶')
  344. # 添加微信
  345. def AddWx(wxid):
  346. # 先启动微信
  347. FindWindow('微信')
  348. time.sleep(1)
  349. # 定位到添加微信位置
  350. pyautogui.moveTo(25, 151)
  351. pyautogui.click()
  352. time.sleep(1)
  353. pyautogui.moveTo(278, 39)
  354. pyautogui.click()
  355. time.sleep(1)
  356. pyautogui.moveTo(153, 39)
  357. pyautogui.click()
  358. time.sleep(1)
  359. # 搜索微信号
  360. ClipboardText(wxid)
  361. time.sleep(1)
  362. pyautogui.moveTo(183, 91)
  363. pyautogui.click()
  364. time.sleep(2)
  365. # 对搜索微信号结果进行截图
  366. userinfo = (306, 68, 565, 240)
  367. userinfo_img = ImageGrab.grab(userinfo)
  368. userinfo_img.save('addwx.png')
  369. # 对搜索微信号结果进行识别
  370. # 开发文档:[url=https://cloud.baidu.com/doc/OCR/s/Ck3h7y2ia]https://cloud.baidu.com/doc/OCR/s/Ck3h7y2ia[/url]
  371. request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
  372. f = open('addwx.png', 'rb')
  373. img = base64.b64encode(f.read())
  374. params = {"image": img}
  375. access_token = '24.21008e8b243c5b2864a7cf0583d907e1.2592000.1661322017.282335-24796078'
  376. request_url = request_url + "?access_token=" + access_token
  377. headers = {'content-type': 'application/x-www-form-urlencoded'}
  378. response = requests.post(request_url, data=params, headers=headers)
  379. if response:
  380. print(response.json())
  381. # 添加
  382. time.sleep(1)
  383. pyautogui.moveTo(435, 203)
  384. pyautogui.click()
  385. time.sleep(1)
  386. pyautogui.moveTo(300, 621)
  387. pyautogui.click()
  388. time.sleep(1)
  389. pyautogui.moveTo(278, 37)
  390. pyautogui.click()
  391. print('已向' + str(wxid) + '发送添加好友请求')
  392. pyautogui.moveTo(682, 18)
  393. pyautogui.click()
  394. # 发送文本消息(微信号或微信昵称或备注,需要发送的文本消息)
  395. SendWxMsg('文件传输助手','Python发送微信消息')
  396. # 发送文件消息(图片、文档、压缩包等)
  397. # SendWxFileMsg('微信号',r"文件路径")
  398. # 转发群里最新的一条消息(微信号或微信昵称或备注,群名称)
  399. # ZhuanfaMsg('微信号','群名称')
  400. # 获取你的个人信息(昵称、微信号)
  401. # GetYourInfo('微信号')
  402. # 获取好友微信的个人信息(昵称、微信号)
  403. # GetFriendInfo('微信号')
  404. # 获取微信群人数
  405. # GetCharRoomUserNum('群名称')
  406. # 发布群公告
  407. # AddGorupNotice('群名称','Python发布群公告')
  408. # 邀请好友进群
  409. # ReqFriendsToGroup('群名称','cbzqx88')
  410. # 获取好友最新的聊天记录
  411. # GetChatRecord('微信号')
  412. # 置顶群或好友
  413. # SetTop('微信号')
  414. # 添加微信
  415. # AddWx('微信号')

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/901356
推荐阅读
相关标签
  

闽ICP备14008679号