企业微信人员日程推送接口(python版)

企业在使用企业微信中,有时想批量推送员工日程信息。这里写了个接口工具分享给大家,供交流学习。

主入口程序:

 

# -*- coding: utf-8 -*-
import time as time

import pymysql as mdb
import getdbconn as dbconn
import renyrw

 

 

 

 

def Main_sendmsg_toUser():

conn=dbconn.dbconn()
cur = conn.cursor(mdb.cursors.DictCursor)
sql_getmsg="select *from wxmsg_rwb where status<>‘Y‘ "
sql_up="update wxmsg_rwb set status=‘Y‘ where id=%s"
cur.execute(sql_getmsg)

rs=cur.fetchall()
for r in rs:
id=(‘%s‘ %r[‘id‘])
touser=(‘%s‘ %r[‘username‘])
msg=(‘%s‘ %r[‘msg‘])
msgtitle=(‘%s‘ %r[‘msgtitle‘])
url=(‘%s‘ %r[‘url‘])
urltxt=(‘%s‘ %r[‘urltxt‘])
renyrw.Api_upload_renyrw(‘1‘,touser,msg)

params=(id)
cur.execute(sql_up,params)
conn.commit()
cur.close()
conn.close()

if __name__ == ‘__main__‘:

Main_sendmsg_toUser()

 

-----------

发送方法,renyrw.py

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

import time as time
import requests
import json
import urllib.request as rq

#from appcomm import appcomm

 

##调用企业微信api 给员工发消息. touser --userid即通讯录员工账号,corpid--企业微信id,corpsecret--应用的密匙,每个应用的不一样;

def get_access_token(cmpid):
#corpid,apikey_txl,apikey_dk,apikey_rch=appcomm.getapiinfo(cmpid)
corpid=‘ ‘
apikey_rch=‘ ‘
url = ‘https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s‘ % (corpid,apikey_rch) #
#url = ‘https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s‘ % (‘wwcffccbc9b870f383‘,‘jZjHJeX-af1EcPLW3h8bMgrLXG9Y0TNkPwC7EWP_CgI‘) #
req = rq.Request(url)
result = rq.urlopen(req)
access_token = json.loads(result.read())

#print(access_token["access_token"])
return access_token["access_token"]

def Api_upload_renyrw(cmpid,userid,text):
#print(getUser(bm))

time_s=time.strftime(‘%Y-%m-%d‘,time.localtime(time.time()))+" 08:00:00"
time_e=time.strftime(‘%Y-%m-%d‘,time.localtime(time.time()))+" 23:00:00"
#time_s="2020-06-18 00:00:00"
#time_e="2020-06-18 23:00:00"
uix_stime = time.mktime(time.strptime(time_s, "%Y-%m-%d %H:%M:%S"))
uix_etime = time.mktime(time.strptime(time_e, "%Y-%m-%d %H:%M:%S"))
#print("3.把字符串转成时间戳形式:", time.mktime(time.strptime(time_s, "%Y-%m-%d %H:%M:%S")))
#print(time_s)
#print(time_e)
#print(uix_stime)
#print(uix_etime)
data={

"schedule": {
"organizer": userid,
"start_time": uix_stime,
"end_time": uix_etime,
"attendees": [
{
"userid": userid
}
],

"summary": text,
"description": "日程信息",
"reminders": {
"is_remind": 1,
"remind_before_event_secs": 3600,
"is_repeat": 0,
"repeat_type": 7
},
"location": "",
"cal_id": ""
}



}
print(data)
json_template=json.dumps(data,ensure_ascii=False)
access_token=get_access_token(cmpid)
print("access_token--",access_token)
#access_token=‘J3bOu0egcg0VWVQHd7IlbkpN_uZjrfD3EK32aw9qFwTzctTC2vikpwzJhd5jgDJAzBgVqOtZlHmS3MT1_PMk6ww-eeipv_04W2CGnT5sK43slQUNoZfMeE-5pyvFf2FBLad3berUuzb-3ysAKWchiQTXh9dGD13fEEmGxtxT9FwYKxRY_s-kVQurWHuckNNGnntkmCdpangWKwenw4Y6sw‘
url="https://qyapi.weixin.qq.com/cgi-bin/oa/schedule/add?access_token="+access_token
#https://qyapi.weixin.qq.com/cgi-bin/oa/schedule/add?access_token=ACCESS_TOKEN

try:
respone=requests.post(url,data=json_template.encode(‘UTF-8‘), timeout=50)
#拿到返回值
errcode=respone.json().get("errcode")
print("返回:--",respone.json())
if(errcode==0):
print("成功")
else:
print("失败")
except Exception as e:
print("异常----",e)

 

-----

数据库连接方法 getdbconn.py

import pymysql as mdb

def dbconn():
host=‘192.168.0.100‘
user=‘user‘
password=‘12345‘
database=‘db1‘
conn=mdb.connect(host,user,password,database)
return conn

 

-----日程信息接口数据表结构  mysql版

/*
Navicat MySQL Data Transfer

Source Server :
Source Server Version : 50717
Source Host :
Source Database :

Target Server Type : MYSQL
Target Server Version : 50717
File Encoding : 65001

Date: 2020-08-19 10:13:16
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for wxmsg_rwb
-- ----------------------------
DROP TABLE IF EXISTS `wxmsg_rwb`;
CREATE TABLE `wxmsg_rwb` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(500) DEFAULT NULL,
`msg` varchar(2000) DEFAULT NULL,
`status` char(10) DEFAULT NULL,
`msgtitle` varchar(200) DEFAULT NULL,
`url` varchar(400) DEFAULT NULL,
`ontime` varchar(40) DEFAULT NULL,
`urltxt` varchar(40) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20861 DEFAULT CHARSET=utf8;

 

-----使用方法,对接企业内部系统如ERP等,把日程相关数据写入表wxmsg_rwb中,定时执行接口程序即可推送日程信息到人员企业微信,如下形式展示:

企业微信人员日程推送接口(python版)

 

企业微信人员日程推送接口(python版)

上一篇:Linux 引导过程内幕(转载)


下一篇:MyEclipse无法破解的某种解决方法