iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >【微信告警脚本】python3企业微信告
  • 743
分享到

【微信告警脚本】python3企业微信告

脚本企业微信告 2023-01-31 07:01:46 743人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

#!/usr/bin/python # --*-- encoding=utf-8 --*-- import urllib.request import JSON import sys import simplejson def gett

#!/usr/bin/python
# --*-- encoding=utf-8 --*--

import urllib.request
import JSON
import sys
import simplejson

def gettoken(corpid, corpsecret):
    gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
    #print  gettoken_url
    try:
        token_file = urllib.request.urlopen(gettoken_url)
    except urllib.request.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(content,accesstoken):
    send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken
    send_values = {
        "touser": '@all',  # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。@all 发送所有人
        #"touser": "HX0011055|HX0011027",  # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        #"touser": "HX0010933",  # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        #"touser": "HX0011055",  # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        "toparty": "1",  # 企业号中的部门id。
        "msgtype": "text",  # 消息类型。
        "agentid": "1000003",  # 企业号中的应用id。
        "text": {
            "content": content
        },
        "safe": "0"
    }
    send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8')
    send_request = urllib.request.Request(send_url, send_data)
    response = json.loads(urllib.request.urlopen(send_request).read())
    print(str(response))

def deal(content):
    corpid = 'ww861ef6bad293D02311'  # CorpID是企业号的标识
    corpsecret = 'Pn3YA4H0d87He8ef-hpf0tGB5HzYUvsEtoLwUKsAHWott'  # corpsecretSecret是管理组凭证密钥
    accesstoken = gettoken(corpid, corpsecret)
    senddata(content,accesstoken)

#通过deal 函数直接发送信息
message = '测试'
deal(message)

--结束END--

本文标题: 【微信告警脚本】python3企业微信告

本文链接: https://www.lsjlt.com/news/192224.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作