42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
|
|
ACCOUNT_ADD_URL = "/cgi-bin/kf/account/add"
|
||
|
|
ACCOUNT_DEL_URL = "/cgi-bin/kf/account/del"
|
||
|
|
ACCOUNT_UPDATE_URL = "/cgi-bin/kf/account/update"
|
||
|
|
ACCOUNT_LIST_URL = "/cgi-bin/kf/account/list"
|
||
|
|
ADD_CONTACT_WAY_URL = "/cgi-bin/kf/add_contact_way"
|
||
|
|
|
||
|
|
|
||
|
|
class WeChatKFAccountManager:
|
||
|
|
def __init__(self, gateway):
|
||
|
|
self._gateway = gateway
|
||
|
|
|
||
|
|
async def add(self, name: str, media_id: str = "") -> dict:
|
||
|
|
payload = {"name": name}
|
||
|
|
if media_id:
|
||
|
|
payload["media_id"] = media_id
|
||
|
|
return await self._post(ACCOUNT_ADD_URL, payload)
|
||
|
|
|
||
|
|
async def delete(self, open_kfid: str) -> dict:
|
||
|
|
return await self._post(ACCOUNT_DEL_URL, {"open_kfid": open_kfid})
|
||
|
|
|
||
|
|
async def update(self, open_kfid: str, name: str = "", media_id: str = "") -> dict:
|
||
|
|
payload = {"open_kfid": open_kfid}
|
||
|
|
if name:
|
||
|
|
payload["name"] = name
|
||
|
|
if media_id:
|
||
|
|
payload["media_id"] = media_id
|
||
|
|
return await self._post(ACCOUNT_UPDATE_URL, payload)
|
||
|
|
|
||
|
|
async def list(self, offset: int = 0, limit: int = 100) -> dict:
|
||
|
|
return await self._post(ACCOUNT_LIST_URL, {"offset": offset, "limit": limit})
|
||
|
|
|
||
|
|
async def add_contact_way(self, open_kfid: str, scene: str = "") -> dict:
|
||
|
|
payload = {"open_kfid": open_kfid}
|
||
|
|
if scene:
|
||
|
|
payload["scene"] = scene
|
||
|
|
return await self._post(ADD_CONTACT_WAY_URL, payload)
|
||
|
|
|
||
|
|
async def _post(self, url: str, payload: dict) -> dict:
|
||
|
|
token = await self._gateway.get_access_token()
|
||
|
|
resp = await self._gateway._http.post(url, params={"access_token": token}, json=payload)
|
||
|
|
return resp.json()
|