• Python 发送企业微信单发和群发机器人


    import time
    import requests
    import json
     
     
    class WeChat:
        def __init__(self):
            self.CORPID = 'ww2e1234567895498f5498f'  #企业ID,在管理后台获取
            self.CORPSECRET = 'xy11234567898hk_ecJ123456789DhKy4_1y12345OI'#自建应用的Secret,每个自建应用里都有单独的secret
            self.AGENTID = '1000002'  #应用ID,在后台应用中获取
            self.TOUSER = "maomao|dingding"  # 接收者用户名,多个用户用|分割
     
        def _get_access_token(self):
            url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
            values = {'corpid': self.CORPID,
                      'corpsecret': self.CORPSECRET,
                      }
            req = requests.post(url, params=values)
            data = json.loads(req.text)
            return data["access_token"]
     
        def get_access_token(self):
            try:
                with open('./tmp/access_token.conf', 'r') as f:
                    t, access_token = f.read().split()
            except:
                with open('./tmp/access_token.conf', 'w') as f:
                    access_token = self._get_access_token()
                    cur_time = time.time()
                    f.write('	'.join([str(cur_time), access_token]))
                    return access_token
            else:
                cur_time = time.time()
                if 0 < cur_time - float(t) < 7260:
                    return access_token
                else:
                    with open('./tmp/access_token.conf', 'w') as f:
                        access_token = self._get_access_token()
                        f.write('	'.join([str(cur_time), access_token]))
                        return access_token
     
        def send_data(self, message):
            send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.get_access_token()
            send_values = {
                "touser": self.TOUSER,
                "msgtype": "text",
                "agentid": self.AGENTID,
                "text": {
                    "content": message
                    },
                "safe": "0"
                }
            send_msges=(bytes(json.dumps(send_values), 'utf-8'))
            respone = requests.post(send_url, send_msges)
            respone = respone.json()   #当返回的数据是json串的时候直接用.json即可将respone转换成字典
            return respone["errmsg"]
     
     
    if __name__ == '__main__':
        wx = WeChat()
        wx.send_data("这是程序发送的第1条消息!
     Python程序调用企业微信API,从自建应用“告警测试应用”发送给管理员的消息!")
        wx.send_data("这是程序发送的第2条消息!")

    群发机器人:

    import requests
    import json
    
    dsj_url = ''
    
    
    def send_msg(send_message):
        data1 = json.dumps({'msgtype': "markdown",
                            "markdown": {
                                "content": send_message,
                                "mentioned_list": ["@all"]
                            }})
        # 指定机器人发送消息
        resp = requests.post(dsj_url, data1, auth=('Content-Type', 'application/json'))
        return '出现故障:%s,此次报警成功' % send_message
    
    
    if __name__ == '__main__':
        # 报警机器人url
        send_message = '''【可疑】<font color="warning">hids 检测到 异常登录!</font>
    
                          >源ip:<font color="comment">10.70.234.21</font>
                          >目标IP:<font color="comment">10.70.218.13</font>
                          >涉及资产:<font color="comment">jssz-ai-newton-cpu-06</font>
                          >详情:
                          >登录主机所属业务组:<font color="comment">未分组主机</font>
                          >登录使用账户:<font color="comment">root</font>
                          >发现时间:<font color="comment">2021-06-10 16:24:12</font>
                          '''
        res = send_msg(send_message)
        print(res)
    每天逼着自己写点东西,终有一天会为自己的变化感动的。这是一个潜移默化的过程,每天坚持编编故事,自己不知不觉就会拥有故事人物的特质的。 Explicit is better than implicit.(清楚优于含糊)
  • 相关阅读:
    java线程系列---Runnable和Thread的区别 (转载)
    JAVA基础(多线程Thread和Runnable的使用区别(转载)
    error: undefined reference to 'property_set (转载)
    Django基本命令
    第三篇数据库与ORM
    PyCharm下创建并运行我们的第一个Django项目
    第二篇MTV模型、基本命令、简单配置
    第一篇web框架
    Django框架全面讲解
    MySQL的异常问题
  • 原文地址:https://www.cnblogs.com/kylin5201314/p/15117040.html
Copyright © 2020-2023  润新知