编程语言
首页 > 编程语言> > 微信企业会话存档SDK接口封装(Python示例)

微信企业会话存档SDK接口封装(Python示例)

作者:互联网

Windows

Linux

Python调用方式 (windows)

import base64
from ctypes import *
import json
import sys

from Crypto import Random
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.PublicKey import RSA

pysdk = cdll.LoadLibrary('./pywechat.dll')

# void free_p(void* p)
# int  init_sdk(char* corpid, char* key)
# char* get_chat_data(int seq, int limit, char* proxy, char* passwd, int timeout, int* datalen)
# char* pull_media_file(char* fileid, char* proxy, char* passwd, int timeout, int datalen)
# char* decrypt_data(char* encrypt_key, char* encrypt_chat_msg, int* datalen)
# void destory_sdk()
pysdk.get_chat_data.restype = c_int64
pysdk.pull_media_file.restype = c_int64
pysdk.decrypt_data.restype = c_int64

corpid = b'企业号'
key = b'会话存档密钥'
seq = c_int(0)
limit = c_int(100)
proxy = b''
passwd = b''
timeout = c_int(10)

corpid_buff = create_string_buffer(corpid, len(corpid)+1)
key_buff = create_string_buffer(key, len(key)+1)
proxy_buff = create_string_buffer(proxy, len(proxy)+1)
passwd_buff = create_string_buffer(passwd, len(passwd)+1)

ret_len = c_int(0)

# 初始化sdk
init_status = pysdk.init_sdk(corpid_buff, key_buff)
print("Init sdk ret_code.", init_status)
if init_status != 0:
	sys.exit(init_status)
# 获取数据
resp = pysdk.get_chat_data(seq, limit, proxy_buff, passwd_buff, timeout, byref(ret_len))

# ret_data_demo = {
# 	"errcode": 0,
# 	"errmsg": "ok",
# 	"chatdata": [{
# 		"seq": 1,
# 		"msgid": "10717693993641340428_1641865486744",
# 		"publickey_ver": 1,
# 		"encrypt_random_key": "随机加密码",
# 		"encrypt_chat_msg": "加密消息内容"
# 	}, {
# 		"seq": 2,
# 		"msgid": "13596831180950397347_1641871046891",
# 		"publickey_ver": 1,
# 		"encrypt_random_key": "随机加密码",
# 		"encrypt_chat_msg": "加密消息内容"
# 	}]
# }
ret_data = json.loads(string_at(resp, ret_len.value))

# 释放内存空间
# pysdk.free_p(resp)
#
datails_data_demo = {"msgid": "13596831180950397347_1641871046891", "action": "send", "from": "发送方",
                     "tolist": ["接收方"], "roomid": "", "msgtime": 1641871046713, "msgtype": "file",
                     "file": {"md5sum": "C328154E2E2D4879443CAF464F1F9E0D", "filename": "Visual Assist X 10.9.2210.rar",
                              "fileext": "rar", "filesize": 35341717,
                              "sdkfileid": "文件序号"}}
random_generator = Random.new().read
assert ret_data.get("errcode") == 0
for chat_data in ret_data.get("chatdata"):
	if 'external' in chat_data.get("msgid"):
		continue
	print(chat_data.get("msgid"))
	# TODO: 判断 publickey_ver
	with open("私钥文件") as f:
		key = f.read()
		rsakey = RSA.importKey(key)
		cipher = Cipher_pkcs1_v1_5.new(rsakey)
		encrypt_key = cipher.decrypt(base64.b64decode(chat_data.get("encrypt_random_key")), None)
		encrypt_msg = bytes(chat_data.get("encrypt_chat_msg"), encoding='utf-8')
		encrypt_key_buff = create_string_buffer(encrypt_key, len(encrypt_key)+1)
		encrypt_msg_buff = create_string_buffer(encrypt_msg, len(encrypt_msg)+1)
		ret_len = c_int(0)
		byte_details = pysdk.decrypt_data(encrypt_key_buff, encrypt_msg_buff, byref(ret_len))
		byte_details = string_at(byte_details, ret_len.value)
		data_details = json.loads(byte_details)
		print(data_details)
		if data_details.get("msgtype") is None:
			print("invalid type error")
			continue
		elif data_details.get("msgtype") == "file":
			fileid = bytes(data_details.get('file').get("sdkfileid"), encoding='utf-8')
			filelen = data_details.get('file').get("filesize")
			file_content = pysdk.pull_media_file(create_string_buffer(fileid, len(fileid)+1), proxy_buff, passwd_buff, timeout, c_int(filelen))
			filename = data_details.get('file').get("filename")
			with open(filename, 'wb') as dstf:
				dstf.write(string_at(file_content, filelen))
				# pysdk.free_p(file_content)
		elif data_details.get("msgtype") == "text":
			print("text message")

pysdk.destory_sdk()
print(1)
sys.exit(0)

标签:encrypt,示例,Python,微信,len,char,int,ret,data
来源: https://blog.csdn.net/u013399150/article/details/122472298