使用腾讯云的Serverless服务,部署一个Go编写的天气变化的提醒应用 该应用通过定时查询高德地图的天气API,当当前天气或未来几天天气不好时,通过Server酱在微信中进行提醒
构建应用
应用使用 go modules开发
- go.mod
module weather
go 1.12
require github.com/tencentyun/scf-go-lib v0.0.0-20190817080819-4a2819cda320
- main.go
package main
import (
"log"
"os"
"strconv"
"time"
"fmt"
"github.com/tencentyun/scf-go-lib/cloudfunction"
"weather/tool"
)
func main() {
cloudfunction.Start(checkWeather)
}
func checkWeather() (string, error) {
// ...
}
创建函数
在腾讯云的Serverless服务中创建新的函数
添加配置
配置共三项:
添加环境变量
在函数配置点击编辑,添加环境变量
city xxxx
weatherKey xxxx
notifyKey xxxx
上传函数
本地编译打包
- Mac/Linux
GOOS=linux GOARCH=amd64 go build -o main main.go
zip main.zip main
- Win
set GOOS=linux
set GOARCH=amd64
go build -o main main.go
然后将main添加到压缩文件中
上传zip
在函数代码中上传压缩文件,保存
测试
待上传完成后,选择 HelloWorld测试模板,点击测试,等函数执行,会看到测试结果
添加触发方式
在触发方式中添加定时触发,cron表达式为 0 30 8-21 * * * *
,这样就可以在每天8点-21点的30分触发查询,如果天气状况不佳,会通过微信通知