新增企业微信、微博、WhatsApp、Workplace 四个渠道扩展。 企业微信渠道扩展主要模块:config, gateway, webhook, webhook_bot, outbound, streaming, pairing, security, crypto, dedupe, persistent_dedupe, card, directory, events, externalcontact, media, mentions, menu, message, oauth, status 微博渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, passive_reply, broadcast, message, menu, media, subscription, template, status WhatsApp 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, monitor, status Workplace 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, challenge, groups, media, mentions, menu, monitor, persona, quick_reply, signature, subscriptions, template, threading, users, status
64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
import logging
|
|
|
|
import httpx
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
WORKPLACE_API_BASE = "https://graph.facebook.com/v24.0"
|
|
|
|
|
|
async def download_media(media_url: str) -> bytes | None:
|
|
try:
|
|
async with httpx.AsyncClient(timeout=httpx.Timeout(30.0)) as client:
|
|
resp = await client.get(media_url)
|
|
if resp.status_code == 200:
|
|
return resp.content
|
|
logger.warning("Workplace media download failed: %d", resp.status_code)
|
|
return None
|
|
except Exception:
|
|
logger.exception("Workplace media download error")
|
|
return None
|
|
|
|
|
|
async def upload_attachment(
|
|
page_id: str,
|
|
access_token: str,
|
|
file_data: bytes,
|
|
file_name: str,
|
|
media_type: str,
|
|
mime_type: str,
|
|
api_version: str = "v24.0",
|
|
) -> str | None:
|
|
try:
|
|
files = {
|
|
"filedata": (file_name, file_data, mime_type),
|
|
}
|
|
data = {
|
|
"message": str(
|
|
{
|
|
"attachment": {
|
|
"type": media_type,
|
|
"payload": {"is_reusable": True},
|
|
}
|
|
}
|
|
),
|
|
}
|
|
|
|
async with httpx.AsyncClient(timeout=httpx.Timeout(60.0)) as client:
|
|
resp = await client.post(
|
|
f"https://graph.facebook.com/{api_version}/{page_id}/message_attachments",
|
|
params={"access_token": access_token},
|
|
files=files,
|
|
data=data,
|
|
)
|
|
if resp.status_code == 200:
|
|
result = resp.json()
|
|
attachment_id = result.get("attachment_id", "")
|
|
logger.info("Workplace attachment uploaded: %s", attachment_id)
|
|
return attachment_id
|
|
logger.error("Workplace attachment upload failed: %d %s", resp.status_code, resp.text[:200])
|
|
return None
|
|
except Exception:
|
|
logger.exception("Workplace attachment upload error")
|
|
return None
|