【python】企微代码 通讯录交互

import requests
import json
import datetime

# 常量需自定义
ID = 
SECRET = 
SECRET1 = 
AGENTID =
APPID = 

############################# 获取token ###############################
def get_token(_secret):
	url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}".format(ID,_secret)
	headers = {'Content-Type': 'application/json'}
	body = {}
	response = requests.post(url, data = json.dumps(body), headers = headers)
	res = (dict(json.loads(response.text))).get("access_token")
	#print("token: "+res)
	return res

############################# 获取部门列表 ###############################
def get_simplelist_all(_token,_department_id):
	url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={}&id={}".format(_token,_department_id)
	headers = {'Content-Type': 'application/json'}
	body = {}
	response = requests.post(url, data = json.dumps(body), headers = headers)
	res = dict(json.loads(response.text))
	#print(res)
	return res

############################# 获取子部门ID列表 ###############################
def get_simplelist(_token,_department_id):
	url = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token={}&id={}".format(_token,_department_id)
	headers = {'Content-Type': 'application/json'}
	body = {}
	response = requests.post(url, data = json.dumps(body), headers = headers)
	res = dict(json.loads(response.text))
	#print(res)
	return res.get("department_id")

############################# 创建部门 ###############################	
def create_department(_token,_name,_department_id):
	url = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token={}&id={}".format(_token,_department_id)
	headers = {'Content-Type': 'application/json'}
	body = {}
	response = requests.post(url, data = json.dumps(body), headers = headers)
	res = dict(json.loads(response.text))
	print(res)
	return res

############################# 获取userid ###############################	
def get_user_id(_token,_tele):
	url = "https://qyapi.weixin.qq.com/cgi-bin//user/getuserid?access_token={}".format(_token)
	headers = {'Content-Type': 'application/json'}
	body = {'mobile':_tele}
	response = requests.post(url, data = json.dumps(body), headers = headers)
	res = dict(json.loads(response.text))
	print(res)
	print(res.get("userid"))
	return res.get("userid")

############################# 获取部门成员 ###############################
def get_department_users(_token,_id):
	url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token={}&department_id={}".format(_token,_id)
	headers = {'Content-Type': 'application/json'}
	body = {}
	response = requests.post(url, data = json.dumps(body), headers = headers)
	res = dict(json.loads(response.text)).get("userlist")
	#print(res)
	return res

############################# 获取全部成员 ###############################	
def get_all_user(_token):
	# 遍历部门 + 成员信息
	dList = get_simplelist(token,'')
	for d in dList:
		dId = d.get('id')
		dUsers = get_department_users(_token,dId)
		for u in dUsers:
			print(u)
	# 部门详情

############################# 发送消息 ###############################	
def send_msg(_token,_userid):
	url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}&debug=1".format(_token)
	headers = {'Content-Type': 'application/json'}
	body = { 
	"touser" : _userid,
   "agentid" : AGENTID,
   "msgtype" : "miniprogram_notice",
   "miniprogram_notice" : {
        "appid": APPID,
        "title": "营销员奖励通知",
        "description":datetime.datetime.now().strftime("%Y年%m月%d日 %H:%M"),
        "content_item": [
            {
                "key": "购买人",
                "value": "xxx"
            },
            {
                "key": "商品名",
                "value": "xxxxx商品"
            },
            {
                "key": "预计奖励",
                "value": "xxxx元"
            },
        ]
    },
   "enable_id_trans": 0,
   "enable_duplicate_check": 0,
   "duplicate_check_interval": 1800
}
	response = requests.post(url, data = json.dumps(body), headers = headers)
	res = dict(json.loads(response.text))
	print(res)

##############################################################################

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 通讯录管理系统的源代码通常会包含多个模块,用于实现联系人数据的存储、检索、添加、删除和编辑等功能。由于这是一个相对复杂的项目,完整的源代码在这里展示可能不合适,但我可以为你概述一个简单的结构和关键部分。 1. **基本结构**: - 数据模型(Contact.py): 定义一个`Contact`类,包含属性如姓名、电话、电子邮件等。 - 数据存储(Database.py): 使用文件或数据库系统(如SQLite、MySQL或更高级的ORM库如SQLAlchemy)处理联系人的持久化。 - 主界面(main.py或cli.py): 用户交互界面,使用命令行或图形界面库(如Tkinter或PyQt)来接收用户输入。 2. **关键模块**: - **添加/编辑联系人**: 接收用户输入并创建新实例,更新已有实例。 - **查询联系人**: 搜索数据库,根据关键词或ID返回匹配的联系人信息。 - **显示列表**: 展示所有联系人、按名称排序或筛选特定条件。 - **删除联系人**: 根据用户选择删除某个联系人。 3. **示例代码片段**: ```python class Contact: def __init__(self, name, phone, email): self.name = name self.phone = phone self.email = email def add_contact(database, contact_data): new_contact = Contact(*contact_data) database.append(new_contact) def search_contact(database, query): return [c for c in database if query.lower() in c.name.lower()] # 假设使用文件数据库 def save_to_file(database, filename): with open(filename, 'w') as f: for contact in database: f.write(f"{contact.name},{contact.phone},{contact.email}\n") # 主程序入口 if __name__ == "__main__": database = [] # 初始空数据库 main_menu(database) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值