Skip to content

Instantly share code, notes, and snippets.

@Laim
Created March 24, 2022 21:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Laim/5d735fbc962cf39d2c485778a995379c to your computer and use it in GitHub Desktop.
Save Laim/5d735fbc962cf39d2c485778a995379c to your computer and use it in GitHub Desktop.
Script that allows sending a pre-defined message to Discord or Telegram. Useful for Handbrake when conversions are completed.
<#
.SYNOPSIS
Sends a message to an external provider such as Discord or Telegram
.DESCRIPTION
Script that allows sending a pre-defined message to Discord or Telegram. Useful for Handbrake when conversions are completed.
.PARAMETER messageSystem
Which message system you want to use, Discord or Telegram.
.NOTES
File Name : handbrake_msg.ps1
Author : Laim McKenzie (https://laim.scot)
Copyright 2022 - Laim McKenzie
.LINK
https://laim.scot / https://gist.github.com/laim
.EXAMPLE
.\handbrake_msg.ps1 -messageSystem Discord
.EXAMPLE
.\handbrake_msg.ps1 -messageSystem Telegram
#>
param (
[Parameter(Mandatory=$true)]
[string]$messageSystem
)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Discord Webhook Function
function Send-Discord {
[string]$discordHook = "";
[string]$content = "Handbrake conversion has now finished. @everyone"
## Don't change below this line ! :)
$payload = [PSCustomObject]@{
content = $content
}
[bool]$hasError = $false
if([string]::IsNullOrEmpty($discordHook)) {
$hasError = $true
}
if([string]::IsNullOrEmpty($content)) {
$hasError = $true
}
if(!$hasError) {
Invoke-RestMethod -Uri $discordHook -Method Post -Body ($payload | ConvertTo-Json) -ContentType "application/json"
} else {
Write-Error "One or more required values are missing. Have you populated the hookUrl and content in Send-Discord?"
}
}
# Telegram Bot Function
function Send-Telegram {
$telegramToken = ""
$telegramChatID = ""
$content = "Handbrake conversion has now finished."
# Don't change below this line unless you know what you're doing.
# You can read up on the Telegram Documentation if you want to include some more stuff. It goes into the $payload
# Documentation: https://core.telegram.org/bots/api#sendmessage
$payload = @{
"chat_id" = $telegramChatID
"text" = $content
}
Invoke-RestMethod -Uri ("https://api.telegram.org/bot{0}/sendMessage" -f $telegramToken) -Method Post -ContentType "application/json" -Body ($payload | ConvertTo-Json)
}
# Runs the function depending on the parameter passed
switch ($messageSystem)
{
"Discord" { Send-Discord }
"Telegram" { Send-Telegram }
default { "Unknown parameter value. Options are Discord or Telegram." }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment