#!/usr/bin/python3.6

#_*_coding:utf-8 _*_

#auther:ping


import requests,sys,json

import urllib3

urllib3.disable_warnings()


import importlib

importlib.reload(sys)


def GetTokenFromServer(Corpid,Secret):

   Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"

   Data = {

       "corpid":Corpid,

       "corpsecret":Secret

   }

   r = requests.get(url=Url,params=Data,verify=False)

   print(r.json())

   if r.json()['errcode'] != 0:

       return False

   else:

       Token = r.json()['access_token']

       file = open('/tmp/zabbix_wechat_config.json', 'w')

       file.write(r.text)

       file.close()

       return Token


def SendMessage(User,Agentid,Subject,Content):

   try:

       file = open('/tmp/zabbix_wechat_config.json', 'r')

       Token = json.load(file)['access_token']

       file.close()

   except:

       Token = GetTokenFromServer(Corpid, Secret)


   n = 0

   Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token

   Data = {

       "touser": User,             # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。

       #"totag": Tagid,                              

       #"toparty": Partyid,                            

       "msgtype": "text",                            

       "agentid": Agentid,                          

       "text": {

           "content": Subject + '\n' + Content

       },

       "safe": "0"

   }

   r = requests.post(url=Url,data=json.dumps(Data),verify=False)

   while r.json()['errcode'] != 0 and n < 4:

       n+=1

       Token = GetTokenFromServer(Corpid, Secret)

       if Token:

           Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token

           r = requests.post(url=Url,data=json.dumps(Data),verify=False)

           print(r.json())


   return r.json()



if __name__ == '__main__':

   User = sys.argv[1]                                              

   Subject = str(sys.argv[2])                                                

   Content = str(sys.argv[3])                                                    


   Corpid = "ww6831eec7222b07c6"                                                                     # CorpID是企业号的标识

   Secret = "I0MiByy8PIAifpJ_NdkQqfRqthmvdTO8CSnNAEcu05Y"                                                                # Secret是管理组凭证密钥

   #Tagid = "2"                                                                        # 通讯录标签ID

   Agentid = "1000003"                                                                 # 应用ID

   Partyid = "2"                                                                      # 部门ID


   Status = SendMessage(User,Agentid,Subject,Content)

   print("Status")



##测试发送:python zabbix_weixin.py "1" "test" "test"