Recently, I did a little research on the use of WeChat Cloud Hosting and simply implemented the automatic reply function for WeChat Cloud Hosting. The operation is extremely simple, and those interested can quickly create this function.
Preparation Conditions#
First, you need a normally functioning WeChat public account subscription number. It doesn't matter if it's certified or not; a normally operating public account is sufficient. Log in to WeChat Cloud Hosting, select the WeChat public account identity to log in, create an environment, and just choose the first framework. After entering the backend, prepare the service code files first.
Prepare Service Code Files#
File name Dockerfile
, the code is as follows, just copy it directly
FROM node:12-slim
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm config set registry https://mirrors.tencent.com/npm/
RUN npm install
COPY . ./
CMD ["node", "index.js"]
File name package.json
, the code is as follows, just copy it directly
{
"name": "cloudbase-push",
"version": "1.0.0",
"description": "call push server",
"main": "index.js",
"scripts": {},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.4"
}
}
The third file index.js
needs a little modification
const express = require('express')
const bodyParser = require('body-parser')
const PORT = process.env.PORT || 80
const app = express()
app.use(bodyParser.raw())
app.use(bodyParser.json({}))
app.use(bodyParser.urlencoded({ extended: true }))
app.all('/', async (req, res) => {
console.log('Message push', req.body)
const { ToUserName, FromUserName, MsgType, Content, CreateTime } = req.body
if (MsgType === 'text') {
if (Content === 'Reply text') {
res.send({
ToUserName: FromUserName,
FromUserName: ToUserName,
CreateTime: CreateTime,
MsgType: 'text',
Content: 'This is the reply message'
})
} else if (Content === 'Reply image') {
res.send({
ToUserName: FromUserName,
FromUserName: ToUserName,
CreateTime: CreateTime,
MsgType: 'image',
Image: {
// MediaID needs to be replaced
MediaId: 'h5HlJXE_4qH5MjLN-fnRu7QT5U4V1bLILEFPkliGrXRNU8vCYThZK-SgtCKoTecS'
}
})
} else if (Content === 'Reply voice') {
res.send({
ToUserName: FromUserName,
FromUserName: ToUserName,
CreateTime: CreateTime,
MsgType: 'voice',
Voice: {
// MediaID needs to be replaced
MediaId: '06JVovlqL4v3DJSQTwas1QPIS-nlBlnEFF-rdu03k0dA9a_z6hqel3SCvoYrPZzp'
}
})
} else if (Content === 'Reply video') {
res.send({
ToUserName: FromUserName,
FromUserName: ToUserName,
CreateTime: CreateTime,
MsgType: 'video',
Video: {
// MediaID needs to be replaced
MediaId: 'h5HlJXE_4qH5MjLN-fnRu5Dos4aaDNh_9yHD4s9qvWTURJt2JpT7thyTYpZeJ9Vz',
Title: 'Video Title',
Description: 'Video description content'
}
})
} else if (Content === 'Reply music') {
res.send({
ToUserName: FromUserName,
FromUserName: ToUserName,
CreateTime: CreateTime,
MsgType: 'music',
Music: {
// ThumbMediaId needs to be replaced
Title: 'Music Title',
Description: 'Daily recommendation of a good song, thank you for listening~',
MusicUrl: 'https://c.y.qq.com/base/fcgi-bin/u?__=0zVuus4U',
HQMusicUrl: 'https://c.y.qq.com/base/fcgi-bin/u?__=0zVuus4U',
ThumbMediaId: 'h5HlJXE_4qH5MjLN-fnRu7QT5U4V1bLILEFPkliGrXRNU8vCYThZK-SgtCKoTecS'
}
})
} else if (Content === 'Reply news') {
res.send({
ToUserName: FromUserName,
FromUserName: ToUserName,
CreateTime: CreateTime,
MsgType: 'news',
ArticleCount: 1,
Articles: [{
Title: 'Relax|Today’s Recommended Music',
Description: 'Daily recommendation of a good song, thank you for listening~',
PicUrl: 'https://y.qq.com/music/photo_new/T002R300x300M000004NEn9X0y2W3u_1.jpg?max_age=2592000',
Url: 'https://c.y.qq.com/base/fcgi-bin/u?__=0zVuus4U'
}]
})
} else {
res.send({
ToUserName: FromUserName,
FromUserName: ToUserName,
CreateTime: CreateTime,
MsgType: 'text',
Content: 'Received, may reply within a day~'
})
}
} else {
res.send('success')
}
})
app.listen(PORT, function () {
console.log(`Running successfully, port: ${PORT}`)
})
//"type":"video","media_id":"h5HlJXE_4qH5MjLN-fnRu5Dos4aaDNh_9yHD4s9qvWTURJt2JpT7thyTYpZeJ9Vz","created_at":1656947116
//"type":"image","media_id":"h5HlJXE_4qH5MjLN-fnRu7QT5U4V1bLILEFPkliGrXRNU8vCYThZK-SgtCKoTecS","created_at":1656946931
In this, several file IDs need to be replaced because the code in the file is from my environment; you need to modify it to the file IDs from your own environment. The method to obtain the file ID is as follows.
Obtain File ID#
First, you need to enable the upload file permission in the cloud call /cgi-bin/media/upload
, paste it in, and save it.
Then obtain the token. Here, first, open the service; you can create a service using the unmodified files, save all three files in the same folder, and choose this folder when creating to upload it. Click publish and wait for the deployment to complete!
Enter the service, open Webshell, and you will see a prompt. You need to log in to Tencent Cloud. After logging in with WeChat, close the previous Webshell page, reopen it, and you can enter the command line. Enter this
cat /.tencentcloudbase/wx/cloudbase_access_token
Right-click to paste the command, paste it, and press enter to obtain the token. When copying, do not copy the #
at the end!!
Once you have the token, you can start using WeChat's API to upload files~ I am using Postman, which I recommend; using Apifox can be a bit tricky.
Use Postman to Upload Files to Get Media ID#
Create new
to create a new HTTP request
, make sure to change the request to POST, paste
https://api.weixin.qq.com/cgi-bin/media/upload?cloudbase_access_token=CMkBEoACDv0i-32PINQVE1b7gfRc-wbAbLSyAvxY1St4x0S52TfdCFHFfbxhICgM7kKIsLlRUaMbJhRqYX7NzZ8X9CXBnNCKMjfY7pfI-M2gLKt0iMeYzvX3Ty0YvyG01nLbYfW0g4CzRh4pjSvh_sL364Hsr5qUDQq6KNEcvfN-z48MHTR4mF4gw1gQCkOgp61H9eXx5c3GrOMrElEttll33po8TQGZvH-nSjufNr3GrTcKfa15WsNqqztEAeYW4PjiYD1oKvmAf8YQ5Jl1tJ9ZQOi3kwDFxSb8yZa20PY7_XLmuhtZmbbkIRN-5nYXvqASwrRWbTJK6cLyCY4xZSQ1snhgFIAA&type=video
Change cloudbase_access_token
to the token you just obtained from the command line. The type below can be image
, video
, voice
, or thumb
, used to upload different types of files. Next, click body
, form-data
, and select the file type to upload the corresponding type of file from your local, paying attention to the file size limit. After completing, click Send.
If everything goes well, you will receive a response similar to
{"type":"video","media_id":"h5HlJXE_4qH5MjLN-fnRu5Dos4aaDNh_9yHD4s9qvWTURJt2JpT7thyTYpZeJ9Vz","created_at":1656947116,"item":[]}
Copy the media_id
from it and replace it in the above index.js
file. Make sure to replace it with the corresponding file type's file ID; do not put an image in the video ID. Re-upload the file for deployment.
Wait for the deployment to complete, then go to Cloud Hosting - Settings - Global Settings - Add Message Push, select your public account, and fill it out as shown below~
Add Global Push#
When submitting, the public account developer needs to scan the authorization code; just authorize directly, and the deployment is complete~
Open the message box of your public account in WeChat and enter the corresponding keywords like
Reply text
to get the reply content filled inindex.js
in the cloud hosting~