skip to Main Content

sendDocument requires 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

But i got Error:400. How to properly send file?

2

Answers


  1. I don’t know much about it, but I recently read a bit on Telegram and I know there’s a module called PoShGram that allows you to interact with it. You can find it here

    At a glance looks like it may have the functionality you need. The read me says:

    The goal of this project to abstract that complexity away in favor of
    simple and direct PowerShell commands.

    Login or Signup to reply.
  2. Sending a file with a Telegram bot via PowerShell requires some more work.
    This is an example of how to send a text document:

    $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 $_
    }
    

    Another option I found is to make use of the Form param in PowerShell v6.1 or higher (download it from GitHub if needed); I found this raw example 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search