Skip to content

Feat deploy #15

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 6 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"brace-style": "warn",
"space-before-function-paren": "warn",
"arrow-parens": "warn",
"operator-linebreak": "warn"
"operator-linebreak": "warn",
"comma-dangle": "off"
}
}
68 changes: 56 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,29 @@

其他非天气接口,注册会员用户每天可免费调用100次,个人使用足够了,对于天气这种特殊接口采用独立计费模式,不过价格也足够便宜,1元10000次,且申请接口后立即赠送500次。

## 开发
## 如何使用

如果你想按照本项目功能配置使用脚本,请在注册完成对应的应用后,把所需的值按规范添加到环境变量即可直接测试使用,当然你也可以自行开发定制。
**准备工作**:

### 环境
1. 安装[NodeJS](https://nodejs.org/zh-cn/)(14.x版本以上,安装最新的稳定版即可)
2. 注册企业微信,并按照步骤提供要求的变量([企业微信注册步骤](#需要的变量))
3. 注册[天行数据](https://www.tianapi.com/),获取到用户`Key`

nodejs版本推荐优先使用14.x及以上
> 如果你需要使用到 Git的功能,你还需要[安装Git](https://git-scm.com/downloads)

**3种方式**:

1. 使用本地电脑手动发送消息([点我,看这里](#本地开发))
2. 使用`GitHub Action`免费实现自动发送消息([点我,看这里](#github-action-部署))
3. 部署本地服务实现自动化运行发送消息([点我,看这里](#部署本地服务))

**需要指出的是,企业微信2022.6.20号的安全更新影响如下**:

> 如果你是在该日期之后注册的应用,那么在调用接口发送消息通知时,接口会提示IP错误,详情请查看 [企业可信ip问题讨论](https://github.com/JS-banana/notify-server/issues/9)

1. 对于本地发送消息的用户,你可以把报错的本地IP复制后填写到应用后台的可信IP内,便可以暂时解决这个问题(电脑重启后IP可能会变)
2. 对于使用`GitHub Action`的用户来说,因为 Action的运行服务器是不固定的,会经常变动,目前没发现合适的解决方案
3. 对于拥有自己服务器的同学,以及2022.6.20之前注册过应用的同学暂时不受影响

### 需要的变量

Expand Down Expand Up @@ -165,11 +181,7 @@ PS:如果出现`接口请求正常,企业微信接受消息正常,个人

</details>

### 企信IP问题

消息发送成功,但提示IP错误的问题收集,详情请查看 [企业可信ip问题讨论](https://github.com/JS-banana/notify-server/issues/9)

### 本地开发
### 本地开发(方式一)

1. 可以先star本项目给个支持,然后直接fork本项目 => 克隆至本地
2. 复制 `.env.example`文件重命名为 `.env`,并按照要求填写对应变量值
Expand Down Expand Up @@ -203,9 +215,9 @@ PS:如果出现`接口请求正常,企业微信接受消息正常,个人

> 注:本项目不作为包发布,因此暂不考虑build构建,直接通过脚本运行即可,github服务已配置有缓存,无需担心安装性能问题

### GitHub部署
### GitHub Action 部署(方式二)

如果要通过 `GitHub Action`使用,需要在 `Secrets` 中一一添加变量,脚本会自动运行,当然,你也可以根据自身需求调整,见 `.github/workflows/goodMorning.yml`
如果要通过 `GitHub Action`使用,需要在 `Secrets` 中一一添加变量,脚本会自动运行,当然,你也可以根据自身需求调整,见 [.github/workflows/goodMorning.yml](.github/workflows/goodMorning.yml)

![secrets](/images/secrets.png)

Expand All @@ -218,9 +230,41 @@ schedule:
- cron: '30 23 * * *'
```

### 部署本地服务(方式三)

使用 [pm2](https://pm2.fenxianglu.cn/) 实现对自动化脚本的本地化部署。当使用该方式后,你只需配置好[发送消息的时间](./scripts/schedule.js),然后启动服务即可😎

如果你是以下设备,请注意:

1. **本地电脑**:不关机无影响,关机后,需要开机后开启本服务
2. **服务器**:无影响,启动服务后会一直保持运行(服务器默认不关机、重启)

> 服务器部署服务的使用方式逻辑和本地开发一样,需要先clone本项目 => 安装依赖 => 执行脚本

如何启动服务:

```sh
npm run depoly:start
```

如何停止服务:

```sh
npm run depoly:stop
```

如何查看服务:

```sh
# 查看服务状态
npm run depoly:status
# 查看服务日志
npm run depoly:log
```

## 全局配置

提供 `config.yml` 配置文件,方便快速自定义调整内容参数
提供 [config.yml](config.yml) 配置文件,方便快速自定义调整内容参数

如:女朋友所在城市为`蚌埠`,则 `city_name: 蚌埠`

Expand Down
10 changes: 10 additions & 0 deletions ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// PM2 配置文件: https://juejin.cn/post/6926357629375610887

module.exports = {
apps: [
{
name: 'notify-server',
script: 'scripts/schedule.js',
},
],
}
Binary file modified images/secrets.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 13 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notify-server",
"version": "0.0.1",
"version": "1.0.0",
"description": "使用TypeScript运行和开发的nodejs技术栈的微信消息通知项目",
"keywords": [
"女朋友",
Expand All @@ -10,13 +10,13 @@
"天气",
"早安"
],
"homepage": "https://github.com/JS-banana/node-ts#readme",
"homepage": "https://github.com/JS-banana/notify-server#readme",
"bugs": {
"url": "https://github.com/JS-banana/node-ts/issues"
"url": "https://github.com/JS-banana/notify-server/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/JS-banana/node-ts.git"
"url": "git+https://github.com/JS-banana/notify-server.git"
},
"license": "GPL-3.0 license",
"author": "JS-banana <sss213018@163.com>",
Expand All @@ -41,22 +41,28 @@
"prepublishOnly": "nr build",
"release": "bumpp --commit --push --tag && pnpm publish",
"start": "esno src/index.ts",
"test": "vitest"
"depoly:start": "pm2 start ecosystem.config.js",
"depoly:stop": "pm2 stop ecosystem.config.js",
"depoly:status": "pm2 status ecosystem.config.js",
"depoly:log": "pm2 log",
"test": "echo 你好~"
},
"devDependencies": {
"@antfu/eslint-config": "^0.14.0",
"@antfu/ni": "^0.12.0",
"@types/node": "^17.0.0",
"bumpp": "^7.1.1",
"chalk": "4.1.2",
"eslint": "^8.5.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"esno": "^0.13.0",
"node-cron": "^3.0.2",
"pm2": "^5.2.0",
"prettier": "^2.5.1",
"typescript": "^4.5.4",
"unbuild": "^0.6.7",
"vite": "^2.7.3",
"vitest": "^0.0.101"
"vite": "^2.7.3"
},
"dependencies": {
"axios": "^0.24.0",
Expand Down
41 changes: 41 additions & 0 deletions scripts/schedule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { exec } = require('child_process')
const cron = require('node-cron')
const dayjs = require('dayjs')
const chalk = require('chalk') // 4.1.2以上版本不支持 CommonJS

// node-cron 定时任务模块
// # ┌────────────── second (optional) // 0-59
// # │ ┌──────────── minute // 0-59
// # │ │ ┌────────── hour // 0-23
// # │ │ │ ┌──────── day of month // 1-31
// # │ │ │ │ ┌────── month // 1-12 (or names)
// # │ │ │ │ │ ┌──── day of week // 0-7 (or names, 0 or 7 are sunday)
// # │ │ │ │ │ │
// # │ │ │ │ │ │
// # * * * * * *

// 为了方便你进行测试,你可以把时间配置成这样,默认为1分钟运行一次
// cron.schedule('* * * * *', () => {})

// 每天 7:30
cron.schedule(
'30 7 * * *',
(now) => {
console.log(
chalk.greenBright('🕒 当前时间:'),
chalk.yellowBright(dayjs(now).format('YYYY-MM-DD HH:mm:ss'))
)

console.log(chalk.cyan('🎃 开始执行发送消息脚本...'))
exec('npm run start', (err, stdout) => {
if (err) {
console.log(chalk.red('❌ 发送消息脚本执行失败'))
console.log('err: ', err)
} else {
console.log(chalk.green('✅ 发送消息脚本执行成功'))
console.log('stdout: ', stdout)
}
})
},
{ timezone: 'Asia/Shanghai' }
)