企业微信应用号-发送图片消息
wework_app_send_imageSend image notifications to WeWork members using a URL. Optionally target specific users or send to all.
Instructions
通过企业微信应用号发送发送图片消息
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | 图片URL | |
| touser | No | 接收消息的成员ID,多个用`|`分隔,为`@all`时向该企业应用全部成员发送,默认从环境变量获取 |
Implementation Reference
- mcp_notify/wework.py:124-132 (handler)Tool handler for wework_app_send_image. Downloads the image from the provided URL and delegates to the wework_send_media helper with msgtype='image'.
@mcp.tool( title="企业微信应用号-发送图片消息", description="通过企业微信应用号发送发送图片消息", ) def wework_app_send_image( url: str = Field(description="图片URL"), touser: str = FIELD_TO_USER, ): return wework_send_media(touser, url, "image") - mcp_notify/wework.py:164-197 (helper)Helper function that downloads media from a URL, uploads it to WeWork's media/cgi-bin/media/upload API, then sends the media_id as a message to the WeWork app. Used by wework_app_send_image (and other media-sending tools).
def wework_send_media(touser, url: str, msgtype=None): if msgtype: pass elif '.jpg' in url.lower() or '.jpeg' in url.lower() or '.png' in url.lower(): msgtype = 'image' elif '.mp4' in url.lower(): msgtype = 'video' elif '.arm' in url.lower(): msgtype = 'voice' else: msgtype = 'file' res = requests.get(url, timeout=120) res.raise_for_status() file = io.BytesIO(res.content) mine = res.headers.get("content-type") or "application/octet-stream" res = requests.post( f"{WEWORK_BASE_URL}/cgi-bin/media/upload", params={"type": msgtype, "access_token": get_access_token()}, files={"media": ("filename", file, mine)}, timeout=120, ) media = res.json() or {} if not (media_id := media.get("media_id")): return media res = requests.post( f"{WEWORK_BASE_URL}/cgi-bin/message/send?access_token={get_access_token()}", json={ "touser": touser or WEWORK_APP_TOUSER, "agentid": WEWORK_APP_AGENTID, "msgtype": msgtype, msgtype: {"media_id": media_id}, }, ) return res.json() - mcp_notify/wework.py:128-131 (schema)Input schema for wework_app_send_image: requires a 'url' (image URL) and optional 'touser' (recipient, defaults to env var WEWORK_APP_TOUSER).
def wework_app_send_image( url: str = Field(description="图片URL"), touser: str = FIELD_TO_USER, ): - mcp_notify/__init__.py:19-19 (registration)Registration of the wework module's tools (including wework_app_send_image) via wework.add_tools(mcp) called from __init__.py. The @mcp.tool decorator inside add_tools() registers it with FastMCP.
wework.add_tools(mcp) - mcp_notify/wework.py:92-99 (helper)Helper that obtains and caches the WeWork API access token (1-hour TTL) used by wework_send_media and the app tools.
@cached(TTLCache(maxsize=1, ttl=3600)) def get_access_token(): res = requests.get( f"{WEWORK_BASE_URL}/cgi-bin/gettoken", params={"corpid": WEWORK_APP_CORPID, "corpsecret": WEWORK_APP_SECRET}, timeout=60, ) return res.json().get("access_token")