1、shell脚本
#!/bin/bash
# 定义钉钉机器人 webhook 地址和 access_token
webhook_url="https://oapi.dingtalk.com/robot/send?access_token=68010751305d75dfed96cd303bcc5ce78910c5d59ff7f2b90f9f1d644a1d9ce0"
# 定义报警消息内容
content="p服务器 CPU 使用率过高,请尽快处理!"
# 发送 HTTP 请求,向钉钉机器人发送报警消息
curl ${webhook_url} \
-H 'Content-Type: application/json' \
-d "{
\"msgtype\": \"text\",
\"text\": {
\"content\": \"${content}\"
}
}"
2、python脚本
# -*- coding: utf-8 -*-
import requests
import json
# 定义钉钉机器人 webhook 地址和 access_token
webhook_url = "https://oapi.dingtalk.com/robot/send?access_token=68010751305d75dfed96cd303bcc5ce78910c5d59ff7f2b90f9f1d644a1d9ce0"
# 定义报警消息内容
content = {
"msgtype": "text",
"text": {
"content": "p服务器 CPU 使用率过高,请尽快处理!"
}
}
# 发送 HTTP 请求,向钉钉机器人发送报警消息
response = requests.post(webhook_url, data=json.dumps(content), headers={'Content-Type': 'application/json'})
# 打印返回结果
print(response.text)
3、发送钉钉群消息
#!/usr/bin/env python
# -*- encoding: UTF-8-*-
"""
@auther: lishang
@file:send_ding_md.py
@time:2023/08/03
"""
import requests
import json
def send_message_to_dingtalk(webhook_url, r):
headers = {'Content-Type': 'application/json;charset=utf-8'}
r = requests.post(webhook_url, data=json.dumps(r), headers=headers)
return r.text
# 设置钉钉群机器人的 Webhook URL
webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token' \
'=68010751305d75dfed96cd303bcc5ce78910c5d59ff7f2b90f9f1d644a1d9ce0'
# 发送消息[文本格式]
r1 = {
"at": {
"atMobiles": [
"17853481576"
],
"atUserIds": [
"user123"
],
"isAtAll": False
},
"text": {
"content": "我就是我, 是不一样的烟火 over"
},
"msgtype": "text"
}
# 发送消息[link格式]
r2 = {
"msgtype": "link",
"link": {
"text": "这个即将发布的新版本,创始人李尚称它为红树林。而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是红树林 over",
"title": "时代的火车向前开",
"picUrl": "https://bkimg.cdn.bcebos.com/pic/a044ad345982b2b7d0a2e4f75ffbdcef76094b362df3?x-bce-process=image"
"/watermark,image_d2F0ZXIvYmFpa2UxNTA=,g_7,xp_5,yp_5/format,f_auto",
"messageUrl": "https://www.zhihu.com/question/28853910/answer/2996587545"
}
}
# 发送消息[markdown格式]
r3 = {
"msgtype": "markdown",
"markdown": {
"title": "济南天气over",
"text": "#### 济南天气 \n "
"> 9度,西北风1级,空气良89,相对温度73%\n "
"> \n"
"> ###### 10点20分发布 [天气]( https://www.dingtalk.com) \n"
}
}
'''
接口文档具体参考: https://open.dingtalk.com/document/robots/custom-robot-access
'''
if __name__ == "__main__":
response = send_message_to_dingtalk(webhook_url, r3)
print(response)