CURL 钉钉机器人 JSON 传参

在日常工作中,经常需要定时执行一些任务。当前我们用的是通过钉钉机器人通知任务完成情况。使用钉钉机器人通知非常简单,通过 curl 命令行工具即可发送通知。

curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx' \
   -H 'Content-Type: application/json' \
   -d '
  {"msgtype": "text", 
    "text": {
        "content": "我就是我, 是不一样的烟火"
     }
  }'

发送的通知内容中,通常又需要通过传入参数,以便知道是哪个任务。但是,在 curl json 传入参数是却不能获取参数值。先拼接好 curl 内容,并通过eval 执行拼接好的命令可以实现传参。

JOB=dingding
PHONE="1825718XXXX"
TOKEN="https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx"
DING="curl -H \"Content-Type: application/json\" -X POST --data '{\"msgtype\": \"text\", \"text\": {\"content\": \"${JOB} done!\"}, \"at\": {\"atMobiles\": [${PHONE}], \"isAtAll\": false}}' ${TOKEN}"
eval $DING

以上例子可以实现对某个群中机器人发送消息 “dingding done!”,并 @1825718XXXX。添加机器人请查看官方文档。


参考: linux下用shell来 CURL POST JSON 数据遇到的问题




你可能感兴趣的:(linux)