平时我们打包都是在打包机上通过Jenkins来自动打包,但我们不知道打包机什么时候能打完包,总不能经常看打包机有没有打完包吧,所有就写了一个脚本来通知企业微信的人告知打包完成了。
安装
首先要安装python2.7 或者python3.x都可以,建议安装python3.6.
把路径添加到系统环境变量里
然后再通过cmd命令执行pip install requests
等待安装完就可以了
Python代码块
# -*- coding: utf-8 -*-
import requests
import sys
print sys.getdefaultencoding()
isSuccess = sys.argv[1]#批处理命令传进来的参数,是打包成功与否的标志
FILE_PATH = sys.argv[2]#打包完成后的文件输出路径
def BuildSuccess(FILE_PATH):
headers = {"Content-Type": "application/json"}
s="xxx提醒您:mm打包机打包成功 路径为:\n".decode('UTF-8').encode('GBK')+FILE_PATH
print(s)
data = {
"msgtype": "text",
"text": {
"content": s.decode('GBK').encode('UTF-8'),
"mentioned_mobile_list":["13966666666"]#这里填需要@的人的手机号
}
}
r = requests.post(
url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=kkkkkkkkkkkkkkkkkkkk',#这里是企业微信群中的机器人链接
headers=headers, json=data)
def BuildFailure():
headers = {"Content-Type": "application/json"}
s="xx提醒您:mm打包机打包失败"
data = {
"msgtype": "text",
"text": {
"content": s,
"mentioned_mobile_list":["13966666666"]#这里填需要@的人的手机号
}
}
r = requests.post(
url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=kkkkkkkkkkkkkkkkkkkk',#这里是企业微信群中的机器人链接
headers=headers, json=data)
if isSuccess == "true":
BuildSuccess(FILE_PATH)
else:
BuildFailure()
这样,打包成功和打包失败时都可以@不同的人,并且发出不同的消息。
这个脚本中的参数一般是jenkins的批处理脚本中传过来的。
mac系统的shell中进行通知企业微信的方式
#--------------------------------------通知企业微信群--------------------------------------
if [ "${IsNotice}" == true ];then
echo "7-【开始通知企业微信群】"
if [ $IsFinish == true ];then
Target="完成:B2[补丁包]"
else
Target="失败:B2[补丁包]"
fi
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=6dfda2e9-cfe7-4d08-ac1f-xxxxxxxxxxxx' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "text",
"text": {
"content": "出包位置:\\'${OutputPath}' @陈嘉欣(jiaxin)"
}
}'
else
echo "7-【不通知企业微信群】"
fi
#--------------------------------------通知企业微信群--------------------------------------
飞书通知
发送普通文本消息
# -*- coding: utf-8 -*-
import requests
import time
def push_report(web_hook):
header = {
"Content-Type": "application/json;charset=UTF-8"
}
curTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
message_body = {
"msg_type": "text",
"content":
{
"text": "当前时间:"+curTime+"\n"+"hello world"
},
}
ChatRob = requests.post(url=web_hook, json=message_body, headers=header)
opener = ChatRob.json()
print("opener:{}".format(opener))
if opener["StatusMessage"] == "success":
print(u"%s 通知消息发送成功!" % opener)
else:
print(u"通知消息发送失败,原因:{}".format(opener))
if __name__ == '__main__':
webhook = "https://open.feishu.cn/open-apis/bot/v2/hook/e01d051xxxxxxxxxxxxxxxxxxx"
push_report(webhook)
发送带链接并且可以艾特人的消息
# -*- coding: utf-8 -*-
import requests
import time
def push_report(web_hook):
header = {
"Content-Type": "application/json;charset=UTF-8"
}
curTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
message_body = {
"msg_type": "post",
"content": {
"post": {
"zh_cn": {
"title": "项目更新通知",
"content": [
[ {
"tag": "text",
"text": "项目有更新: "
},
{
"tag": "a",
"text": "请查看",
"href": "https://www.bilibili.com/video/BV1PA411w77M/?spm_id_from=333.1007.top_right_bar_window_custom_collection.content.click"
},
{
"tag": "at",
"user_id": "96442767"
},
]
]
}
}
}
}
ChatRob = requests.post(url=web_hook, json=message_body, headers=header)
opener = ChatRob.json()
print("opener:{}".format(opener))
if opener["StatusMessage"] == "success":
print(u"%s 通知消息发送成功!" % opener)
else:
print(u"通知消息发送失败,原因:{}".format(opener))
if __name__ == '__main__':
webhook = "https://open.feishu.cn/open-apis/bot/v2/hook/e01d0516xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
push_report(webhook)