116-钉钉机器人

在很多情况下,如果能发消息到手机是一个非常实用的功能。比如,zabbix监控报警,可以采用执行脚本,向手机发送报警消息。
阿里巴巴的钉钉可以很方便的向用户发送消息,实际上就是建一个群,在群里面创建一个机器人。发消息就是通过脚本让机器人在群里说话而已。
在群里创建机器人并设置,可以通过钉钉网页版,网址为:https://im.dingtalk.com/
创建机器人的步骤如下:
1、在钉钉群聊的右上角点机器人

《116-钉钉机器人》 ding1.png

2、点击“+”添加机器人

《116-钉钉机器人》 ding2.png

3、选择机器人类型

《116-钉钉机器人》 ding3.png

4、给机器人起名

《116-钉钉机器人》 ding4.png

5、将webhook内容保存下来,备用

《116-钉钉机器人》 ding5.png

编写脚本dingtalk.py:

#!/usr/bin/env python3

import json
import requests
import sys


def send_msg(url, reminders, msg):
    headers = {'Content-Type': 'application/json;charset=utf-8'}
    data = {
        "msgtype": "text",  # 发送消息类型为文本
        "at": {
            "atMobiles": reminders,
            "isAtAll": False,   # 不@所有人
        },
        "text": {
            "content": msg,   # 消息正文
        }
    }
    r = requests.post(url, data=json.dumps(data), headers=headers)
    return r.text

if __name__ == '__main__':
    msg = sys.argv[1]
    reminders = ['15055667788']  # 特殊提醒要查看的人,就是@某人一下
    url = 此处填写上面第5步webhook的内容
    print(send_msg(url, reminders, msg))

发送消息进行测试:

# python3 dingtalk.py "这是一个测试而已"

收到的消息如下:

《116-钉钉机器人》 ding6.png

    原文作者:凯茜的老爸
    原文地址: https://www.jianshu.com/p/a3c62eb71ae3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞