johnpoint

johnpoint

(。・∀・)ノ゙嗨
github

Set up automatic message sending on WeChat.

First of all, let's talk about the closed ecosystem of WeChat. Apart from opening the API to Sogou, a search engine that can search for WeChat public accounts, there are almost no other APIs available for use. Although this is for security considerations, it makes it difficult to implement some interesting features, such as bots. And some practical features are not implemented in the official garbage client.

Requirements#

Use the web version of WeChat on Chrome to automatically and cyclically send messages using the built-in console and JavaScript.

Construction#

First of all, I want to declare that I did not have any prior knowledge of JavaScript. Everything was achieved step by step through the effective use of search engines. So, if any experts who read this article find anything inappropriate, please feel free to point it out in the comments.

Web page source code
First, of course, log in to the web version of WeChat, and conveniently open the F12 developer tools. Try sending a few messages and observe the activity of the web page in the network tab. Then I found this:

<pre id="editArea" contenteditable-directive="" mm-paste="" class="flex edit_area ng-isolate-scope ng-pristine ng-valid" contenteditable="true" ng-blur="editAreaBlur($event)" ng-model="editAreaCtn" ng-click="editAreaClick($event)" ng-keyup="editAreaKeyup($event)" ng-keydown="editAreaKeydown($event)"></pre>

However, it didn't work...

Google search
Then, I searched with the keywords "WeChat", "send message", "Chrome", "console", and "JavaScript". Finally, I found this article->Use JavaScript script to implement WeChat timed message sending, which matched the keywords. So I opened it and found that it was exactly what I wanted.

Code modification

// Monday to Friday: 6:50 AM remind the other party to wake up, 9:30 PM remind the other party to return to the dormitory
var appElement = document.querySelector('[ng-controller=chatSenderController]');
var $scope = angular.element(appElement).scope();
setInterval(function(){
    var localTime = new Date();
    if(localTime.getDay() < 6){ // Not the weekend
        var localTimeString = localTime.toLocaleTimeString();
        if(localTimeString.indexOf('上午6:49:00') === 0){
            $scope.editAreaCtn = "It's 6:50, time to wake up!";
            $scope.sendTextMessage();
        }else if(localTimeString.indexOf('上午6:54:00') === 0){
            $scope.editAreaCtn = "It's a new day, wish you good luck!";
            $scope.sendTextMessage();
        }else if(localTimeString.indexOf('下午9:28:00') === 0){
            $scope.editAreaCtn = "It's almost 9:30, time to go back!";
            $scope.sendTextMessage();
        }
    }
},1000);

In this article, there are some additional checks before sending WeChat messages, but I don't need them. I just need to send messages without thinking (oh, and set an interval time, otherwise it will become spam). Finally, I modified it like this:

var appElement = document.querySelector('[ng-controller=chatSenderController]');
var $scope = angular.element(appElement).scope();
setInterval(function(){

$scope.editAreaCtn = "Message content";
$scope.sendTextMessage();
},1000);

Then I entered this small piece of code into the console and ran it. Oops, it was too fast. I quickly stopped it and found that Chrome was completely focused on sending messages and froze... Helpless, I had to kill the Chrome process.

Adding sleep and final result
Then I remembered that Python has a sleep function, so I immediately searched for it and found a homemade method for JavaScript sleep (JavaScript itself does not support sleep).

Finally, I modified the code again, combined it with a for loop for an infinite loop, and achieved the final result:

var appElement = document.querySelector('[ng-controller=chatSenderController]');
var $scope = angular.element(appElement).scope();
async function test() {
    $scope.editAreaCtn = "Message content";
    $scope.sendTextMessage();
}

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms))
}
i = 0
for (; ; i++) {
    test()
    await sleep(1000000)
}

So, just log in to the web version of WeChat, locate the chat window where you want to send the message, and run it!


Continued

After finishing the previous article, a problem arose. Someone started mocking me for having encrypted code. So, according to his request, I made some changes and added the functionality to randomly select an interval time between 10 and 15 minutes. The code is as follows:

var appElement = document.querySelector('[ng-controller=chatSenderController]');
var $scope = angular.element(appElement).scope();
var n = 10;
var m = 15;

// Import sleep
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}

// Send message
async function test() {
  $scope.editAreaCtn = "Message content";
  $scope.sendTextMessage();
}

// Generate a random number between n and m
function rd(n,m){
  var c = m-n+1;
  return Math.floor(Math.random() * c + n);
}

// Infinite loop
while(true){
  test();
  t = rd(n,m)
  console.log(t)
  await sleep(t * 10000);
}

End of today's article

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.