Skip to content

Instantly share code, notes, and snippets.

@devblackops
Created September 7, 2017 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devblackops/3400ebe94f57d99c60f28de4f83e04c3 to your computer and use it in GitHub Desktop.
Save devblackops/3400ebe94f57d99c60f28de4f83e04c3 to your computer and use it in GitHub Desktop.
PoshBot function to shorten a url
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