Skip to content

Instantly share code, notes, and snippets.

@SteloNLD
Last active May 24, 2019 06:52
Show Gist options
  • Save SteloNLD/1402f4e61551573aee555d265549a8b7 to your computer and use it in GitHub Desktop.
Save SteloNLD/1402f4e61551573aee555d265549a8b7 to your computer and use it in GitHub Desktop.
Telegram example
# ------------------------------------------------------------------------------------------------------------------------
# Preferences
# ------------------------------------------------------------------------------------------------------------------------
# Setting StrictMode to enforce Coding Best Practices.
Set-StrictMode -Version 2.0
# Setting the Error Preference variable to stop to make sure that non terminating errors do terminate.
$ErrorActionPreference = 'Stop'
# Verbose preference, should be toggled with the parameter -Verbose.
#$VerbosePreference = 'Continue'
# Debug preference, generates more logging should be toggled with the parameter -Verbose.
#$DebugPreference = 'Continue'
# ------------------------------------------------------------------------------------------------------------------------
# Settings
# ------------------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------
# Telegram
$TelegramAuthorizedChats = @(
@{ID='853881768'; Roles=@('Subscriber', 'Member')}
@{ID='-336683942'; Roles=@('Subscriber', 'Member')}
)
# ------------------------------------------------------------------------------------------------------------------------
# Functions
# ------------------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------
# Connect-TelegramBot
Function Connect-TelegramBot {
Param (
[String] $Token
)
$Global:TelegramBot = @{
#Name = 'dd'
#DisplayName = '..'
Token = $Token
}
# Make sure thath older updates (messages) arent proccesed by retrieving the latest update from the queu.
$BotMessagesHistory = (Invoke-RestMethod -Uri "https://api.telegram.org/bot$($Token)/getUpdates").result
$GLobal:LatestUpdateID = $BotMessagesHistory | Select-Object -Last 1 | Select-Object -ExpandProperty update_id
}
# -----------------------------------------------------------
# Get-TelegramBotMessages
Function Get-TelegramBotMessages {
# Recieve Messages from BotMessageQue
(Invoke-RestMethod -Uri "https://api.telegram.org/bot$($Global:TelegramBot.Token)/getUpdates").result
}
# -----------------------------------------------------------
# Send-TelegramBotChatMessage
Function Send-TelegramBotChatMessage {
Param (
[String] $ChatID,
[String] $Message,
[String] $Token = $Global:TelegramBot.Token
)
$APICall = @{
Uri = "https://api.telegram.org/bot$($Token)/sendMessage?chat_id=$($ChatID)&text=$($Message -join '%0A')"
}
Return Invoke-RestMethod @APICall
}
# -----------------------------------------------------------
# Invoke-MigrationStatusUpdate
Function Invoke-MigrationStatusUpdate {
Param (
[Parameter(Mandatory=$true)]
[ValidateSet("User", "Batch", "MoveRequest")]
[String] $Type,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String] $Name
)
$null = Send-TelegramBotChatMessage -ChatID $BotMessage.message.chat.id -Message "A moment please, retrieving results for $($Type) '$($Name)'"
return "status is failed"
}
Connect-TelegramBot -Token '..'
while ($true) {
Start-Sleep -s 5
$BotMessages = Get-TelegramBotMessages
# Filter on new messages from AuthorizedChats
$BotMessages = $BotMessages | Where-Object {
$PSItem.update_id -gt $LatestUpdateID -and
$PSItem.message.chat.id -in $TelegramAuthorizedChats.ID
}
Foreach ($BotMessage in $BotMessages) {
Try {
Switch -Regex ($BotMessage.message.text) {
"^/Start" {
$Message = @(
"Thank you for contacting me, see below for a list of options:"
""
"/status user|batch name"
)
}
"^/status" {
$Args = $BotMessage.message.text -split ' '
$Message = Invoke-MigrationStatusUpdate -Type $Args[1] -Name ($Args[2..$Arg.Length] -join ' ')
}
Default {
$Message = "Could not answer the question '$($BotMessage.message.text)', please use /start for help."
}
}
}
Catch {
$Message = "$($_)"
}
#Format BaseMessage
$TelegramMessage = @{
ChatID = $BotMessage.message.chat.id
Message = $Message
}
# Send Message
$null = Send-TelegramBotChatMessage @TelegramMessage
$Global:LatestUpdateID = $BotMessage.update_id
}
}
$MyBotUpdates = Invoke-WebRequest -Uri "https://api.telegram.org/bot$($MyToken)/getUpdates"
#Convert the result from json and put them in an array
$jsonresult = [array]($MyBotUpdates | ConvertFrom-Json).result
$LastMessage = ""
Foreach ($Result in $jsonresult) {
If ($Result.message.chat.id -eq $ChatID) {
$LastMessage = $Result.message.text
}
}
Write-Host "The Last Message sent from others to this conversation is $($LastMessage)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment