1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#!/bin/bash corpid= "wxd6b3"
corpsecret= "aJTaPaGjW"
access_token=`curl -s "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$corpsecret" |jq '.access_token' | awk -F '"' '{print $2}' `
curl -l -H "Content-type: application/json" -X POST -d '{"touser":"@all","msgtype":"text","toparty":"14","agentid":"14","text":{"content":"测试"} , "safe":"0"}' "
Python脚本如下: # coding:utf-8 import sys
import urllib2
import time
import json
import requests
reload(sys) sys.setdefaultencoding( 'utf-8' )
#title = sys.argv[2] # 位置参数获取title 适用于zabbix #content = sys.argv[3] # 位置参数获取content 适用于zabbix title = "title 测试" # 位置参数获取title 适用于zabbix
content = "content 测试" # 位置参数获取content 适用于zabbix
class Token(object): # 获取token
def __init__(self, corpid, corpsecret):
self.baseurl = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}' . format (
corpid, corpsecret)
self.expire_time = sys.maxint
def get_token(self):
if self.expire_time > time . time ():
request = urllib2.Request(self.baseurl)
response = urllib2.urlopen(request)
ret = response. read ().strip()
ret = json.loads(ret)
if 'errcode' in ret.keys():
print >> ret[ 'errmsg' ], sys.stderr
sys. exit (1)
self.expire_time = time . time () + ret[ 'expires_in' ]
self.access_token = ret[ 'access_token' ]
return self.access_token
def send_msg(title, content): # 发送消息
corpid = "" # 填写自己应用的
corpsecret = "" # 填写自己应用的
qs_token = Token(corpid=corpid, corpsecret=corpsecret).get_token()
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}" . format (
qs_token)
payload = {
"touser" : "@all" ,
"msgtype" : "text" ,
"agentid" : "14" ,
"text" : {
"content" : "标题:{0}\n 内容:{1}" . format (title, content)
},
"safe" : "0"
}
ret = requests.post(url, data=json.dumps(payload, ensure_ascii=False))
print ret.json()
if __name__ == '__main__' :
# print title, content
send_msg(title, content)
|
微信服务号操作参考:
http://www.anbooks.cn/topic/4145905736700.html
本文转自 tianshuai369 51CTO博客,原文链接:http://blog.51cto.com/kkkkkk/1710011