wiexin_file.py

文件版

#!/usr/bin/env python
# -*- coding:utf-8 -*-

__author__ = "jihongrui@jsqix.com"

import urllib2
import json

Appid_Group = {
    '1': '2',  # zabbix 专用
    '6': '4',  # 服务器专用
    '7': '5',  # DBA 专用
    '8': '6',  # 技术部专用
    '9': '7',  # 客服部专用
    '10': '8',  # 杭州组专用
    '11': '9'  # 测试专用
}

class weixin(object):
    ""
    def __init__(self,appid,msg):
        ""
        self.appid = str(appid)
        self.msg = str(msg)
        self.tokenfile = 'token.log'

    def get_token(self,mode=False):
        ""
        if mode:
            file = open(self.tokenfile,'r')
            # try:
            #     self.token  = file.read()
            # finally:
            #     file.close()
            with open(self.tokenfile, 'r') as f:

                self.token = f.read()
        else:

            appid = "wxddaasdasdcb42d5"
            secrect = "N26FX62Ej5isadasdasdasdttQX62vw5gKmpbUkap4_6CczzI2d7o"
            GURL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s" % (appid, secrect)
            try:
                f = urllib2.urlopen(GURL).read()
                j = json.loads(f)
                self.token = j['access_token']  # .encode('utf8')

                file = open(self.tokenfile,'w')
                # try:
                #     file.write(self.token)
                # finally:
                #     file.close()
                with open(self.tokenfile,'w') as f:
                    f.write(self.token)
            except:
                print "ERROR from class get token"
                exit("error token")

    def send_msg(self):
        ""
        # App_ID = str(AppID)
        Group_ID = Appid_Group[self.appid]
        User_ID = '@all'
        # Message = str(message)
        dict_arr = {
            "touser": User_ID,  # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
            "toparty": Group_ID,  # 企业号中的部门id
            "msgtype": "text",
            "agentid": self.appid,  # 企业号中的应用id,消息类型。
            "text": {
                "content": self.msg
            },
            "safe": "0"
        }

        try:
            json_template = json.dumps(dict_arr, ensure_ascii=False)
            requst_url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + self.token
            Format_MSG = urllib2.urlopen(requst_url, json_template)
            return_MSG = Format_MSG.read()
            # 读取json数据
            j = json.loads(return_MSG)
            j.keys()
            # return self.j.keys
            errcode = j['errcode']
            # print errcode,type(errcode)
            errmsg = j['errmsg']
            # print errmsg,type(errmsg)
            if errcode == 0 and errmsg == u'ok':
                return True
            else:
                return False

        except urllib2.HTTPError, e:
            print e
        except urllib2.URLError, e:
            print e
        except:
            print "ERROR from send message"

def DO_PUSH(appid,msg):
    ""
    xp = weixin(appid,msg)
    xp.get_token(True)
    if xp.send_msg():
        return True
    else:
        xp.get_token(False)
        if xp.send_msg():
            return True
        else:
            return False


if __name__ == '__main__':
    ""
    DO_PUSH(6,'test')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,934评论 9 468
  • 所有关于Internet 的正式标准都是以RFC(Request for Comment )文档出版。另外大量的R...
    SawyerWang阅读 14,019评论 0 9
  • Windows 10 企业版 1511版 (64位) Windows 10 Enterprise, Version...
    g0阅读 11,564评论 1 8
  • 转载自酷安 @breathiness #电脑玩家# #电脑好软# #学习使我快乐# 电脑好软推荐——Linux 篇...
    失落无意义阅读 11,283评论 0 50
  • 在追名逐利的世间,我们都在拼命的活下来,为了活的更好,我们不敢懈怠任何一点时间,紧绷的神经像拉直的玄,不知道在哪一...
    请不同名阅读 2,206评论 0 0