Skip to content

Instantly share code, notes, and snippets.

@dimiboy
Created August 6, 2019 07:26
Show Gist options
  • Save dimiboy/1737878ab1a093e3d3f20594a2ba2d1a to your computer and use it in GitHub Desktop.
Save dimiboy/1737878ab1a093e3d3f20594a2ba2d1a to your computer and use it in GitHub Desktop.
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