preface
In the wechat group, there are often some wechat groups to send some dispatches on a regular basis. This time we will introduce a method of using automation to achieve similar functions of wechat robot.
Implementation Function Introduction
https://news.ycombinator.com/ as a professional hacker access to information important source of information, a lot of people visit it often. The automation I taught you today is to automatically save the current content of this website to a PDF file and then send it to the wechat group to share with your friends.
The principle is roughly as follows:
Tools used
- The Puppeteer github.com/GoogleChrom…
- CukeTest cuketest.com/
Main code implementation
Open CukeTest and edit feature file according to business requirements:
# language: zh-CNAutomatically grab hacker News and save it as A PDF and share it with wechat group. Puppeteer scenario: Use puppeteer to automatically grab Hacker News and save it as a PDF if opened using puppeteer"https://news.ycombinator.com/"At the same time, save the current page content in a PDF file. @wechat Scenario: Windows desktop WeChat sending Group If you open the WeChat group, select document when opening the PDF file, click Send to send it to your friendsCopy the code
As behavior-driven scripts, feature files make scripts very readable.
Puppeteer
Puppeteer is the Google Chrome team’s official Headless Chrome tool. Puppeteer is a Node.js library that provides an advanced API to control the DevTools protocol on the Headless Chrome. This library makes it very easy to export pages as PDF documents. For details about the API, please refer to github.com/GoogleChrom…
// Main implementation code...... // Open page await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'}); // Save to PDF await page.pdf({path:'hn.pdf', format: 'A4'}); .Copy the code
It has the advantage of being faster and more feature-rich than calling Chrome through Selenium.
CukeTest
CukeTest is a tool for editing node. js automatic scripts. It has built-in libraries for operating Windows controls and operating wechat on the Windows desktop.
Usage:
Create an object model file for the wechat Windows desktop application and add the controls to the model file. See a free instructional video on how to operate Windows controls (ke.qq.com/course/3473…
Call the action object API
// Main implementation code...... Given(/^ Open wechat group, select document $/, asyncfunction () {
await model.getVirtual("Send file").click(0, 0, 1); }); When(/^ Open PDF file $/, asyncfunction () {
await model.getEdit("File name (N):1").set('hn.pdf');
await model.getGeneric("Open (O)").click(0, 0, 1); }); When(/^ Click Send, send to group friends $/, asyncfunction () {
await model.getVirtual("Send").click(0, 0, 1); }); .Copy the code
run
Click the run button to run. If you want to run the script at a scheduled time every day, you can configure it to run as a scheduled task or Jenkins Job.
conclusion
In this example, we use node.js + Cucumber framework to make code more readable, Puppeteer as an automated library to crawl content, and CukeTest editing tool, which also provides Windows automation.
If you don’t want to send a PDF, Puppeteer can also be used to periodically capture the text on the web and send it to wechat, which is more real-time.
CukeTest gives you the ability to automate your Windows, Web, API, and Mobile applications. Combined with the rich Node.js open source library, you can do all kinds of fun and interesting automation with endless imagination. If you have a good idea or are having problems with the automation process, welcome to our learning group discussion. QQ group: 707467292