I Wrote a Tool to Help Me Automatically Update My WeChat Official Account
Recently, I started updating my WeChat Official Account. However, the publishing process of the Official Account is quite complicated. Even if I already have existing blogs, it still takes a long time to publish them on the Official Account. So, I developed a tool to help me automatically update the Official Account.
I started writing blogs in 2020, and by now, I have about fifty to sixty blogs. Manually publishing each blog on the Official Account one by one takes a long time, and it’s repetitive work, so I’m not very willing to do it.
Moreover, I’ve always been writing blogs in Markdown. Even if I manually publish my existing blogs on the Official Account now, I’ll still have to update them manually in the future. Because the WeChat Official Account is a closed system, it has its own set of solutions for editing and managing articles. In the long run, this is also a hassle.
Based on these two reasons, I decided to develop a tool to help me update the Official Account, which led to this blog.
Code Address: [markdown - to - wechat](https://github.com/yexia553/markdown - to - wechat)
The following content is a necessary explanation of using this tool:
Functions
The main function is to synchronize Markdown files to the WeChat Official Account without the need to re - edit each article manually.
Install Dependencies
1 | pip3 install -r requirements.txt |
Configure Whitelist and Token
The WeChat Official Account only allows requests to relevant APIs from IPs in the whitelist. So, you need to configure the whitelist in the WeChat Official Account background.
Background path: Settings and Development -> Basic Configuration: Fill in the server IP and generate a token.
Configure Custom Variables
var.py
I created a var.py with the following content:1
2
3
4
5
6
7
8
9
10
11
12
13
14CONTENT_SOURCE_URL = "https://panzhixiang.cn" # The original article address, such as your own blog website
AUTHOR = "Pan Zhixiang" # The author's name you want to display in the Official Account article
IMAGE_PATH = "./myNotes/images" # The path of images referenced in Markdown
MARKDOWN_PATH = ["./myNotes/"] # The path of Markdown files, you can pass in multiple paths
FOOTER = '''
<div>
<br />
<br />
Synchronously published on my personal blog: <a href="https://panzhixiang.cn">https://panzhixiang.cn</a>
</div>
''' # Content added to the bottom of each article. If there is none, just keep an empty string.
# The following are the configurations of the WeChat Official Account. You can set them through environment variables or write them directly in the second quotation marks.
WECHAT_APP_SECRET = os.environ.get('WECHAT_APP_SECRET', '')
WECHAT_APP_ID = os.environ.get('WECHAT_APP_ID', '')You need to modify it according to your actual situation.
Set the Synchronization Time Range
This tool synchronizes blogs within a certain time range to the Official Account. The code is as follows:1
2
3
4# In sync.py
for x in date_range(
datetime.now() - timedelta(days=7), datetime.now() + timedelta(days=2)
):The above code will synchronize blogs from 7 days ago to 2 days later from the current time to the Official Account. You can modify it according to your needs.
It should be explained that the time used for comparison here is the value of the date attribute in the Markdown file, not the file creation or modification time. So, you need to add the date attribute in the Markdown file, for example:1
2
3
4
5
6
7---
title: markdown to wechat
date: 2020-12-12
tags:
- python
---
The following is the main text
Run
1 | python3 sync.py |
Precautions
Prevent Repeated Upload of Blogs to the Official Account
- The code determines whether a file has been processed by calculating the md5 value of the Markdown file. So, any modification to the file will cause a change in the md5 value, resulting in repeated uploads.
- After the code runs once, a file named “cache.bin” is generated. This file stores information such as the md5 values of the processed files. This is the key for the code to “remember” which blogs have been processed. If you want to move the code location, you must move this file together, otherwise, it will lead to repeated uploads.
Cover Image
It is recommended that each blog’s Markdown file should have at least one image. In this way, you can use your own image as the cover image. Otherwise, an image will be randomly fetched from https://picsum.photos as the cover image.
Finally, it should be noted that this tool was not developed from scratch. Instead, it is a secondary development based on Uncle Miao‘s [markdown - to - wechat](https://github.com/chenyukang/markdown - to - wechat). Thanks to Uncle Miao for the open - source code.