#! /usr/bin/env python
# coding:utf-8
import sys
import urllib2
import time
import json
import requests
def send_weixin(title,content):
baseurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=你的corpID&corpsecret=你的corpsercert"
request = urllib2.Request(baseurl)
response = urllib2.urlopen(request)
ret = response.read().strip()
dd = eval(ret)
# print dd["access_token"]
mytoken = dd["access_token"]
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}".format(mytoken)
payload = {
"touser": "@all",
"msgtype": "text",
"agentid": "你的agentID",
"text": {
"content": "title:{0}\n content:{1}".format(title, content)
},
"safe": "0"
}
ret = requests.post(url, data=json.dumps(payload, ensure_ascii=False))
# print ret.json()
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-t","--title",dest="title",default="nagios,be careful",
help="input your nagios title",metavar="title")
parser.add_option("-c","--content",dest="content",default="trouble is open",
help="input your nagios content",metavar="content")
(options,args) = parser.parse_args()
send_weixin(options.title,options.content)
python微信企业号群发消息
最新推荐文章于 2024-07-04 15:36:49 发布