1 第一步添加机器人,见怎么添加自定义机器人?-钉钉帮助中心
2 网上查找免费的天气url地址,url一般会要求传城市id或者城市名,可以网上查找;
3 我使用了jq插件,解析获取到的json值,网上下载,放在服务器指定目录;
#!/bin/bash
currentDate=`date "+%Y-%m-%d %H:%M:%S"`
#获取武汉市的天气
curl -o /data/weather.json 'https://api.help.bj.cn/apis/weather/?id=101200101'
weather=$(cat /data/weather.json)
#这里使用了jq工具解析 获取到的天气json文件中的具体值,可以网上下载,查询使用手法
city=$(/data/shell/jq '.city' /data/weather.json)
lowTep=$(/data/shell/jq '.temp' /data/weather.json)
#highTem=$(/data/shell/jq '.weatherinfo.temp2' /data/weather.json)
wd=$(/data/shell/jq '.wd' /data/weather.json)
weather=$(/data/shell/jq '.weather' /data/weather.json)
echo $city市的当前气温:$lowTep,风力:$wd天气情况:$weather > ./wear.txt
info=$( cat /data/shell/wear.txt )
echo $info
# 注意token值需要填写你自己添加机器人的值,可以百度如何添加预警机器人并获取地址
webhook='https://oapi.dingtalk.com/robot/send?access_token=XXXXX'
# isAtAll 设置成true 会@所有人
curl $webhook -H 'Content-Type: application/json' -d "
{
'msgtype': 'text',
'text': {
'content': '温馨提示:$info\n北京时间:$currentDate\n'
},
'at': {
'isAtAll': false
}
}"
4 效果如下: