通过钉钉机器人可以通过程序向手机端的钉钉发送信息。
import requests ?# 发送HTTP 请求模块
import json ?# json 解析模块
-
开启步骤
-
在钉钉聊天工具当中加入聊天机器人并获取webhook 接口 -
编写 钉钉机器人聊天的 python 代码
-
相关资料
创建聊天机器人,获取Webhook接口。
启动钉钉(这里使用网页版钉钉代替手机版的钉钉)
进入网址 阿里巴巴-钉钉,是一个工作方式!超过1000w家企业正在使用钉钉!用手机进行登录

现创建群组(如果已经有群组可以忽略此步骤)

为群主添加两个或两个以上的成员。为群取个名称,这里取名为:达内-LINUX云计算学院 。

点击右上角的机器人图标。你给群添加一个机器人。

点击加号添加机器人。

选择自定义机器人。

选择添加。

为机器人取一个名称。安全设置设置为关键字方式,这里关键字为学习。
每次发送的数据时必须带学习 这两个字才能发送出去。

设置完毕,得到操纵机器人的API链接。

https://oapi.dingtalk.com/robot/send?access_token=e18e76a4bfef376cf96592805315ab5f5c87550c882c7e15a23421b90c9df388
钉钉机器人聊天示例
import requests
import json
url = '您机器人的webhook地址'
url = 'https://oapi.dingtalk.com/robot/send?access_token=e18e76a4bfef376cf96592805315ab5f5c87550c882c7e15a23421b90c9df388'
headers = {'Content-Type': 'application/json; charset=UTF-8'}
data = {
"msgtype": "text",
"text": {
"content": "好好学习天天向上我就是我, 是不一样的烟火@156xxxx8827"
},
"at": {
"atMobiles": [ # @哪些人
# "156xxxx8827",
# "189xxxx8325"
],
"isAtAll": False # @所有人
}
}
# data = {
# "msgtype": "markdown",
# "markdown": {
# "title": "春节放假通知",
# "text": """## 放假通知
# 
# 好好学习天天向上 [TMOOC](http://tmooc.cn)
# """
# },
# "at": {
# "atMobiles": [
# "13488820215"
# ],
# "isAtAll": False
# }
# }
?
data = {
"msgtype": "markdown",
"markdown": {
"title": "七夕节",
"text": "#### 七夕节\n> 银烛秋光冷画屏,轻罗小扇扑流萤。天街夜色凉如水,卧看牵牛织女星。\n好好学习天天向上 \n> \n> ###### 七夕 [七夕](https://baike.sogou.com/v179909070.htm) \n"
},
"at": {
"atMobiles": [
],
"isAtAll": False
}
}
?
?
r = requests.post(url, headers=headers, data=json.dumps(data))
print(r.json())
|