shell和Python调用企业微信服务号进行报警

简介:
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

相关文章
|
7月前
|
安全 开发者 Python
用Python找出了删除自己微信的所有人并将他们自动化删除了
用Python找出了删除自己微信的所有人并将他们自动化删除了
|
7月前
|
JSON Java 测试技术
Python + Appium 自动化操作微信入门
Python + Appium 自动化操作微信入门
140 0
|
1月前
|
数据采集 测试技术 API
python爬虫之app爬取-微信朋友圈
搭建appium环境,appium基本使用,API操作等等
79 0
|
1天前
|
运维 Shell Python
Shell和Python学习教程总结
Shell和Python学习教程总结
|
2天前
|
监控 Python
Python监控主机是否存活,并发报警邮件
Python监控主机是否存活,并发报警邮件
|
2天前
|
数据采集 存储 人工智能
【Python+微信】【企业微信开发入坑指北】4. 企业微信接入GPT,只需一个URL,自动获取文章总结
【Python+微信】【企业微信开发入坑指北】4. 企业微信接入GPT,只需一个URL,自动获取文章总结
13 0
|
2天前
|
人工智能 机器人 API
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
6 0
|
2天前
|
缓存 人工智能 API
【Python+微信】【企业微信开发入坑指北】2. 如何利用企业微信API主动给用户发应用消息
【Python+微信】【企业微信开发入坑指北】2. 如何利用企业微信API主动给用户发应用消息
6 0
|
2天前
|
Linux 网络安全 开发工具
【超详细!超多图!】【代码管理】Python微信公众号开发(3)- 服务器代码上传Github
【超详细!超多图!】【代码管理】Python微信公众号开发(3)- 服务器代码上传Github
10 0
|
1月前
|
数据采集 存储 关系型数据库
Python爬虫-使用代理获取微信公众号文章
使用代理爬取微信公众号文章
54 0