59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
|
||
|
|
from yuxi.channel.extensions.ringcentral.sdk import AsyncRingCentralClient
|
||
|
|
from yuxi.channel.extensions.ringcentral.types import ResolvedRingCentralAccount
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
DEFAULT_SUBSCRIPTION_EXPIRES = 86400 * 7
|
||
|
|
|
||
|
|
|
||
|
|
async def create_subscription(
|
||
|
|
client: AsyncRingCentralClient,
|
||
|
|
account: ResolvedRingCentralAccount,
|
||
|
|
webhook_url: str,
|
||
|
|
expires_in: int = DEFAULT_SUBSCRIPTION_EXPIRES,
|
||
|
|
) -> dict:
|
||
|
|
payload = {
|
||
|
|
"eventFilters": [
|
||
|
|
"/restapi/v1.0/glip/posts",
|
||
|
|
"/restapi/v1.0/glip/groups",
|
||
|
|
],
|
||
|
|
"expiresIn": expires_in,
|
||
|
|
"deliveryMode": {
|
||
|
|
"transportType": "WebHook",
|
||
|
|
"address": webhook_url,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
if not any("adaptive-cards" in f for f in payload["eventFilters"]):
|
||
|
|
payload["eventFilters"].append("/team-messaging/v1/adaptive-cards/action")
|
||
|
|
|
||
|
|
result = await client.post("/restapi/v1.0/subscription", body=payload)
|
||
|
|
logger.info("RingCentral subscription created: id=%s", result.get("id"))
|
||
|
|
return result
|
||
|
|
|
||
|
|
|
||
|
|
async def renew_subscription(
|
||
|
|
client: AsyncRingCentralClient,
|
||
|
|
subscription_id: str,
|
||
|
|
expires_in: int = DEFAULT_SUBSCRIPTION_EXPIRES,
|
||
|
|
) -> dict:
|
||
|
|
result = await client.put(
|
||
|
|
f"/restapi/v1.0/subscription/{subscription_id}",
|
||
|
|
body={"expiresIn": expires_in},
|
||
|
|
)
|
||
|
|
logger.info("RingCentral subscription renewed: id=%s", subscription_id)
|
||
|
|
return result
|
||
|
|
|
||
|
|
|
||
|
|
async def delete_subscription(client: AsyncRingCentralClient, subscription_id: str) -> None:
|
||
|
|
await client.delete(f"/restapi/v1.0/subscription/{subscription_id}")
|
||
|
|
logger.info("RingCentral subscription deleted: id=%s", subscription_id)
|
||
|
|
|
||
|
|
|
||
|
|
async def get_subscription(client: AsyncRingCentralClient, subscription_id: str) -> dict:
|
||
|
|
return await client.get(f"/restapi/v1.0/subscription/{subscription_id}")
|