当前位置:   article > 正文

python:UI自动化实现微信自动回复脚本_uiautomation 微信自动回复

uiautomation 微信自动回复

环境:windows11,微信3.9.10,python12

安装模块:pip install uiautomation pyautogui

具体窗口定位可以使用inspect.exe查看,一般位于C:\Program Files (x86)\Windows Kits\10\bin下的文件夹,选择x86版本的就行

  1. import uiautomation as auto
  2. import os
  3. from time import sleep
  4. import pyautogui
  5. class WechatAuto:
  6. def __init__(self, wechat_locate: str):
  7. """
  8. 启动微信,定位基本元素
  9. :param wechat_locate: 微信安装绝对路径.exe
  10. """
  11. os.system(rf'start {wechat_locate}')
  12. self.wechat_window = auto.WindowControl(searchDepth=1, ClassName='WeChatMainWndForPC')
  13. # 窗口控制栏,固定,最大小化,退出
  14. self.window_control = self.wechat_window.ToolBarControl(searchDepth=5, LocalizedControlType="工具栏")
  15. # 用户导航:用户,聊天,通讯录,收藏……
  16. self.guide_tool = self.wechat_window.ToolBarControl(searchDepth=4, Name='导航')
  17. # 会话列表
  18. self.chat_list = self.wechat_window.ListControl(searchDepth=8, Name='会话')
  19. # 当前聊天对象消息列表窗口,聊天内容
  20. self.chat_massage = self.wechat_window.ListControl(searchDepth=13, Name='消息')
  21. # 聊天界面搜索框
  22. self.chat_search = self.wechat_window.EditControl(searchDepth=7, Name='搜索')
  23. # 先清除搜索框再输入内容
  24. self.chat_search_clear = self.wechat_window.ButtonControl(searchDepth=7, Name='清空')
  25. # 搜索结果
  26. self.search_result = self.wechat_window.ListControl(searchDepth=8, Name="@str:IDS_FAV_SEARCH_RESULT:3780",
  27. LocalizedControlType="列表")
  28. # 发送按钮
  29. self.send_button = self.wechat_window.ButtonControl(searchDepth=15, Name="发送(S)")
  30. self.chat_name: str = ""
  31. def input_content(self, name: str):
  32. return self.wechat_window.EditControl(searchDepth=14, LocalizedControlType="编辑", Name=name)
  33. def searchFriend(self, name: str, where: str = "聊天") -> list:
  34. """
  35. 从聊天列表查找聊天对象
  36. :param name: 聊天对象名称
  37. :param where: 查找位置=聊天/通讯录
  38. :return:
  39. """
  40. where_index: int = 0
  41. match where:
  42. case "聊天":
  43. where_index = 1
  44. case "通讯录":
  45. where_index = 2
  46. case _:
  47. print("invalid value")
  48. return [0, 0]
  49. # 定位聊天列表
  50. try:
  51. chat_list = self.guide_tool.GetChildren()[where_index]
  52. assert chat_list.Name, where
  53. chat_list.Click()
  54. except AssertionError:
  55. print("can not locate the contacts element,check with the Wechat version and try again,or the wrong "
  56. "name about your friend")
  57. return [0, 0]
  58. except Exception as err:
  59. print(err)
  60. return [0, 0]
  61. # 定位搜索框
  62. try:
  63. check = self.chat_search.Name
  64. assert check, "搜索"
  65. except AssertionError:
  66. print("can not find the search element,check with the Wechat version and try again")
  67. return [0, 0]
  68. except Exception as err:
  69. print(err)
  70. return [0, 0]
  71. # 从聊天列表中查找聊天对象
  72. self.chat_search.Click()
  73. self.chat_search_clear.Click()
  74. self.chat_search.Click()
  75. self.chat_search.SendKeys(text=name, waitTime=0.2)
  76. try:
  77. assert self.search_result.GetChildren()[1], name
  78. except AssertionError:
  79. print("can not find the element in chet list,search in from contact")
  80. return [0, 0]
  81. except Exception as err:
  82. print(err)
  83. return [0, 0]
  84. return [1, self.search_result.GetChildren()[1]]
  85. def chatWithSomeone(self, name: str, massage: str) -> int:
  86. """
  87. 查找聊天对象,定位到聊天输入框
  88. :param name: 聊天对象名称
  89. :param massage: 聊天发送信息
  90. :return: 成功返回1
  91. """
  92. if name != self.chat_name:
  93. chat_with = self.searchFriend(name)
  94. if chat_with[0] == 0:
  95. chat_with = self.searchFriend(name, where="通讯录")
  96. if chat_with[0] == 0:
  97. print(f"can not find your friend name {name}")
  98. return 0
  99. chat_with[1].Click()
  100. self.chat_name = name
  101. content = self.input_content(name)
  102. content.Click()
  103. content.SendKeys(text=massage, waitTime=0.2)
  104. self.send_button.Click()
  105. return 1
  106. def autoAddFriend(self, auto_add: bool = False):
  107. """
  108. 开启自动添加好友申请
  109. :param auto_add: 是True,否False
  110. :return: str,添加的好友名称
  111. """
  112. pass
  113. def autoChatBaseOnContent(self, question_answer: dict[str, str]):
  114. """
  115. 根据内容回循环复所有聊天
  116. :param question_answer: 聊天内容:回复内容,如果朋友向我发送的内容包含在【聊天内容】中,则使用【回复内容】进行回复
  117. :return: None
  118. """
  119. friend_question: dict[str, str] = {}
  120. for chat in self.chat_list.GetChildren():
  121. chat_name = chat.TextControl(searchDepth=5, foundIndex=1).Name
  122. chat_last_content = chat.TextControl(searchDepth=5, foundIndex=3).Name
  123. friend_question[chat_name] = chat_last_content
  124. qus = question_answer.keys()
  125. for friend in friend_question:
  126. # 这里是包含关系,也可以使用【jieba】模块进行词语解析
  127. my_answer = [question_answer[j] for j in qus if j in friend_question[friend]]
  128. for answer in my_answer:
  129. if answer != friend_question[friend]:
  130. self.chatWithSomeone(name=friend, massage=answer)
  131. friend_question[friend] = answer
  132. @staticmethod
  133. def setFileToClipBoard(file_path: str) -> int:
  134. """
  135. ui自动化,将文件复制到剪贴板中
  136. :param file_path: 文件绝对地址
  137. :return: 复制城成功返回1
  138. """
  139. if not os.path.exists(file_path):
  140. return 0
  141. father_path = os.path.abspath(os.path.join(file_path, os.pardir))
  142. file_name = file_path.split("\\")[-1]
  143. window_name = father_path.split("\\")[-1]
  144. try:
  145. os.system(rf"start explorer {father_path}")
  146. explorer = auto.WindowControl(searchDepth=1, ClassName="CabinetWClass", Name=window_name)
  147. file_locate = explorer.ListItemControl(searchDepth=8, Name=file_name, LocalizedControlType="项目列表")
  148. sleep(1)
  149. file_locate.Click(simulateMove=True, waitTime=0.2)
  150. close = auto.ButtonControl(searchDepth=4, Name="关闭")
  151. pyautogui.hotkey('ctrl', 'c')
  152. close.Click()
  153. except Exception as err:
  154. print(err)
  155. return 0
  156. return 1
  157. def sendFileToSomeone(self, name: str, file_path: str) -> int:
  158. """
  159. 发送文件给某人
  160. :param name: 发送对象
  161. :param file_path: 文件路径
  162. :return:
  163. """
  164. check_friend = self.searchFriend(name)
  165. if check_friend[0] == 0:
  166. print(f"can not find your friend name {name}")
  167. return 0
  168. check_file = self.setFileToClipBoard(file_path)
  169. if check_file == 0:
  170. print(f"on such file {file_path}")
  171. return 0
  172. check_friend[1].Click()
  173. content = self.input_content(name)
  174. content.Click()
  175. pyautogui.hotkey('ctrl', 'v')
  176. self.send_button.Click()
  177. return 1
  178. if __name__ == '__main__':
  179. wechat = WechatAuto(r"C:\AppInstall\WeChat\WeChat.exe")
  180. wechat.chatWithSomeone("文件传输助手", "112223345")

注:自动回复有可能会被微信判定为违规,有封号风险,谨慎使用!

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

闽ICP备14008679号