sendDocument需要 multipart / form-data 。
$path = 'C:\file.txt'
$userId = 12345
$token = "ABC123"
$url = "https://api.telegram.org/-/sendDocument?chat_id=+"
[net.servicepointmanager]::securityprotocol = 'ssl3,tls,tls11,tls12'
$url = $url.Replace("-",$token).Replace("+",$userId)
$Response = Iwr -Uri $url -Method Post -InFile $path -ContentType "multipart/form-data"
$Response.Content
但是我收到了错误:400 。如何正确发送文件?
最佳答案
通过PowerShell用Telegram机器人发送文件需要做更多的工作。
这是如何发送文本文档的示例:
$payload = @{
chat_id = $ChatID
document = $FileURL
caption = $Caption
parse_mode = $ParseMode
disable_notification = $DisableNotification.IsPresent
}
$invokeRestMethodSplat = @{
Uri = ("https://api.telegram.org/bot{0}/sendDocument" -f $BotToken)
Body = (ConvertTo-Json -Compress -InputObject $payload)
ErrorAction = 'Stop'
ContentType = "application/json"
Method = 'Post'
}
try {
Invoke-RestMethod @invokeRestMethodSplat
}
catch {
Write-Error $_
}
我发现的另一个选择是在PowerShell v6.1或更高版本中使用Form
参数(如果需要,可以从GitHub上使用download);我找到了这个原始示例here:$doc = "C:\something.txt"
$Uri = "https://api.telegram.org/bot$($BotToken)/sendDocument"
#Build the Form
$Form = @{
chat_id = $chatID
document = Get-Item $doc
}
Invoke-RestMethod -Uri $uri -Form $Form -Method Post
关于.net - 通过Telegram机器人发送sendDocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64563264/