钉钉机器人+Mac定时Launchctl

添加钉钉机器人

钉钉机器人+Mac定时Launchctl_第1张图片
钉钉机器人+Mac定时Launchctl_第2张图片

  • webhook地址是 post请求的url
  • 机器人开发文档

python 代码

# its_time_to_book_dinner.py
# coding:utf-8
import requests
dingtalk_url = "https://oapi.dingtalk.com/robot/send?" \
             "access_token=xxxxxxxxxxxxxxxxxx"
params = """
{
     "msgtype": "text",
     "text": {
         "content": "癫狗喊你美餐下单!"
     },
     "at": {
         "isAtAll": true
     }
}
"""
headers = {"Content-Type": "application/json"}
response = requests.post(dingtalk_url, data=params, headers=headers)
print response.content

Mac中的定时任务利器:launchctl

vim /path/to/run.sh
python /path/to/its_time_to_book_dinner.py

配置文件如下

vim ~/Library/LaunchAgents/com.dinner.order.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <!-- Label唯一的标识 -->
  <key>Label</key>
  <string>com.dinner.order.plist</string>
  <!-- 指定要运行的脚本 -->
  <key>ProgramArguments</key>
  <array>
    <string>/path/to/run.sh</string>
  </array>
  <!-- 指定要运行的时间 -->
  <key>StartCalendarInterval</key>
    <dict>

      <key>Minute</key>
      <integer>50</integer>

      <key>Hour</key>
      <integer>11</integer>

    </dict>
<!-- 标准输出文件 -->
<key>StandardOutPath</key>
<string>/path/to/run.log</string>
<!-- 标准错误输出文件,错误日志 -->
<key>StandardErrorPath</key>
<string>/path/to/run.err</string>
</dict>
</plist>

加载

launchctl load -w com.dinner.order.plist

修改配置文件重新加载

launchctl unload -w com.dinner.order.plist
launchctl load -w com.dinner.order.plist

你可能感兴趣的:(工具使用,钉钉机器人,Mac定时Launchctl)