背景:
由于公司打卡规则是15min采点一次,采点即定位,如定位异常会不计算在工时内,除非进行异常申诉;
大多同事经常因为不小心杀掉app进程、或打电话引起网络异常等原因,引起采点异常;
故想出监控采点接口,通过打通钉钉群及jenkins,及时监控到异常,得以立马打开app恢复正常
打通钉钉群通知
第一步:钉钉右上角+ 加群 —— 群设置——智能群助手——添加机器人——添加成功




第二步:自定义机器人安全设置
目前机器人一定要有安全设置,如果用 Python 脚本的话,推荐用加签方式
【具体查看下方官网可查看到其他语言的代码示例】
https://developers.dingtalk.com/document/robots/customize-robot-security-settings
import time
import hmac
import hashlib
import base64
import urllib.parse
#
# 加签
timestamp = str(round(time.time() * 1000))
secret = 'this is secret'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code =hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
# print(timestamp)
# print(sign)
这部分的代码直接复制过来即可,secret = 添加机器人时,安全设置中的加签码(上图三)
第三步:调用钉钉接口,设置发送消息内容
def dingmessage():
token = "这个是token,url自带的"
# 请求的url,webhook地址
webhook =f"https://oapi.dingtalk.com/robot/send?access_token={token}×tamp={timestamp}&sign={sign}"
# 构建请求头部
header = {"Content-Type": "application/json","Charset": "UTF-8"}
# 循环生成器并发送消息
for resp in get_point(userId):
message = {
"msgtype": "text", #text带收到/回复的消息,markdown只是文本消息
"text": {"content": resp},
"at": {
"atMobiles": [
"1557235××××"
],
# @ 所有人
"isAtAll": False
}
}
message_json = json.dumps(message)
info = requests.post(url=webhook, data=message_json, headers=header, verify=False) # 打印返回的结果
print(info.text)
if __name__ == '__main__':
dingmessage()
-
webhook地址,在机器人生成后,会自动生成,复制即可;
注意:地址中的参数 access_token={token}×tamp={timestamp}&sign={sign}
需要加上安全设置中的2个参数 -
关于消息这块的设置,见官网【具有多种形式消息】
https://open.dingtalk.com/document/robots/custom-robot-access
注意:此处我调用的业务中返回的参数信息,尝试了3种类型的;
"msgtype": "text", #text 带“收到/回复”按钮的消息,markdown只是文本消息,可加 url、图片等
"text": {"content": resp},
"at": {
"atMobiles": [
"1557235××××"
],
# True @ 所有人 False需搭配指定手机号或userId使用,指定到具体人员
"isAtAll": False

- 本篇主要讲打通钉钉群,需结合上一篇监控业务数据的代码,一起使用
网友评论