PoshGram.psm1
<#
.Synopsis Evaluates if the provided URL is reachable .DESCRIPTION Evaluates if the provided URL is reachable and returns a boolean based on results .EXAMPLE Confirm-URL -Uri http://gph.is/2y3AWRU Determines if the provided URL is accessible and returns a boolean based on results .PARAMETER Uri Uri you wish to resolve .OUTPUTS System.Boolean .NOTES Author: Jake Morrison - @jakemorrison - http://techthoughts.info/ .COMPONENT PoshGram - https://github.com/techthoughts2/PoshGram #> function Confirm-URL { [CmdletBinding()] param( ## The URI to resolve [Parameter(Mandatory = $true, HelpMessage = 'Uri you wish to resolve')] [string]$Uri ) $result = $true #assume the best [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Write-Verbose -Message "Attempting to confirm $Uri" try { Invoke-WebRequest -Uri $uri -UseBasicParsing -DisableKeepAlive -ErrorAction Stop | Out-Null Write-Verbose -Message "Confirmed." }#try_Invoke-WebRequest catch { Write-Verbose -Message "Catch on Invoke-WebRequest. This is not neccessarily a bad thing. Checking status code." if ([int]$_.Exception.Response.StatusCode -eq 0) { Write-Warning -Message "$Uri" Write-Warning -Message 'The URL provided does not appear to be valid' $result = $false } #we will proceed with any other exit code }#catch_Invoke-WebRequest return $result }#Confirm-URL <# .Synopsis Resolve a URI to the URIs it redirects to .DESCRIPTION Resolves a URI provided to the URI it redirects to. If no redirect is in place, null is returned. This is useful for resolving shortlinks to try url file paths. .EXAMPLE Resolve-ShortLink -Uri http://gph.is/2y3AWRU Resolve shortlink to full URI .PARAMETER Uri Uri you wish to resolve .OUTPUTS System.String -or- Null .NOTES Author: Jake Morrison - @jakemorrison - http://techthoughts.info/ .COMPONENT PoshGram - https://github.com/techthoughts2/PoshGram #> function Resolve-ShortLink { [CmdletBinding()] param( ## The URI to resolve [Parameter(Mandatory = $true, HelpMessage = 'Uri you wish to resolve')] [string]$Uri ) $result = $null $a = $null [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 try { $a = Invoke-WebRequest -Uri $uri -MaximumRedirection 0 -ErrorAction Stop }#try_Invoke-WebRequest catch { #if($_.ErrorDetails.Message -like "*maximum redirection*"){ if ($_.Exception.Message -like "*Moved*") { $a = $_ Write-Verbose -Message 'Moved detected.' #$result = $a.Headers.Location $result = $a.Exception.Response.Headers.Location.AbsoluteUri }#if_Error_Moved else { Write-Warning -Message 'An Error was encountered resolving a potential shortlink:' Write-Error $_ }#else_Error_Moved }#catch_Invoke-WebRequest return $result }#Resolve-ShortLink <# .Synopsis Verifies that specified file is a supported Telegram extension type .DESCRIPTION Evaluates the specified file path to determine if the file is a supported extension type .EXAMPLE Test-FileExtension -FilePath C:\photos\aphoto.jpg -Type Photo Verifies if the path specified is a supported photo extension type .EXAMPLE Test-FileExtension -FilePath $PhotoPath -Type Photo -Verbose Verifies if the path specified in $PhotoPath is a supported photo extension type with verbose output .EXAMPLE $fileTypeEval = Test-FileExtension -FilePath $AnimationPath -Type Animation Verifies if the path specified is a supported Animation extension type .EXAMPLE $fileTypeEval = Test-FileExtension -FilePath $Audio -Type Audio Verifies if the path specified is a supported Audio extension type .EXAMPLE $fileTypeEval = Test-FileExtension -FilePath $PhotoPath -Type Photo Verifies if the path specified is a supported photo extension type .EXAMPLE $fileTypeEval = Test-FileExtension -FilePath $Video -Type Video Verifies if the path specified is a supported Video extension type .PARAMETER FilePath Path to file that will be evaluated .PARAMETER Type Telegram message type .OUTPUTS System.Boolean .NOTES Author: Jake Morrison - @jakemorrison - http://techthoughts.info/ .COMPONENT PoshGram - https://github.com/techthoughts2/PoshGram #> function Test-FileExtension { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = 'Path to file that will be evaluated')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$FilePath, [Parameter(Mandatory = $true, HelpMessage = 'Telegram message type')] [ValidateSet('Photo', 'Video', 'Audio', 'Animation')] [string]$Type ) #------------------------------------------------------------ $supportedPhotoExtensions = @( 'JPG', 'JPEG', 'PNG', 'GIF', 'BMP', 'WEBP', 'SVG', 'TIFF' ) $supportedVideoExtensions = @( 'MP4' ) $supportedAudioExtensions = @( 'MP3' ) $supportedAnimationExtensions = @( 'GIF' ) switch ($Type) { Photo { $extType = $supportedPhotoExtensions }#photo Video { $extType = $supportedVideoExtensions }#video Audio { $extType = $supportedAudioExtensions }#audio Animation { $extType = $supportedAnimationExtensions }#animation }#switch_Type Write-Verbose -Message "Validating type: $Type" #------------------------------------------------------------ [bool]$results = $true #assume the best. #------------------------------------------------------------ Write-Verbose -Message "Processing $FilePath ..." $divide = $FilePath.Split(".") $rawExtension = $divide[$divide.Length - 1] $extension = $rawExtension.ToUpper() Write-Verbose "Verifying discovered extension: $extension" switch ($extension) { {$extType -contains $_} { Write-Verbose -Message "Extension verified." } default { Write-Warning -Message "The specified file is not a supported $Type extension." $results = $false }#default }#switch_extension return $results }#function_Test-FileExtension <# .Synopsis Verifies that file size is supported by Telegram .DESCRIPTION Evaluates if the file is at or below the supported Telegram file size .EXAMPLE Test-FileSize -Path C:\videos\video.mp4 Verifies if the path specified is a supported video extension type .EXAMPLE Test-FileSize -Path $Path -Verbose Verifies if the path specified in $VideoPath is a supported video extension type with verbose output .PARAMETER Path Path to file .OUTPUTS System.Boolean .NOTES Author: Jake Morrison - @jakemorrison - http://techthoughts.info/ Telegram currently supports a 50MB file size for bots .COMPONENT PoshGram - https://github.com/techthoughts2/PoshGram #> function Test-FileSize { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = 'Path to file')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$Path ) $results = $true #assume the best $supportedSize = 50 try { $size = Get-ChildItem -Path $Path -ErrorAction Stop if (($size.Length / 1MB) -gt $supportedSize) { Write-Warning -Message "The file is over $supportedSize (MB)" $results = $false } } catch { Write-Warning -Message "An error was encountered evaluating the file size" $results = $false } return $results }#function_Test-FileSize <# .Synopsis Verifies that specified URL path contains a supported Telegram extension type .DESCRIPTION Evaluates the specified URL path to determine if the URL leads to a supported extension type .EXAMPLE Test-URLExtension -URL $URL -Type Photo Verifies if the URL path specified is a supported photo extension type .EXAMPLE Test-URLExtension -URL $PhotoURL -Type Photo -Verbose Verifies if the URL path specified is a supported photo extension type with Verbose output .EXAMPLE Test-URLExtension -URL $VideoURL -Type Video Verifies if the URL path specified is a supported video extension type .EXAMPLE Test-URLExtension -URL $AudioURL -Type Audio Verifies if the URL path specified is a supported audio extension type .EXAMPLE Test-URLExtension -URL $AnimationURL -Type Animation Verifies if the URL path specified is a supported animation extension type .EXAMPLE Test-URLExtension -URL $DocumentURL -Type Document Verifies if the URL path specified is a supported document extension type .PARAMETER URL The URL string to the specified online file .OUTPUTS System.Boolean .NOTES Author: Jake Morrison - @jakemorrison - http://techthoughts.info/ .COMPONENT PoshGram - https://github.com/techthoughts2/PoshGram #> function Test-URLExtension { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = 'URL string of document')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$URL, [Parameter(Mandatory = $true, HelpMessage = 'Telegram message type')] [ValidateSet('Photo', 'Video', 'Audio', 'Animation', 'Document')] [string]$Type ) #------------------------------------------------------------ $supportedPhotoExtensions = @( 'JPG', 'JPEG', 'PNG', 'GIF', 'BMP', 'WEBP', 'SVG', 'TIFF' ) $supportedVideoExtensions = @( 'MP4' ) $supportedAudioExtensions = @( 'MP3' ) $supportedAnimationExtensions = @( 'GIF' ) $supportedDocumentExtensions = @( 'PDF', 'GIF', 'ZIP' ) switch ($Type) { Photo { $extType = $supportedPhotoExtensions }#photo Video { $extType = $supportedVideoExtensions }#video Audio { $extType = $supportedAudioExtensions }#audio Animation { $extType = $supportedAnimationExtensions }#animation Document { $extType = $supportedDocumentExtensions }#document }#switch_Type Write-Verbose -Message "Validating type: $Type" #------------------------------------------------------------ [bool]$results = $true #assume the best. #------------------------------------------------------------ Write-Verbose -Message "Testing provided URL" $urlEval = Confirm-URL -Uri $URL if ($urlEval -ne $true) { Write-Verbose -Message 'URL Confirmation did not return true.' $results = $false return $results }#if_urlEval #------------------------------------------------------------ Write-Verbose -Message "Resolving potential shortlink..." $slEval = Resolve-ShortLink -Uri $URL -ErrorAction SilentlyContinue if ($slEval) { $URL = $slEval }#if_slEval #------------------------------------------------------------ Write-Verbose -Message "Processing $URL ..." $divide = $URL.Split(".") $rawExtension = $divide[$divide.Length - 1] $extension = $rawExtension.ToUpper() Write-Verbose "Verifying discovered extension: $extension" switch ($extension) { {$extType -contains $_} { Write-Verbose -Message "Extension verified." } default { Write-Warning -Message "The specified file is not a supported $Type extension." $results = $false }#default }#switch_extension #------------------------------------------------------------ return $results }#function_Test-URLExtension <# .Synopsis Verifies that specified URL file size is supported by Telegram .DESCRIPTION Evaluates the specified URL path to determine if the file is at or below the supported Telegram file size .EXAMPLE Test-URLFileSize -URL "https://github.com/techthoughts2/PoshGram/raw/master/test/SourceFiles/techthoughts.png" Verifies if the file in the specified URL is at or below the Telegram maximum size .EXAMPLE Test-URLFileSize -URL $URL -Verbose Verifies if the file in the specified URL is at or below the Telegram maximum size with verbose output .PARAMETER URL URL address to file .OUTPUTS System.Boolean .NOTES Author: Jake Morrison - @jakemorrison - http://techthoughts.info/ Telegram currently supports a 50MB file size for bots .COMPONENT PoshGram - https://github.com/techthoughts2/PoshGram #> function Test-URLFileSize { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = 'URL address to file')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$URL ) $results = $true #assume the best $supportedSize = 50 try { $urlFileInfo = Invoke-WebRequest $URL -ErrorAction Stop if (($urlFileInfo.RawContentLength / 1MB) -gt $supportedSize) { Write-Warning -Message "The file is over $supportedSize (MB)" $results = $false }#if_size }#try_Invoke-WebRequest catch { Write-Warning -Message "An error was encountered evaluating the file size" $results = $false }#catch_Invoke-WebRequest return $results }#function_Test-URLFileSize <# .EXTERNALHELP PoshGram-help.xml #> function Send-TelegramLocalAnimation { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken, #you could set a token right here if you wanted [Parameter(Mandatory = $true, HelpMessage = '-#########')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$ChatID, #you could set a Chat ID right here if you wanted [Parameter(Mandatory = $true, HelpMessage = 'File path to the animation you wish to send')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$AnimationPath, [Parameter(Mandatory = $false, HelpMessage = 'Animation caption')] [string]$Caption = "", #set to false by default [Parameter(Mandatory = $false, HelpMessage = 'HTML vs Markdown for message formatting')] [ValidateSet("Markdown", "HTML")] [string]$ParseMode = "Markdown", #set to Markdown by default [Parameter(Mandatory = $false, HelpMessage = 'Sends the message silently')] [bool]$DisableNotification = $false #set to false by default ) #------------------------------------------------------------------------ $results = $true #assume the best #------------------------------------------------------------------------ Write-Verbose -Message "Verifying presence of animation..." if (!(Test-Path -Path $AnimationPath)) { Write-Warning "The specified animation path: $AnimationPath was not found." $results = $false return $results }#if_testPath else{ Write-Verbose -Message "Path verified." }#else_testPath #------------------------------------------------------------------------ Write-Verbose -Message "Verifying extension type..." $fileTypeEval = Test-FileExtension -FilePath $AnimationPath -Type Animation if ($fileTypeEval -eq $false) { $results = $false return $results }#if_animationExtension else { Write-Verbose -Message "Extension supported." }#else_animationExtension #------------------------------------------------------------------------ Write-Verbose -Message "Verifying file size..." $fileSizeEval = Test-FileSize -Path $AnimationPath if ($fileSizeEval -eq $false) { $results = $false return $results }#if_animationSize else { Write-Verbose -Message "File size verified." }#else_animationSize #------------------------------------------------------------------------ try { $fileObject = Get-Item $AnimationPath -ErrorAction Stop }#try_Get-ItemAnimation catch { Write-Warning "The specified animation could not be interpreted properly." $results = $false return $results }#catch_Get-ItemAnimation #------------------------------------------------------------------------ $uri = "https://api.telegram.org/bot$BotToken/sendAnimation" $Form = @{ chat_id = $ChatID animation = $fileObject caption = $Caption parse_mode = $ParseMode disable_notification = $DisableNotification }#form #------------------------------------------------------------------------ try { $results = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form -ErrorAction Stop }#try_messageSend catch { Write-Warning "An error was encountered sending the Telegram animation message:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Send-TelegramLocalAnimation <# .EXTERNALHELP PoshGram-help.xml #> function Send-TelegramLocalAudio { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken, #you could set a token right here if you wanted [Parameter(Mandatory = $true, HelpMessage = '-#########')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$ChatID, #you could set a Chat ID right here if you wanted [Parameter(Mandatory = $true, HelpMessage = 'Local path to file you wish to send')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$Audio, [Parameter(Mandatory = $false, HelpMessage = 'Caption for file')] [string]$Caption = "", #set to false by default [Parameter(Mandatory = $false, HelpMessage = 'HTML vs Markdown for message formatting')] [ValidateSet("Markdown", "HTML")] [string]$ParseMode = "Markdown", #set to Markdown by default [Parameter(Mandatory = $false, HelpMessage = 'Duration of the audio in seconds')] [int]$Duration, [Parameter(Mandatory = $false, HelpMessage = 'Performer')] [string]$Performer, [Parameter(Mandatory = $false, HelpMessage = 'TrackName')] [string]$Title, [Parameter(Mandatory = $false, HelpMessage = 'Sends the message silently')] [bool]$DisableNotification = $false #set to false by default ) #------------------------------------------------------------------------ $results = $true #assume the best #------------------------------------------------------------------------ Write-Verbose -Message "Verifying presence of file..." if (!(Test-Path -Path $Audio)) { Write-Warning "The specified file: $Audio was not found." $results = $false return $results }#if_testPath else{ Write-Verbose -Message "Path verified." }#else_testPath #------------------------------------------------------------------------ Write-Verbose -Message "Verifying extension type..." $fileTypeEval = Test-FileExtension -FilePath $Audio -Type Audio if ($fileTypeEval -eq $false) { $results = $false return $results }#if_videoExtension else { Write-Verbose -Message "Extension supported." }#else_videoExtension #------------------------------------------------------------------------ Write-Verbose -Message "Verifying file size..." $fileSizeEval = Test-FileSize -Path $Audio if ($fileSizeEval -eq $false) { $results = $false return $results }#if_videoSize else { Write-Verbose -Message "File size verified." }#else_videoSize #------------------------------------------------------------------------ try { $fileObject = Get-Item $Audio -ErrorAction Stop }#try_Get-ItemVideo catch { Write-Warning "The specified file could not be interpreted properly." $results = $false return $results }#catch_Get-ItemVideo #------------------------------------------------------------------------ $uri = "https://api.telegram.org/bot$BotToken/sendAudio" $Form = @{ chat_id = $ChatID audio = $fileObject caption = $Caption parse_mode = $ParseMode duration = $Duration performer = $Performer title = $Title disable_notification = $DisableNotification }#form #------------------------------------------------------------------------ try { $results = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form -ErrorAction Stop }#try_messageSend catch { Write-Warning "An error was encountered sending the Telegram audio message:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Send-TelegramLocalAudio <# .EXTERNALHELP PoshGram-help.xml #> function Send-TelegramLocalDocument { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken, #you could set a token right here if you wanted [Parameter(Mandatory = $true, HelpMessage = '-#########')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$ChatID, #you could set a Chat ID right here if you wanted [Parameter(Mandatory = $true, HelpMessage = 'Local path to file you wish to send')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$File, [Parameter(Mandatory = $false, HelpMessage = 'Caption for file')] [string]$Caption = "", #set to false by default [Parameter(Mandatory = $false, HelpMessage = 'HTML vs Markdown for message formatting')] [ValidateSet("Markdown", "HTML")] [string]$ParseMode = "Markdown", #set to Markdown by default [Parameter(Mandatory = $false, HelpMessage = 'Sends the message silently')] [bool]$DisableNotification = $false #set to false by default ) #------------------------------------------------------------------------ $results = $true #assume the best #------------------------------------------------------------------------ Write-Verbose -Message "Verifying presence of file..." if (!(Test-Path -Path $File)) { Write-Warning "The specified file: $File was not found." $results = $false return $results }#if_testPath else { Write-Verbose -Message "Path verified." }#else_testPath #------------------------------------------------------------------------ Write-Verbose -Message "Verifying file size..." $fileSizeEval = Test-FileSize -Path $File if ($fileSizeEval -eq $false) { $results = $false return $results }#if_photoSize else { Write-Verbose -Message "File size verified." }#else_photoSize #------------------------------------------------------------------------ try { $fileObject = Get-Item $File -ErrorAction Stop }#try_Get-ItemPhoto catch { Write-Warning "The specified file could not be interpreted properly." $results = $false return $results }#catch_Get-ItemPhoto #------------------------------------------------------------------------ $uri = "https://api.telegram.org/bot$BotToken/sendDocument" $Form = @{ chat_id = $ChatID document = $fileObject caption = $Caption parse_mode = $ParseMode disable_notification = $DisableNotification }#form #------------------------------------------------------------------------ try { $results = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form -ErrorAction Stop }#try_messageSend catch { Write-Warning "An error was encountered sending the Telegram document message:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Send-TelegramLocalDocument <# .EXTERNALHELP PoshGram-help.xml #> function Send-TelegramLocalPhoto { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken, #you could set a token right here if you wanted [Parameter(Mandatory = $true, HelpMessage = '-#########')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$ChatID, #you could set a Chat ID right here if you wanted [Parameter(Mandatory = $true, HelpMessage = 'File path to the photo you wish to send')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$PhotoPath, [Parameter(Mandatory = $false, HelpMessage = 'Photo caption')] [string]$Caption = "", #set to false by default [Parameter(Mandatory = $false, HelpMessage = 'HTML vs Markdown for message formatting')] [ValidateSet("Markdown", "HTML")] [string]$ParseMode = "Markdown", #set to Markdown by default [Parameter(Mandatory = $false, HelpMessage = 'Sends the message silently')] [bool]$DisableNotification = $false #set to false by default ) #------------------------------------------------------------------------ $results = $true #assume the best #------------------------------------------------------------------------ Write-Verbose -Message "Verifying presence of photo..." if (!(Test-Path -Path $PhotoPath)) { Write-Warning "The specified photo path: $PhotoPath was not found." $results = $false return $results }#if_testPath else{ Write-Verbose -Message "Path verified." }#else_testPath #------------------------------------------------------------------------ Write-Verbose -Message "Verifying extension type..." $fileTypeEval = Test-FileExtension -FilePath $PhotoPath -Type Photo if ($fileTypeEval -eq $false) { $results = $false return $results }#if_photoExtension else { Write-Verbose -Message "Extension supported." }#else_photoExtension #------------------------------------------------------------------------ Write-Verbose -Message "Verifying file size..." $fileSizeEval = Test-FileSize -Path $PhotoPath if ($fileSizeEval -eq $false) { $results = $false return $results }#if_photoSize else { Write-Verbose -Message "File size verified." }#else_photoSize #------------------------------------------------------------------------ try { $fileObject = Get-Item $PhotoPath -ErrorAction Stop }#try_Get-ItemPhoto catch { Write-Warning "The specified photo could not be interpreted properly." $results = $false return $results }#catch_Get-ItemPhoto #------------------------------------------------------------------------ $uri = "https://api.telegram.org/bot$BotToken/sendphoto" $Form = @{ chat_id = $ChatID photo = $fileObject caption = $Caption parse_mode = $ParseMode disable_notification = $DisableNotification }#form #------------------------------------------------------------------------ try { $results = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form -ErrorAction Stop }#try_messageSend catch { Write-Warning "An error was encountered sending the Telegram photo message:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Send-TelegramLocalPhoto <# .EXTERNALHELP PoshGram-help.xml #> function Send-TelegramLocalVideo { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken, #you could set a token right here if you wanted [Parameter(Mandatory = $true, HelpMessage = '-#########')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$ChatID, #you could set a Chat ID right here if you wanted [Parameter(Mandatory = $true, HelpMessage = 'Local path to file you wish to send')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$Video, [Parameter(Mandatory = $false, HelpMessage = 'Duration of video in seconds')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [Int32]$Duration, [Parameter(Mandatory = $false, HelpMessage = 'Video width')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [Int32]$Width, [Parameter(Mandatory = $false, HelpMessage = 'Video height')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [Int32]$Height, [Parameter(Mandatory = $false, HelpMessage = 'Caption for file')] [string]$Caption = "", #set to false by default [Parameter(Mandatory = $false, HelpMessage = 'HTML vs Markdown for message formatting')] [ValidateSet("Markdown", "HTML")] [string]$ParseMode = "Markdown", #set to Markdown by default [Parameter(Mandatory = $false, HelpMessage = 'Pass True, if the uploaded video is suitable for streaming')] [bool]$Streaming, #set to Markdown by default [Parameter(Mandatory = $false, HelpMessage = 'Sends the message silently')] [bool]$DisableNotification = $false #set to false by default ) #------------------------------------------------------------------------ $results = $true #assume the best #------------------------------------------------------------------------ Write-Verbose -Message "Verifying presence of file..." if (!(Test-Path -Path $Video)) { Write-Warning "The specified file: $Video was not found." $results = $false return $results }#if_testPath else{ Write-Verbose -Message "Path verified." }#else_testPath #------------------------------------------------------------------------ Write-Verbose -Message "Verifying extension type..." $fileTypeEval = Test-FileExtension -FilePath $Video -Type Video if ($fileTypeEval -eq $false) { $results = $false return $results }#if_videoExtension else { Write-Verbose -Message "Extension supported." }#else_videoExtension #------------------------------------------------------------------------ Write-Verbose -Message "Verifying file size..." $fileSizeEval = Test-FileSize -Path $Video if ($fileSizeEval -eq $false) { $results = $false return $results }#if_videoSize else { Write-Verbose -Message "File size verified." }#else_videoSize #------------------------------------------------------------------------ try { $fileObject = Get-Item $Video -ErrorAction Stop }#try_Get-ItemVideo catch { Write-Warning "The specified file could not be interpreted properly." $results = $false return $results }#catch_Get-ItemVideo #------------------------------------------------------------------------ $uri = "https://api.telegram.org/bot$BotToken/sendVideo" $Form = @{ chat_id = $ChatID video = $fileObject duration = $Duration width = $Width height = $Height caption = $Caption parse_mode = $ParseMode supports_streaming = $Streaming disable_notification = $DisableNotification }#form #------------------------------------------------------------------------ try { $results = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form -ErrorAction Stop }#try_messageSend catch { Write-Warning "An error was encountered sending the Telegram video message:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Send-TelegramLocalVideo <# .EXTERNALHELP PoshGram-help.xml #> function Send-TelegramLocation { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken, #you could set a token right here if you wanted [Parameter(Mandatory = $true, HelpMessage = '-#########')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$ChatID, #you could set a Chat ID right here if you wanted [Parameter(Mandatory = $true, HelpMessage = 'Latitude of the location')] [ValidateRange(-90, 90)] [single]$Latitude, [Parameter(Mandatory = $true, HelpMessage = 'Longitude of the location')] [ValidateRange(-180, 180)] [single]$Longitude, [Parameter(Mandatory = $false, HelpMessage = 'Sends the message silently')] [bool]$DisableNotification = $false #set to false by default ) #------------------------------------------------------------------------ $results = $true #assume the best #------------------------------------------------------------------------ $uri = "https://api.telegram.org/bot$BotToken/sendLocation" $Form = @{ chat_id = $ChatID latitude = $Latitude longitude = $Longitude disable_notification = $DisableNotification }#form #------------------------------------------------------------------------ try { $results = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form -ErrorAction Stop }#try_messageSend catch { Write-Warning "An error was encountered sending the Telegram location:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Send-TelegramLocation <# .EXTERNALHELP PoshGram-help.xml #> function Send-TelegramMediaGroup { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken, #you could set a token right here if you wanted [Parameter(Mandatory = $true, HelpMessage = '-#########')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$ChatID, #you could set a Chat ID right here if you wanted [Parameter(Mandatory = $true, HelpMessage = 'Type of media to send')] [ValidateSet("Photo", "Video")] [string]$MediaType, #set to false by default [Parameter(Mandatory = $false, HelpMessage = 'List of filepaths for media you want to send')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string[]]$FilePaths, [Parameter(Mandatory = $false, HelpMessage = 'Sends the message silently')] [bool]$DisableNotification = $false #set to false by default ) #------------------------------------------------------------------------ $results = $true #assume the best $MediaType = $MediaType.ToLower() #------------------------------------------------------------------------ Write-Verbose -Message "Evaluating file count..." if ($FilePaths.Count -le 1 -or $FilePaths.Count -gt 10) { Write-Warning -Message "Send-TelegramMediaGroup requires a minimum of 2 and a maximum of 10 media files to be provided." $results = $false return $results }#file_Count else { Write-Verbose -Message "File count is: $($FilePaths.Count)" }#else_FileCount #------------------------------------------------------------------------ Write-Verbose -Message "You have specified a media type of: $MediaType" #------------------------------------------------------------------------ foreach ($file in $FilePaths) { $fileTypeEval = $null $fileSizeEval = $null Write-Verbose -Message "Verifying presence of media..." if (!(Test-Path -Path $file)) { Write-Warning "The specified media path: $file was not found." $results = $false return $results }#if_testPath Write-Verbose -Message "Verifying extension type..." $fileTypeEval = Test-FileExtension -FilePath $file -Type $MediaType if ($fileTypeEval -eq $false) { $results = $false return $results }#if_Extension else { Write-Verbose -Message "Extension supported." }#else_Extension Write-Verbose -Message "Verifying file size..." $fileSizeEval = Test-FileSize -Path $file if ($fileSizeEval -eq $false) { $results = $false return $results }#if_Size else { Write-Verbose -Message "File size verified." }#else_Size }#foreach_File #------------------------------------------------------------------------ $uri = "https://api.telegram.org/bot$BotToken/sendMediaGroup" #------------------------------------------------------------------------ Write-Verbose -Message "Forming serialzied JSON for all media files..." $Form = @{ chat_id = $ChatID; disable_notification = $DisableNotification media = '' } $json = @' [ '@ $i = 1 foreach ($file in $FilePaths) { $fInfo = $null try { $fInfo = Get-Item -Path $file -ErrorAction Stop } catch { Write-Warning -Message "An issue was encountered retrieving data from: $file" $results = $false return $results } $Form += @{"$MediaType$i" = $fInfo } $json += "{`"type`":`"$MediaType`",`"`media`":`"attach://$MediaType$i`"}," $i++ } $json = $json.Substring(0, $json.Length - 1) $json += @' ] '@ $Form.media = $json Write-Verbose -Message "JSON formation completed." #------------------------------------------------------------------------ Write-Verbose -Message "Sending media..." try { $results = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form -ErrorAction Stop Write-Verbose -Message "Media sent." }#try_messageSend catch { Write-Warning "An error was encountered sending the Telegram photo message:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Send-TelegramMediaGroup <# .EXTERNALHELP PoshGram-help.xml #> function Send-TelegramTextMessage { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken, #you could set a token right here if you wanted [Parameter(Mandatory = $true, HelpMessage = '-#########')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$ChatID, #you could set a Chat ID right here if you wanted [Parameter(Mandatory = $true, HelpMessage = 'Text of the message to be sent')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$Message, [Parameter(Mandatory = $false, HelpMessage = 'HTML vs Markdown for message formatting')] [ValidateSet("Markdown", "HTML")] [string]$ParseMode = "Markdown", #set to Markdown by default [Parameter(Mandatory = $false, HelpMessage = 'Disables link previews')] [bool]$Preview = $false, #set to false by default [Parameter(Mandatory = $false, HelpMessage = 'Sends the message silently')] [bool]$DisableNotification = $false #set to false by default ) #------------------------------------------------------------------------ $results = $true #assume the best #------------------------------------------------------------------------ if ($Message -like "*_*") { Write-Verbose -Message "Characters detected that must be sent as HTML" $ParseMode = 'HTML' }#if_ #------------------------------------------------------------------------ $payload = @{ "chat_id" = $ChatID "text" = $Message "parse_mode" = $ParseMode "disable_web_page_preview" = $Preview "disable_notification" = $DisableNotification }#payload #------------------------------------------------------------------------ try { Write-Verbose -Message "Sending message..." $results = Invoke-RestMethod ` -Uri ("https://api.telegram.org/bot{0}/sendMessage" -f $BotToken) ` -Method Post ` -ContentType "application/json" ` -Body (ConvertTo-Json -Compress -InputObject $payload) ` -ErrorAction Stop }#try_messageSend catch { Write-Warning "An error was encountered sending the Telegram message:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Send-TelegramTextMessage <# .EXTERNALHELP PoshGram-help.xml #> function Send-TelegramURLAnimation { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken, #you could set a token right here if you wanted [Parameter(Mandatory = $true, HelpMessage = '-#########')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$ChatID, #you could set a Chat ID right here if you wanted [Parameter(Mandatory = $true, HelpMessage = 'URL path to animation')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$AnimationURL, [Parameter(Mandatory = $false, HelpMessage = 'animation caption')] [string]$Caption, [Parameter(Mandatory = $false, HelpMessage = 'HTML vs Markdown for message formatting')] [ValidateSet("Markdown", "HTML")] [string]$ParseMode = "Markdown", #set to Markdown by default [Parameter(Mandatory = $false, HelpMessage = 'Sends the message silently')] [bool]$DisableNotification = $false #set to false by default ) #------------------------------------------------------------------------ $results = $true #assume the best #------------------------------------------------------------------------ Write-Verbose -Message "Verifying URL leads to supported animation extension..." $fileTypeEval = Test-URLExtension -URL $AnimationURL -Type Animation if ($fileTypeEval -eq $false) { $results = $false return $results }#if_documentExtension else { Write-Verbose -Message "Extension supported." }#else_documentExtension #------------------------------------------------------------------------ Write-Verbose -Message "Verifying URL presence and file size..." $fileSizeEval = Test-URLFileSize -URL $AnimationURL if ($fileSizeEval -eq $false) { $results = $false return $results }#if_animationSize else { Write-Verbose -Message "File size verified." }#else_animationSize #------------------------------------------------------------------------ $payload = @{ "chat_id" = $ChatID "animation" = $AnimationURL "caption" = $Caption "parse_mode" = $ParseMode "disable_notification" = $DisableNotification }#payload #------------------------------------------------------------------------ try { Write-Verbose -Message "Sending message..." $results = Invoke-RestMethod ` -Uri ("https://api.telegram.org/bot{0}/sendAnimation" -f $BotToken) ` -Method Post ` -ContentType "application/json" ` -Body (ConvertTo-Json -Compress -InputObject $payload) ` -ErrorAction Stop }#try_messageSend catch { Write-Warning "An error was encountered sending the Telegram message:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Send-TelegramURLAnimation <# .EXTERNALHELP PoshGram-help.xml #> function Send-TelegramURLAudio { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken, #you could set a token right here if you wanted [Parameter(Mandatory = $true, HelpMessage = '-#########')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$ChatID, #you could set a Chat ID right here if you wanted [Parameter(Mandatory = $true, HelpMessage = 'Local path to file you wish to send')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$AudioURL, [Parameter(Mandatory = $false, HelpMessage = 'Caption for file')] [string]$Caption = "", #set to false by default [Parameter(Mandatory = $false, HelpMessage = 'HTML vs Markdown for message formatting')] [ValidateSet("Markdown", "HTML")] [string]$ParseMode = "Markdown", #set to Markdown by default [Parameter(Mandatory = $false, HelpMessage = 'Duration of the audio in seconds')] [int]$Duration, [Parameter(Mandatory = $false, HelpMessage = 'Performer')] [string]$Performer, [Parameter(Mandatory = $false, HelpMessage = 'TrackName')] [string]$Title, [Parameter(Mandatory = $false, HelpMessage = 'Sends the message silently')] [bool]$DisableNotification = $false #set to false by default ) #------------------------------------------------------------------------ $results = $true #assume the best #------------------------------------------------------------------------ Write-Verbose -Message "Verifying URL leads to supported audio extension..." $fileTypeEval = Test-URLExtension -URL $AudioURL -Type Audio if ($fileTypeEval -eq $false) { $results = $false return $results }#if_documentExtension else { Write-Verbose -Message "Extension supported." }#else_documentExtension #------------------------------------------------------------------------ Write-Verbose -Message "Verifying URL presence and file size..." $fileSizeEval = Test-URLFileSize -URL $AudioURL if ($fileSizeEval -eq $false) { $results = $false return $results }#if_documentSize else { Write-Verbose -Message "File size verified." }#else_documentSize #------------------------------------------------------------------------ $payload = @{ chat_id = $ChatID audio = $AudioURL caption = $Caption parse_mode = $ParseMode duration = $Duration performer = $Performer title = $Title disable_notification = $DisableNotification }#payload #------------------------------------------------------------------------ try { Write-Verbose -Message "Sending message..." $results = Invoke-RestMethod ` -Uri ("https://api.telegram.org/bot{0}/sendAudio" -f $BotToken) ` -Method Post ` -ContentType "application/json" ` -Body (ConvertTo-Json -Compress -InputObject $payload) ` -ErrorAction Stop }#try_messageSend catch { Write-Warning "An error was encountered sending the Telegram message:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Send-TelegramURLAudio <# .EXTERNALHELP PoshGram-help.xml #> function Send-TelegramURLDocument { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken, #you could set a token right here if you wanted [Parameter(Mandatory = $true, HelpMessage = '-#########')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$ChatID, #you could set a Chat ID right here if you wanted [Parameter(Mandatory = $true, HelpMessage = 'URL path to file')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$FileURL, [Parameter(Mandatory = $false, HelpMessage = 'File caption')] [string]$Caption, [Parameter(Mandatory = $false, HelpMessage = 'HTML vs Markdown for message formatting')] [ValidateSet("Markdown", "HTML")] [string]$ParseMode = "Markdown", #set to Markdown by default [Parameter(Mandatory = $false, HelpMessage = 'Sends the message silently')] [bool]$DisableNotification = $false #set to false by default ) #------------------------------------------------------------------------ $results = $true #assume the best #------------------------------------------------------------------------ Write-Verbose -Message "Verifying URL leads to supported document extension..." $fileTypeEval = Test-URLExtension -URL $FileURL -Type Document if ($fileTypeEval -eq $false) { $results = $false return $results }#if_documentExtension else { Write-Verbose -Message "Extension supported." }#else_documentExtension #------------------------------------------------------------------------ Write-Verbose -Message "Verifying URL presence and file size..." $fileSizeEval = Test-URLFileSize -URL $FileURL if ($fileSizeEval -eq $false) { $results = $false return $results }#if_documentSize else { Write-Verbose -Message "File size verified." }#else_documentSize #------------------------------------------------------------------------ $payload = @{ "chat_id" = $ChatID "document" = $FileURL "caption" = $Caption "parse_mode" = $ParseMode "disable_notification" = $DisableNotification }#payload #------------------------------------------------------------------------ try { Write-Verbose -Message "Sending message..." $results = Invoke-RestMethod ` -Uri ("https://api.telegram.org/bot{0}/sendDocument" -f $BotToken) ` -Method Post ` -ContentType "application/json" ` -Body (ConvertTo-Json -Compress -InputObject $payload) ` -ErrorAction Stop }#try_messageSend catch { Write-Warning "An error was encountered sending the Telegram message:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Send-TelegramURLDocument <# .EXTERNALHELP PoshGram-help.xml #> function Send-TelegramURLPhoto { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken, #you could set a token right here if you wanted [Parameter(Mandatory = $true, HelpMessage = '-#########')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$ChatID, #you could set a Chat ID right here if you wanted [Parameter(Mandatory = $true, HelpMessage = 'URL path to photo')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$PhotoURL, [Parameter(Mandatory = $false, HelpMessage = 'Photo caption')] [string]$Caption, [Parameter(Mandatory = $false, HelpMessage = 'HTML vs Markdown for message formatting')] [ValidateSet("Markdown", "HTML")] [string]$ParseMode = "Markdown", #set to Markdown by default [Parameter(Mandatory = $false, HelpMessage = 'Sends the message silently')] [bool]$DisableNotification = $false #set to false by default ) #------------------------------------------------------------------------ $results = $true #assume the best #------------------------------------------------------------------------ Write-Verbose -Message "Verifying URL leads to supported photo extension..." $fileTypeEval = Test-URLExtension -URL $PhotoURL -Type Photo if ($fileTypeEval -eq $false) { $results = $false return $results }#if_documentExtension else { Write-Verbose -Message "Extension supported." }#else_documentExtension #------------------------------------------------------------------------ Write-Verbose -Message "Verifying URL presence and file size..." $fileSizeEval = Test-URLFileSize -URL $PhotoURL if ($fileSizeEval -eq $false) { $results = $false return $results }#if_photoSize else { Write-Verbose -Message "File size verified." }#else_photoSize #------------------------------------------------------------------------ $payload = @{ "chat_id" = $ChatID "photo" = $PhotoURL "caption" = $Caption "parse_mode" = $ParseMode "disable_notification" = $DisableNotification }#payload #------------------------------------------------------------------------ try { Write-Verbose -Message "Sending message..." $results = Invoke-RestMethod ` -Uri ("https://api.telegram.org/bot{0}/sendphoto" -f $BotToken) ` -Method Post ` -ContentType "application/json" ` -Body (ConvertTo-Json -Compress -InputObject $payload) ` -ErrorAction Stop }#try_messageSend catch { Write-Warning "An error was encountered sending the Telegram message:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Send-TelegramURLPhoto <# .EXTERNALHELP PoshGram-help.xml #> function Send-TelegramURLVideo { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken, #you could set a token right here if you wanted [Parameter(Mandatory = $true, HelpMessage = '-#########')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$ChatID, #you could set a Chat ID right here if you wanted [Parameter(Mandatory = $true, HelpMessage = 'URL to file you wish to send')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$VideoURL, [Parameter(Mandatory = $false, HelpMessage = 'Duration of video in seconds')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [Int32]$Duration, [Parameter(Mandatory = $false, HelpMessage = 'Video width')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [Int32]$Width, [Parameter(Mandatory = $false, HelpMessage = 'Video height')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [Int32]$Height, [Parameter(Mandatory = $false, HelpMessage = 'Caption for file')] [string]$Caption = "", #set to false by default [Parameter(Mandatory = $false, HelpMessage = 'HTML vs Markdown for message formatting')] [ValidateSet("Markdown", "HTML")] [string]$ParseMode = "Markdown", #set to Markdown by default [Parameter(Mandatory = $false, HelpMessage = 'Pass True, if the uploaded video is suitable for streaming')] [bool]$Streaming, #set to Markdown by default [Parameter(Mandatory = $false, HelpMessage = 'Sends the message silently')] [bool]$DisableNotification = $false #set to false by default ) #------------------------------------------------------------------------ $results = $true #assume the best #------------------------------------------------------------------------ Write-Verbose -Message "Verifying URL leads to supported document extension..." $fileTypeEval = Test-URLExtension -URL $VideoURL -Type Video if ($fileTypeEval -eq $false) { $results = $false return $results }#if_documentExtension else { Write-Verbose -Message "Extension supported." }#else_documentExtension #------------------------------------------------------------------------ Write-Verbose -Message "Verifying URL presence and file size..." $fileSizeEval = Test-URLFileSize -URL $VideoURL if ($fileSizeEval -eq $false) { $results = $false return $results }#if_documentSize else { Write-Verbose -Message "File size verified." }#else_documentSize #------------------------------------------------------------------------ $payload = @{ chat_id = $ChatID video = $VideoURL duration = $Duration width = $Width height = $Height caption = $Caption parse_mode = $ParseMode supports_streaming = $Streaming disable_notification = $DisableNotification }#payload #------------------------------------------------------------------------ try { Write-Verbose -Message "Sending message..." $results = Invoke-RestMethod ` -Uri ("https://api.telegram.org/bot{0}/sendVideo" -f $BotToken) ` -Method Post ` -ContentType "application/json" ` -Body (ConvertTo-Json -Compress -InputObject $payload) ` -ErrorAction Stop }#try_messageSend catch { Write-Warning "An error was encountered sending the Telegram message:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Send-TelegramURLVideo <# .EXTERNALHELP PoshGram-help.xml #> function Test-BotToken { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken ) #------------------------------------------------------------------------ $results = $true #assume the best #------------------------------------------------------------------------ try { Write-Verbose -Message "Testing Bot Token..." $results = Invoke-RestMethod ` -Uri ("https://api.telegram.org/bot{0}/getMe" -f $BotToken) ` -Method Get ` -ErrorAction Stop }#try_messageSend catch { Write-Warning "An error was encountered testing the BOT token:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Test-BotToken |