🎉 make some improvements
This commit is contained in:
@@ -4,11 +4,31 @@
|
||||
|
||||
## 搭建步骤
|
||||
### 服务器端配置
|
||||
1. 配置 Node.js 环境,推荐使用 nvm。
|
||||
1. 配置 Node.js 环境,推荐使用 [nvm](https://github.com/nvm-sh/nvm)。
|
||||
2. 下载代码:`git clone https://github.com/songquanpeng/wechat-message-push.git`。
|
||||
3. 安装依赖:`npm i`。
|
||||
4. 安装 pm2:`npm i -g pm2`。
|
||||
5. 使用 Nginx 反代我们的 Node.js 服务,默认端口 3000。
|
||||
1. 在 `/etc/nginx/site-enabled/` 目录下创建文件 `wechat-push-service.conf`(主配置文件 nginx.conf 内要有 `include /etc/nginx/sites-enabled/*.conf;`):
|
||||
```conf
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
listen [::]:443 ssl http2;
|
||||
server_name 你的域名;
|
||||
|
||||
location / {
|
||||
proxy_pass http://localhost:3000; # 注意如果你改变了默认端口,记得在这里进行更新
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection 'upgrade';
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Forwarded-For $remote_addr
|
||||
proxy_cache_bypass $http_upgrade;
|
||||
}
|
||||
}
|
||||
```
|
||||
2. 之后使用 [certbot](https://certbot.eff.org/lets-encrypt/ubuntuxenial-nginx) 申请证书:`sudo certbot --nginx`。
|
||||
3. 重启 Nginx 服务:`sudo service nginx restart`。
|
||||
|
||||
### 微信公众平台端配置
|
||||
1. 首先前往[此页面](https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index)拿到 APP_ID 以及 APP_SECRET。
|
||||
@@ -16,7 +36,7 @@
|
||||
3. 使用微信扫描下方的测试号二维码,拿到你的 OPEN_ID。
|
||||
4. 新增模板消息模板,模板标题随意,模板内容填 `{{text.DATA}}`,提交后可以拿到 TEMPLATE_ID。
|
||||
|
||||
### 启动服务
|
||||
### 应用内配置
|
||||
1. 使用 pm2 启动应用:`pm2 start ./app.js --name wechat-message-push-service`。
|
||||
2. 访问首页填写配置项并提交。
|
||||
2. 之后访问 `https://你的域名/push?content=Hi`,如果微信能够收到消息一条内容为 Hi 的模板消息,则配置成功。
|
||||
2. 之后访问 `https://你的域名/Hi` 或 `https://你的域名/push?content=Hi`,如果你的微信能够收到一条内容为 Hi 的模板消息,则配置成功。
|
||||
@@ -17,6 +17,7 @@ const port = parseInt(process.env.PORT || "3000");
|
||||
app.set("port", port);
|
||||
app.set("views", path.join(__dirname, "views"));
|
||||
app.set("view engine", "ejs");
|
||||
app.set("trust proxy", true);
|
||||
|
||||
app.use(logger("dev"));
|
||||
app.use(express.json());
|
||||
|
||||
@@ -4,13 +4,14 @@ const crypto = require("crypto");
|
||||
const axios = require("axios");
|
||||
const fs = require("fs");
|
||||
const requestToken = require("./utils").requestToken;
|
||||
const pushMessage = require("./utils").pushMessage;
|
||||
|
||||
router.all("/", (req, res, next) => {
|
||||
fs.promises
|
||||
.access("./.env")
|
||||
.then(() => {
|
||||
res.render("message", {
|
||||
message: "服务已在运行。",
|
||||
message: "服务已在运行,本次访问已被记录。",
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
@@ -65,28 +66,13 @@ router.all("/verify", (req, res, next) => {
|
||||
});
|
||||
|
||||
router.all("/push", (req, res, next) => {
|
||||
// Reference: https://mp.weixin.qq.com/debug/cgi-bin/readtmpl?t=tmplmsg/faq_tmpl
|
||||
let content = req.query.content || req.body.content;
|
||||
let access_token = req.app.access_token;
|
||||
let request_data = {
|
||||
touser: process.env.OPEN_ID,
|
||||
template_id: process.env.TEMPLATE_ID,
|
||||
};
|
||||
request_data.data = { text: { value: content } };
|
||||
axios
|
||||
.post(
|
||||
`https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${access_token}`,
|
||||
request_data
|
||||
)
|
||||
.then((response) => {
|
||||
if (response.data && response.data.errcode === "40001") {
|
||||
requestToken(req.app);
|
||||
}
|
||||
res.json(response.data);
|
||||
})
|
||||
.catch((error) => {
|
||||
res.json(error);
|
||||
});
|
||||
pushMessage(req, res, content);
|
||||
});
|
||||
|
||||
router.all("/:content", (req, res, next) => {
|
||||
let content = req.params.content;
|
||||
pushMessage(req, res, content);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -18,4 +18,28 @@ module.exports = {
|
||||
});
|
||||
return token;
|
||||
},
|
||||
|
||||
pushMessage: function (req, res, content) {
|
||||
// Reference: https://mp.weixin.qq.com/debug/cgi-bin/readtmpl?t=tmplmsg/faq_tmpl
|
||||
let access_token = req.app.access_token;
|
||||
let request_data = {
|
||||
touser: process.env.OPEN_ID,
|
||||
template_id: process.env.TEMPLATE_ID,
|
||||
};
|
||||
request_data.data = { text: { value: content } };
|
||||
axios
|
||||
.post(
|
||||
`https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${access_token}`,
|
||||
request_data
|
||||
)
|
||||
.then((response) => {
|
||||
if (response.data && response.data.errcode === "40001") {
|
||||
requestToken(req.app);
|
||||
}
|
||||
res.json(response.data);
|
||||
})
|
||||
.catch((error) => {
|
||||
res.json(error);
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user