Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Telegram listener via Powershell
### Edit only this part! ################
$MyToken = ""
$ChatID = ""
#########################################
$InitialMessageDate = 0
$counter = 0
while ($true)
{
$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 = ""
#write-host "before loop $InitialMessageDate"
Foreach ($Result in $jsonresult) {
#write-host "$Result.update_id[-1]"
If (($Result.channel_post.chat.id -eq $ChatID) -AND ($Result.update_id -gt $InitialMessageDate)) {
if(($LastMessage -ne $null) -AND ($counter -gt 0)){
powershell $Result.channel_post.text
}
$LastMessage = $Result.channel_post.text
}
}
$InitialMessageDate = $jsonresult.update_id[-1]
sleep 1
$counter++
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.