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 running WeChat public account subscription number, whether it is certified or not doesn't matter, as long as it is a normally running public account. 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 to modify a few things
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 introduction 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 in your own environment. The method to obtain the file ID is as follows.
Get 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 get the token, here you can first open the service, you can use the unmodified file to create a service, save the three files in the same folder, and select this folder when creating to upload. Click publish and wait for the deployment to complete!
Enter the service, open Webshell, at this time you will see a prompt, you need to log in to Tencent Cloud, log in with WeChat scan code, then close the previous Webshell webpage, reopen it to enter the command line, enter this
cat /.tencentcloudbase/wx/cloudbase_access_token
Right-click to paste the command, paste it and press Enter to get the token, be careful not to copy the #
at the end when copying!!
Once you get 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 MediaID#
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, and below type, the optional values are image
, video
, voice
, thumb
, used to upload different types of files. Next, click body
, form-data
, select file type as file, and upload the corresponding type of file from your local. Note the file size limit, and after completion, 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, be careful to replace it with the corresponding file type's file ID, do not put the image in the video ID, re-upload the file and deploy.
Wait for the deployment to complete, then go to Cloud Hosting - Settings - Global Settings - Add Message Push, select your public account, fill in as shown below~
Add Global Push#
When submitting, the public account developer needs to scan the code for authorization, just authorize directly, and the deployment is complete~
Open your public account message box in WeChat and enter the corresponding keywords like
Reply text
to get the reply content filled inindex.js
in the cloud hosting~