如何解决通过Telegram机器人发送sendDocument
look at this post需要多部分/表单数据。
$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 。如何正确发送文件?
解决方法
我对此了解不多,但是最近我在Telegram上读了一些,我知道有一个名为PoShGram的模块可以让您与其进行交互。您可以找到它here
乍一看,它可能具有您需要的功能。读我说:
,该项目的目标是将这种复杂性抽象化,以支持 简单直接的PowerShell命令。
通过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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。