This is a simple version of WeChat robot made using AppleScript and Mac version of WeChat client. This robot originated from the fact that I wanted to make a ChatBot to help me record consumption, diet, exercise and other data, and help me summarize the content of WeChat public account articles. Currently, the mainstream solution for implementing ChatBot using personal WeChat is Wechaty. However, among the puppets used by Wechaty, the free web version or UOS version is not stable enough (meaning it is often kicked offline inexplicably). Solutions such as ipadloacl are stable and feature-rich. You need to spend additional gold coins to purchase tokens. So after some small attempts, I implemented this WeChat robot that is slow, has few functions, but is stable enough ( I chose the Mac version of WeChat client because I only have Mac devices ).
This robot is mainly completed using AppleScript, supplemented by some Python code. Python is used because the mouse click operation that comes with AppleScript cannot be used on the WeChat client, so an additional Python library is used to implement the click operation. Therefore, you need to first have a Python3 interpreter and install PyUserInput:
pip3 install PyUserInput
Of course, if you have other solutions for simulating mouse clicks, you can also use your own (I only discovered that PyUserInput has been Deprecated when I wrote this README).
Next, you need to modify some configurations in wemac .applescript
as follows:
mouseclick.py
file is placed;After setting the above variables, you can try to run the script once. Before running, please make sure you are logged in to the Mac version of WeChat . Execute the script and you will find that AppleScript will automatically find the target chat, click on the chat window, and start deleting the most recent chat record . Yes, this AppleScript will delete the chat record. The consideration at the time was that there was no place to record each message. has been replied to, you can only avoid repeated replying by deleting the message; after deleting the message, if the message is at the robot, it will reply; then, the execution of AppleScript is over. Note that one AppleScript execution only A message will be processed .
So how to keep the robot running? A feasible solution is to write a run.sh
and write an infinite loop in it to continuously execute wemac .applescript
(in fact, I do this myself).
If you want to customize the reply content, such as connecting to a large language model, you can add your own reply logic in the "Message Processing Logic" comment at line 270 of wemac .applescript
Since AppleScript itself has limited capabilities, you can use the shell to connect other Language, such as Python, you can call a Python script to customize the reply content in the following way:
set replyMessage to do shell script "python3 your_script.py " & (my escaped(question))
The standard output (stdout) of the executed shell will be assigned to replyMessage
, and then sent by the robot as a reply.
When splicing shell commands, you can use the escaped
function to escape parameters to avoid injection attacks.
At this point, a simple chatbot is running. During the process, you may encounter some errors that cannot locate elements, but generally speaking, ignore them and re-run AppleScript.
run.sh
at the same time to handle different chats, but this will slow down the running speed;But the advantage is that it is stable enough and can basically run forever as long as you log in. In theory, using AppleScript to simulate clicks will not be detected by WeChat and lead to account bans.
The Mac WeChat client version used is Version. 3.8.6 (28078)
.