python脚本调用微信发送svn版本号

svn钩子脚本:

post-commit.bat

@echo off
SET REPOS=%1
SET REV=%2
SET DIR=%REPOS%/hooks
SET PATH=%PATH%;
SET WORKING=D:/jenkins
"C:\Program Files\TortoiseSVN\bin\svn.exe" update %WORKING% --username "test" --password "test123"
python "D:\scripts\sys1.py" %REV%

sys1.py

#!/usr/bin/env python
#_*_coding:utf-8 _*_
 
import urllib,urllib2
import json
import sys
import simplejson
import subprocess
 
def gettoken(corpid,corpsecret):
    gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
    print  gettoken_url
    try:
        token_file = urllib2.urlopen(gettoken_url)
    except urllib2.HTTPError as e:
        print e.code
        print e.read().decode("utf8")
        sys.exit()
    token_data = token_file.read().decode('utf-8')
    token_json = json.loads(token_data)
    token_json.keys()
    token = token_json['access_token']
    return token
 
def senddata(access_token,user):
    info=sys.argv[1]
    send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
    #p=subprocess.Popen("/root/wechat/check_app.sh",shell=True,stdout=subprocess.PIPE)
    #content1 = p.stdout.read()
    send_values = {
        "touser":user,
        "toparty":"1",
        "msgtype":"text",
        "agentid":1,
        # "text":{"content":"测试消息,请无视"},
        "text":{"content":"告警信息" + '\n' + "svn有更新,版本号为:" + info},
        "safe":"0"}
    # send_values["text"]["content"]="欢迎"
    # send_data = json.dumps(send_values, ensure_ascii=False)
    send_data = simplejson.dumps(send_values, ensure_ascii=True).encode('utf-8')
    send_request = urllib2.Request(send_url, send_data)
    response = json.loads(urllib2.urlopen(send_request).read())
    print str(response)

 

if __name__ == '__main__':
    user='gui'
    #content='欢迎光临'
    corpid ='wx86…………'
    corpsecret = 'X_gA-r………………………………'
    accesstoken = gettoken(corpid,corpsecret)
    senddata(accesstoken,user)

里面通过info 变量传入svn版本号,微信告警通知。

转载于:https://my.oschina.net/yiyuanxi/blog/2394889

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值