updaet
This commit is contained in:
parent
8c432a606b
commit
8472d79279
@ -123,8 +123,12 @@ async def wechat_corp_callback(
|
|||||||
chat_id = msg_root.find('ChatId').text
|
chat_id = msg_root.find('ChatId').text
|
||||||
change_type = msg_root.find('ChangeType').text
|
change_type = msg_root.find('ChangeType').text
|
||||||
|
|
||||||
# if change_type == 'add_member':
|
# 处理进群事件
|
||||||
# # 获取群聊成员
|
if change_type == 'add_member':
|
||||||
|
userid = msg_root.find('UserId').text
|
||||||
|
unionid = await wecom_client.get_unionid_from_userid(userid)
|
||||||
|
print(f"根据userid获取unionid结果: {unionid}")
|
||||||
|
|
||||||
|
|
||||||
return Response(content="success", media_type="text/plain")
|
return Response(content="success", media_type="text/plain")
|
||||||
|
|
||||||
|
|||||||
@ -71,19 +71,42 @@ class WecomClient:
|
|||||||
logging.error(f"code2session异常: {str(e)}")
|
logging.error(f"code2session异常: {str(e)}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
async def get_unionid_from_userid(self, userid: str) -> Optional[str]:
|
||||||
|
"""根据userid获取unionid"""
|
||||||
|
try:
|
||||||
|
access_token = await self.get_access_token()
|
||||||
|
if not access_token:
|
||||||
|
raise Exception("获取access_token失败")
|
||||||
|
|
||||||
|
url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get"
|
||||||
|
params = {
|
||||||
|
"access_token": access_token,
|
||||||
|
"external_userid": userid
|
||||||
|
}
|
||||||
|
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(url, params=params) as response:
|
||||||
|
result = await response.json()
|
||||||
|
|
||||||
|
return result.get("unionid")
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"get_unionid_from_userid异常: {str(e)}")
|
||||||
|
return None
|
||||||
|
|
||||||
async def unionid_to_external_userid(self, unionid: str, openid: str) -> Optional[str]:
|
async def unionid_to_external_userid(self, unionid: str, openid: str) -> Optional[str]:
|
||||||
"""根据unionid获取external_userid"""
|
"""根据unionid获取external_userid"""
|
||||||
try:
|
try:
|
||||||
url = f"https://qyapi.weixin.qq.com/cgi-bin/corpgroup/unionid_to_external_userid?access_token={await self.get_access_token()}"
|
url = f"https://qyapi.weixin.qq.com/cgi-bin/idconvert/unionid_to_external_userid?access_token={await self.get_access_token()}"
|
||||||
params = {
|
params = {
|
||||||
"unionid": unionid,
|
"unionid": unionid,
|
||||||
"openid": openid,
|
"openid": openid,
|
||||||
"corpid": self.corp_id
|
"subject_type": 1
|
||||||
}
|
}
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.post(url, json=params) as response:
|
async with session.post(url, json=params) as response:
|
||||||
result = await response.json()
|
result = await response.json()
|
||||||
|
|
||||||
return result
|
return result
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"unionid_to_external_userid异常: {str(e)}")
|
logging.error(f"unionid_to_external_userid异常: {str(e)}")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user