新增企业微信、微博、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
177 lines
4.8 KiB
Python
177 lines
4.8 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
import httpx
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
CARD_UPDATE_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/update_template_card"
|
|
|
|
|
|
def build_text_notice_card(
|
|
title: str,
|
|
description: str = "",
|
|
url: str = "",
|
|
emphasis_title: str = "",
|
|
sub_title_text: str = "",
|
|
horizontal_content_list: list[dict] | None = None,
|
|
jump_list: list[dict] | None = None,
|
|
card_action: dict | None = None,
|
|
) -> dict:
|
|
card: dict = {
|
|
"card_type": "text_notice",
|
|
"main_title": {"title": title},
|
|
}
|
|
if description:
|
|
card["main_title"]["desc"] = description
|
|
if emphasis_title:
|
|
card["emphasis_content"] = {"title": emphasis_title}
|
|
if sub_title_text:
|
|
card["sub_title_text"] = sub_title_text
|
|
if horizontal_content_list:
|
|
card["horizontal_content_list"] = horizontal_content_list
|
|
if jump_list:
|
|
card["jump_list"] = jump_list
|
|
if card_action:
|
|
card["card_action"] = card_action
|
|
return card
|
|
|
|
|
|
def build_news_notice_card(
|
|
title: str,
|
|
description: str = "",
|
|
url: str = "",
|
|
pic_url: str = "",
|
|
image_text_area: dict | None = None,
|
|
vertical_content_list: list[dict] | None = None,
|
|
horizontal_content_list: list[dict] | None = None,
|
|
jump_list: list[dict] | None = None,
|
|
card_action: dict | None = None,
|
|
) -> dict:
|
|
card: dict = {
|
|
"card_type": "news_notice",
|
|
"main_title": {"title": title},
|
|
}
|
|
if description:
|
|
card["main_title"]["desc"] = description
|
|
if image_text_area:
|
|
card["image_text_area"] = image_text_area
|
|
if vertical_content_list:
|
|
card["vertical_content_list"] = vertical_content_list
|
|
if horizontal_content_list:
|
|
card["horizontal_content_list"] = horizontal_content_list
|
|
if jump_list:
|
|
card["jump_list"] = jump_list
|
|
if card_action:
|
|
card["card_action"] = card_action
|
|
return card
|
|
|
|
|
|
def build_button_interaction_card(
|
|
title: str,
|
|
description: str = "",
|
|
task_id: str = "",
|
|
buttons: list[dict] | None = None,
|
|
button_list: list[dict] | None = None,
|
|
horizontal_content_list: list[dict] | None = None,
|
|
card_action: dict | None = None,
|
|
) -> dict:
|
|
card: dict = {
|
|
"card_type": "button_interaction",
|
|
"main_title": {"title": title},
|
|
}
|
|
if description:
|
|
card["main_title"]["desc"] = description
|
|
if task_id:
|
|
card["task_id"] = task_id
|
|
if buttons:
|
|
card["button_selection"] = {"question_key": "action", "option_list": buttons}
|
|
if button_list:
|
|
card["button_list"] = button_list
|
|
if horizontal_content_list:
|
|
card["horizontal_content_list"] = horizontal_content_list
|
|
if card_action:
|
|
card["card_action"] = card_action
|
|
return card
|
|
|
|
|
|
def build_vote_interaction_card(
|
|
title: str,
|
|
description: str = "",
|
|
task_id: str = "",
|
|
checkbox: dict | None = None,
|
|
submit_button: dict | None = None,
|
|
) -> dict:
|
|
card: dict = {
|
|
"card_type": "vote_interaction",
|
|
"main_title": {"title": title},
|
|
}
|
|
if description:
|
|
card["main_title"]["desc"] = description
|
|
if task_id:
|
|
card["task_id"] = task_id
|
|
if checkbox:
|
|
card["checkbox"] = checkbox
|
|
if submit_button:
|
|
card["submit_button"] = submit_button
|
|
return card
|
|
|
|
|
|
def build_multiple_interaction_card(
|
|
title: str,
|
|
description: str = "",
|
|
task_id: str = "",
|
|
select_list: list[dict] | None = None,
|
|
submit_button: dict | None = None,
|
|
) -> dict:
|
|
card: dict = {
|
|
"card_type": "multiple_interaction",
|
|
"main_title": {"title": title},
|
|
}
|
|
if description:
|
|
card["main_title"]["desc"] = description
|
|
if task_id:
|
|
card["task_id"] = task_id
|
|
if select_list:
|
|
card["select_list"] = select_list
|
|
if submit_button:
|
|
card["submit_button"] = submit_button
|
|
return card
|
|
|
|
|
|
async def update_template_card(
|
|
http_client: httpx.AsyncClient,
|
|
access_token: str,
|
|
agent_id: int,
|
|
userids: list[str] | None = None,
|
|
partyids: list[int] | None = None,
|
|
tagids: list[int] | None = None,
|
|
response_code: str = "",
|
|
button: dict | None = None,
|
|
template_card: dict | None = None,
|
|
) -> bool:
|
|
payload: dict = {
|
|
"agentid": agent_id,
|
|
"response_code": response_code,
|
|
}
|
|
if userids:
|
|
payload["userids"] = userids
|
|
if partyids:
|
|
payload["partyids"] = partyids
|
|
if tagids:
|
|
payload["tagids"] = tagids
|
|
if button:
|
|
payload["button"] = button
|
|
if template_card:
|
|
payload["template_card"] = template_card
|
|
|
|
url = f"{CARD_UPDATE_URL}?access_token={access_token}"
|
|
try:
|
|
resp = await http_client.post(url, json=payload)
|
|
data = resp.json()
|
|
return data.get("errcode") == 0
|
|
except Exception:
|
|
logger.exception("WeCom update_template_card failed")
|
|
return False
|