• Как при помощи Powershell отправить файл своему боту в Telegram Bot API?

    @Danil97 Автор вопроса
    MaxKozlov, отлично, все работает. Спасибо больше
  • Как при помощи Powershell отправить файл своему боту в Telegram Bot API?

    @Danil97 Автор вопроса
    Сделал так
    $FilePath = 'D:\1.txt';
    $URL = 'https://api.telegram.org/botХХХХ/sendDocument?chat_id=ХХХХ&document';
    
    $fileBytes = [System.IO.File]::ReadAllBytes($FilePath);
    $fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
    $boundary = [System.Guid]::NewGuid().ToString(); 
    $LF = "`r`n";
    
    $bodyLines = ( 
        "--$boundary",
        "Content-Disposition: form-data; name=`"file`"; filename=`"1.txt`"",
        "Content-Type: application/octet-stream$LF",
        $fileEnc,
        "--$boundary--$LF" 
    ) -join $LF
    
    Invoke-RestMethod -Uri $URL -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines

    Получаю ошибку:
    Invoke-RestMethod : {"ok":false,"error_code":400,"description":"Bad Request: there is no document in the request"}
    строка:18 знак:1
    + Invoke-RestMethod -Uri $URL -Method Post -ContentType "multipart/form ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
        + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

    Версия PS 5.1