Python:企业微信批量发工资条工具 -应用消息发送模块

16年python练手,开发了采用企业微信批量发工资条的程序,其中对企业微信发消息的功能做了包装。在此记录和分享一下。

整体程序,使用了 wxPython GUI ,以 excel 来存放数据和消息格式配置。

消息内容和格式都可以在excel内自由定制,不限列数,行数。下面是最终在企业微信内的显示效果。

发消息模块(仅供学习研究)

#!/usr/bin/python
# -*- coding: utf-8 -*-
# WeiXin_App_Helper.py 
#
# Copyright (C) 2016 - xulong <fangkailove@yeah.net>
#
import time
import json
import requests
#import pdb



#******************************************
#类定义区
#******************************************
class WeiXin_App_Helper(object):
    """ 
    #企业微信应用消息发送工具类, 
    corpid : 填写自己企业ID,
    corpsecret : 填写自己的管理组Secret,
    appid :应用ID
    """
    def __init__(self, corpid, corpsecret,appid):
        #self.corpid = corpid
        #self.corpsecret = corpsecret
        self.baseurl = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}'.format(corpid, corpsecret)
        self.appid = appid

        self.expire_time = 0.0
        self.access_token = ""

        #print 'ini exprie_time',self.expire_time



    # 获取token
    def __get_token(self):
        retDict = {'errcode':0}
        if self.expire_time < time.time(): #到期时间小于当前时间,说明token到期了需要重新获取
            try:
                response = requests.get(self.baseurl)
                #pdb.set_trace()
                if response.status_code == 200:
                    retDict = response.json()
                    if 'errcode' in retDict.keys():
                        if retDict['errcode'] != 0: 
                            return retDict
                    self.expire_time = time.time() + retDict['expires_in']
                    self.access_token = retDict['access_token']
            except  Exception, e:
                retDict["errcode"]=9999
                retDict["errmsg"]=e.message

        #print 'get new  expire_time:',self.expire_time
        return retDict 

    def send_msg(self,touser,content):
        """
         发送消息,
         touser:接收消息者(企业微信中的微信号),
         content:消息内容
        """

        #print "access_Token", qs_token
        #pdb.set_trace()
        retDict=self.__get_token()
        if retDict['errcode'] != 0: 
            return retDict

        url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}".format(self.access_token)
        payload = {
                    "touser": touser,
                    "msgtype": "text",
                    "agentid": self.appid,    
                    "text": {
                          "content": content
                    },
                    "safe": "1"
        }
        #print payload
        data = json.dumps(payload, ensure_ascii=False)
        data = data.encode('utf-8')
        try:
            #pdb.set_trace()
            response = requests.post(url, data=data)#,verify=resource_path("cacert.pem"))
            #print ret
            
            if response.status_code == 200:
                retDict = response.json()
        except  Exception, e:
            retDict["errcode"]=9999
            retDict["errmsg"]=e.message

        return retDict





if __name__ == '__main__':
    print 'test send message'
    g_corpid = "xxxxxxxxxxxxxxxxxx"  # 填写自己企业ID
    g_corpsecret ="XXXXXXXXXXXXXXXXXXXXXXXXXX"  # 填写自己的管理组Secret
    g_appid = 99  #应用ID
    winxin = WeiXin_App_Helper(g_corpid,g_corpsecret,g_appid)
    winxin.send_msg('101062','test mdsfasfasfasfessage')

转载请注明:转自 Python:企业微信批量发工资条工具 -应用消息发送模块_星陨阁:wxgnolux-CSDN博客_python企业微信接收消息

需要拿来就用的工具,请看下面博文: 基于企业微信和钉钉的工资条发送工具 -  工资条帮_星陨阁:wxgnolux-CSDN博客https://gnolux.blog.csdn.net/article/details/112056996

基于企业微信和钉钉的工资条发送工具 - 工资条帮新版操作说明_wxgnolux的博客-CSDN博客更新说明20220825ahttps://wxgnolux.lanzouj.com/iQKk80a9n29e 密码:enbq1.添加了 seal 标签,支持印章功能;2.图文编辑工具添加了侦听,消息模板修改时,会自动刷新;3.修订了数据检查 账号姓名匹配检查时的bug。20220821b:1.消息模按钮边栏默认为隐藏状态,只有打开数据文件才显示。2.没有有效参数表的数据,加载后不清除原消息模板。3.图文编辑器内的占位图重新换了。............https://gnolux.blog.csdn.net/article/details/124537239 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wxgnolux

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

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

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

打赏作者

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

抵扣说明:

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

余额充值