Created
September 7, 2017 03:51
-
-
Save devblackops/3400ebe94f57d99c60f28de4f83e04c3 to your computer and use it in GitHub Desktop.
PoshBot function to shorten a url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Url { | |
[PoshBot.BotCommand( | |
Command = $false, | |
TriggerType = 'Regex', | |
Regex = '^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)|[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}[^ ]+' | |
)] | |
[cmdletbinding()] | |
param( | |
[parameter(Mandatory, ValueFromRemainingArguments)] | |
[object[]]$Arguments, | |
[PoshBot.FromConfig('GoogleApiShortenerApiKey')] | |
[parameter(Mandatory)] | |
[string]$ApiKey | |
) | |
$body = @{ | |
longUrl = $arguments[0] | |
} | ConvertTo-Json | |
$irmParams = @{ | |
Uri = "https://www.googleapis.com/urlshortener/v1/url?key=$ApiKey" | |
Method = 'Post' | |
Body = $body | |
ContentType = 'application/json' | |
} | |
try { | |
$goo = Invoke-RestMethod @irmParams | |
Write-Output "Nice link, I made it better $($goo.id)" | |
} catch { | |
Write-Error $_ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment