# 发送图片到钉钉群聊 $ErrorActionPreference = "Stop" # 钉钉配置 $APP_KEY = "ding4ursdp0l2giat4bj" $APP_SECRET = "J0gBicjKiIHoKla7WfKKhRs1Tv8L6Xd5UhW3EVQByF16G7Vn7UUcRhP6u-PBCQNo" $OPEN_CONVERSATION_ID = "cidcjYshXVtKck5LfOO9AqOJg==" $ROBOT_CODE = "4293382733" $IMAGE_PATH = "C:\Users\ALC\.openclaw\media\browser\1ad3cfc9-2dd5-496b-9fa5-26d23b973f76.jpg" Write-Host "====================================" -ForegroundColor Cyan Write-Host "开始发送图片到钉钉群聊" -ForegroundColor Cyan Write-Host "====================================" -ForegroundColor Cyan # 步骤 1: 获取 Access Token Write-Host "`n步骤 1: 获取 Access Token..." -ForegroundColor Yellow $tokenUrl = "https://api.dingtalk.com/v1.0/oauth2/accessToken" $tokenHeaders = @{ "Content-Type" = "application/json" } $tokenBody = @{ appKey = $APP_KEY appSecret = $APP_SECRET } | ConvertTo-Json try { $tokenResponse = Invoke-RestMethod -Uri $tokenUrl -Method Post -Headers $tokenHeaders -Body $tokenBody $accessToken = $tokenResponse.accessToken if ($accessToken) { Write-Host "✅ Access Token 获取成功" -ForegroundColor Green } else { Write-Host "❌ 获取 Access Token 失败" -ForegroundColor Red exit 1 } } catch { Write-Host "❌ 获取 Access Token 异常: $($_.Exception.Message)" -ForegroundColor Red exit 1 } # 步骤 2: 上传媒体文件 Write-Host "`n步骤 2: 上传媒体文件..." -ForegroundColor Yellow if (-not (Test-Path $IMAGE_PATH)) { Write-Host "❌ 文件不存在: $IMAGE_PATH" -ForegroundColor Red exit 1 } $fileSize = (Get-Item $IMAGE_PATH).Length Write-Host "📁 文件大小: $fileSize bytes ($($fileSize / 1MB).ToString('F2')) MB" -ForegroundColor Cyan $uploadUrl = "https://api.dingtalk.com/v1.0/media/upload" $uploadHeaders = @{ "x-acs-dingtalk-access-token" = $accessToken } $FileStream = [System.IO.FileStream]::new($IMAGE_PATH, [System.IO.FileMode]::Open) $FileContent = [System.IO.FileStream]::new($IMAGE_PATH, [System.IO.FileMode]::Open) # 准备 multipart/form-data $boundary = [System.Guid]::NewGuid().ToString() $LF = "`r`n" $bodyLines = @() # 添加 file 字段 $bodyLines += "--$boundary" $bodyLines += "Content-Disposition: form-data; name=`"file`"; filename=`"$([System.IO.Path]::GetFileName($IMAGE_PATH))`"" $bodyLines += "Content-Type: application/octet-stream" $bodyLines += "" $bodyLines += [System.IO.File]::ReadAllBytes($IMAGE_PATH) $bodyLines += "--$boundary--" try { $uploadHeaders.Add("Content-Type", "multipart/form-data; boundary=$boundary") $body = $bodyLines -join $LF $uploadResponse = Invoke-RestMethod -Uri "$uploadUrl?type=image" -Method Post -Headers $uploadHeaders -Body $body $mediaId = $uploadResponse.mediaId if ($mediaId) { Write-Host "✅ 文件上传成功,mediaId: $mediaId" -ForegroundColor Green } else { Write-Host "❌ 上传失败" -ForegroundColor Red exit 1 } } catch { Write-Host "❌ 上传异常: $($_.Exception.Message)" -ForegroundColor Red exit 1 } finally { $FileStream.Dispose() $FileContent.Dispose() } # 步骤 3: 发送媒体消息到群聊 Write-Host "`n步骤 3: 发送媒体消息到群聊..." -ForegroundColor Yellow $sendUrl = "https://api.dingtalk.com/v1.0/robot/orgGroupSend" $sendHeaders = @{ "x-acs-dingtalk-access-token" = $accessToken "Content-Type" = "application/json" } $sendBody = @{ openConversationId = $OPEN_CONVERSATION_ID robotCode = $ROBOT_CODE msgKey = "sampleImage" msgParam = @{ mediaId = $mediaId altText = "日本 Yahoo 首页截图" } | ConvertTo-Json -Depth 3 } | ConvertTo-Json -Depth 3 try { $sendResponse = Invoke-RestMethod -Uri $sendUrl -Method Post -Headers $sendHeaders -Body $sendBody if ($sendResponse.processQueryKey) { Write-Host "✅ 消息发送成功!processQueryKey: $($sendResponse.processQueryKey)" -ForegroundColor Green } else { Write-Host "❌ 发送失败" -ForegroundColor Red exit 1 } } catch { Write-Host "❌ 发送异常: $($_.Exception.Message)" -ForegroundColor Red Write-Host "响应内容: $($_.ErrorDetails.Message)" -ForegroundColor Red exit 1 } Write-Host "`n====================================" -ForegroundColor Green Write-Host "✅ 发送成功!" -ForegroundColor Green Write-Host "====================================" -ForegroundColor Green