企业微信告警Python模板

# -*- coding: utf-8 -*-
import requests
import json
class DLF:
def __init__(self, corpid, corpsecret):
self.url = "https://qyapi.weixin.qq.com/cgi-bin"
self.corpid = corpid #填写corpid
self.corpsecret = corpsecret #填写corpsecret


self._token = self._get_token()
def _get_token(self):
'''
获取企业微信API接口的access_token
:return:
'''
token_url = self.url + "/gettoken?corpid=%s&corpsecret=%s" %(self.corpid,
self.corpsecret)
try:
res = requests.get(token_url).json()
token = res['access_token']
return token
except Exception as e:
return str(e)
def _get_media_id(self, file_obj):
get_media_url = self.url + "/media/upload?access_token=
{}&type=file".format(self._token)
data = {"media": file_obj}
try:
res = requests.post(url=get_media_url, files=data)
media_id = res.json()['media_id']
return media_id
except Exception as e:
return str(e)
def send_text(self, agentid, content, touser=None, toparty=None):
send_msg_url = self.url + "/message/send?access_token=%s" %
(self._token)
send_data = {
"touser": touser,
"toparty": toparty,
"msgtype": "text",
"agentid": agentid,
"text": {
"content": content
    }
}
try:
res = requests.post(send_msg_url, data=json.dumps(send_data))
except Exception as e:
return str(e)
def send_image(self, agentid, file_obj, touser=None, toparty=None):
media_id = self._get_media_id(file_obj)
send_msg_url = self.url + "/message/send?access_token=%s" %
(self._token)
send_data = {
"touser": touser,
"toparty": toparty,
"msgtype": "image",
"agentid": agentid,
"image": {
"media_id": media_id
    }
}
try:
res = requests.post(send_msg_url, data=json.dumps(send_data))
except Exception as e:
return str(e)

这段代码是一个用于与企业微信(WeChat Work)交互的 Python 类,用于发送文本消息和图片消息。让我们逐行解释代码的功能和每个部分的作用:

1. 导入模块和设置编码

# -*- coding: utf-8 -*-

import requests

import json

  • requests 模块用于发送 HTTP 请求。
  • json 模块用于处理 JSON 数据。
  • # -*- coding: utf-8 -*- 表示该文件采用 UTF-8 编码。

2. 定义 DLF 类

class DLF:

   def __init__(self, corpid, corpsecret):

     self.url = "https://qyapi.weixin.qq.com/cgi-bin"

     self.corpid = corpid # 填写 corpid

     self.corpsecret = corpsecret # 填写 corpsecret

     self._token = self._get_token()

  • 定义了一个名为 DLF 的类,用于与企业微信进行交互。
  • __init__ 方法用于初始化对象,需要提供 corpidcorpsecret 作为参数。
  • url 是企业微信 API 的基础 URL。
  • corpidcorpsecret 是企业微信应用的身份验证信息。
  • _token 是一个私有属性,用于存储获取的 access_token。

3. 获取 access_token

    def _get_token(self):
        '''
        获取企业微信API接口的access_token
        :return:
        '''
        token_url = self.url + "/gettoken?corpid=%s&corpsecret=%s" % (self.corpid, self.corpsecret)
        try:
            res = requests.get(token_url).json()
            token = res['access_token']
            return token
        except Exception as e:
            return str(e)

  • _get_token 方法用于从企业微信服务器获取 access_token。
  • 使用 requests.get 发送 GET 请求获取 access_token。
  • 如果请求成功,从返回的 JSON 数据中提取出 access_token 并返回;如果请求失败,返回错误信息。

4. 获取媒体文件 ID

    def _get_media_id(self, file_obj):
        get_media_url = self.url + "/media/upload?access_token={}&type=file".format(self._token)
        data = {"media": file_obj}
        try:
            res = requests.post(url=get_media_url, files=data)
            media_id = res.json()['media_id']
            return media_id
        except Exception as e:
            return str(e)

  • _get_media_id 方法用于上传媒体文件并获取其对应的 media_id。
  • 使用 requests.post 发送 POST 请求上传媒体文件。
  • 如果上传成功,从返回的 JSON 数据中提取出 media_id 并返回;如果上传失败,返回错误信息。

5. 发送文本消息

    def send_text(self, agentid, content, touser=None, toparty=None):
        send_msg_url = self.url + "/message/send?access_token=%s" % (self._token)
        send_data = {
            "touser": touser,
            "toparty": toparty,
            "msgtype": "text",
            "agentid": agentid,
            "text": {
                "content": content
            }
        }
        try:
            res = requests.post(send_msg_url, data=json.dumps(send_data))
        except Exception as e:
            return str(e)

  • send_text 方法用于发送文本消息。
  • 构建消息内容和接收者信息,并将其转换为 JSON 格式。
  • 使用 requests.post 发送 POST 请求发送消息。

6. 发送图片消息

    def send_image(self, agentid, file_obj, touser=None, toparty=None):
        media_id = self._get_media_id(file_obj)
        send_msg_url = self.url + "/message/send?access_token=%s" % (self._token)
        send_data = {
            "touser": touser,
            "toparty": toparty,
            "msgtype": "image",
            "agentid": agentid,
            "image": {
                "media_id": media_id
            }
        }
        try:
            res = requests.post(send_msg_url, data=json.dumps(send_data))
        except Exception as e:
            return str(e)

  • send_image 方法用于发送图片消息。
  • 调用 _get_media_id 方法获取图片文件的 media_id。
  • 构建消息内容和接收者信息,并将其转换为 JSON 格式。
  • 使用 requests.post 发送 POST 请求发送消息。

这个类可以用于在企业微信中发送文本消息和图片消息,提供了简单易用的接口。

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Rancher 是一款容器管理平台,可以帮助企业以更高效的方式部署和管理容器化的应用程序。相比起传统的虚拟机部署方式,容器化的应用程序具有更高的可移植性和跨平台性。然而,随着容器规模的不断扩大,监控和告警成为企业微信(Rancher企业微信告警)中不可忽视的一部分。 Rancher 的告警功能可以帮助用户及时了解到容器集群中发生的异常情况,并通过各种途径通知相关人员。企业微信作为一款流行的企业通讯工具,可以与Rancher集成,将告警信息发送给企业微信中指定的用户或者群组。 用户可以根据自己的需求,在Rancher中配置告警规则。例如,当容器的CPU使用率或内存使用率超过设定的阈值时,Rancher会触发告警,并将相关信息发送到企业微信中。同时,Rancher还支持通过企业微信发送告警通知的方式,可以提供详细的应用程序日志和监控指标,方便用户了解到问题的具体原因。 Rancher企业微信告警的好处在于,可以及时通知相关人员,让他们能够快速响应并采取应对措施。另外,Rancher还支持自定义告警模板,用户可以根据自身需求,定制化告警通知的内容和格式,提高告警信息的可读性。 总结一下,Rancher企业微信告警是将容器集群中的异常情况以告警的形式发送到企业微信中,通过该功能,用户可以及时了解到问题并采取相应的措施。这种集成功能能够提升企业的运维效率,确保容器应用程序的稳定性和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向日葵般灿烂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值