python 工资管理软件_Python:企业微信指量发工资条工具 -消息发送模块

本文介绍了如何使用Python的wxPython库和企业微信API开发一个程序,该程序通过Excel存储数据和消息格式,灵活定制消息内容和格式,批量发送工资条消息给企业微信用户。代码中包含了获取access_token的逻辑以及发送消息的函数实现。
摘要由CSDN通过智能技术生成

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

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

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

266cd5c6fca9490e898e32ca7212bbd7.jpg

发消息模块

#!/usr/bin/python

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

# WeiXin_App_Helper.py

#

# Copyright (C) 2016 - xulong #

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')

转载请注明:转自 https://blog.csdn.net/fangkailove/article/details/90604890

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值