python项目源码 日程管理_企业微信人员日程推送接口(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='企业自己的corpid '

apikey_rch=' 企业自己的日程密匙key'

url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (corpid,apikey_rch) #

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)

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中,定时执行接口程序即可推送日程信息到人员企业微信,如下形式展示:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值