From 844816c057a9f8610ec5659dcef76620eb64db72 Mon Sep 17 00:00:00 2001 From: Alex Kup <116945542+Lifailon@users.noreply.github.com> Date: Thu, 18 Jan 2024 18:52:30 +0300 Subject: [PATCH] update modules telegram --- Scripts/Get-FromTelegram.psm1 | 77 +++++++++++++++---------- Scripts/Send-ToTelegram.psm1 | 24 ++++---- Scripts/Telegram-Bot-Service.ps1 | 96 +++++++++++++++++++------------- 3 files changed, 119 insertions(+), 78 deletions(-) diff --git a/Scripts/Get-FromTelegram.psm1 b/Scripts/Get-FromTelegram.psm1 index 473b8e5..ea396ff 100644 --- a/Scripts/Get-FromTelegram.psm1 +++ b/Scripts/Get-FromTelegram.psm1 @@ -1,31 +1,50 @@ function Get-FromTelegram { -param ( - $token = "687...:AAF...", - [switch]$last, - [switch]$date -) -$endpoint = "getUpdates" -$url = "https://api.telegram.org/bot$token/$endpoint" -$result = Invoke-RestMethod -Uri $url -if ($date) { -$Collections = New-Object System.Collections.Generic.List[System.Object] -foreach ($r in $($result.result)) { - $EpochTime = [DateTime]"1/1/1970" - $TimeZone = Get-TimeZone - $UTCTime = $EpochTime.AddSeconds($r.message.date) - $d = $UTCTime.AddMinutes($TimeZone.BaseUtcOffset.TotalMinutes) - #$d - $Collections.Add([PSCustomObject]@{ - Message = $r.message.text; - Date = $d - }) + param ( + $Token = "687...:AAF...", + [switch]$Date, + [switch]$Last, + [switch]$ChatID + ) + $endpoint = "getUpdates" + $url = "https://api.telegram.org/bot$Token/$endpoint" + $result = Invoke-RestMethod -Uri $url + if ($Date) { + $Collections = New-Object System.Collections.Generic.List[System.Object] + foreach ($r in $($result.result)) { + $EpochTime = [DateTime]"1/1/1970" + $TimeZone = Get-TimeZone + $UTCTime = $EpochTime.AddSeconds($r.message.date) + $d = $UTCTime.AddMinutes($TimeZone.BaseUtcOffset.TotalMinutes) + $Collections.Add([PSCustomObject]@{ + Message = $r.message.text; + Date = $d + }) + } + $Collections + } + else { + if ($Last) { + $result.result.message.text[-1] + } + elseif ($ChatID) { + $Collections = New-Object System.Collections.Generic.List[System.Object] + foreach ($r in $($result.result)) { + $Collections.Add([PSCustomObject]@{ + Message = $r.message.text; + UserName = $r.message.chat.username; + ChatID = $r.message.chat.id; + ChatType = $r.message.chat.type + }) + } + $Collections + } + else { + $result.result.message.text + } + } } -$Collections -} else { -if ($last) { - $result.result.message.text[-1] -} else { - $result.result.message.text -} -} -} \ No newline at end of file + +# Get-FromTelegram +# Get-FromTelegram -Last +# Get-FromTelegram -Date +# Get-FromTelegram -ChatID \ No newline at end of file diff --git a/Scripts/Send-ToTelegram.psm1 b/Scripts/Send-ToTelegram.psm1 index e25084a..6fa4b0d 100644 --- a/Scripts/Send-ToTelegram.psm1 +++ b/Scripts/Send-ToTelegram.psm1 @@ -1,14 +1,18 @@ function Send-ToTelegram { param ( -[Parameter(Mandatory = $True)]$Text, -$token = "687...:AAF...", -$chat = "125468108" + [Parameter(Mandatory = $True)]$Text, + $Token = "687...:AAF...", + $Chat = "125468108", + $Keyboard ) -$endpoint = "sendMessage" -$url = "https://api.telegram.org/bot$token/$endpoint" -$Body = @{ -chat_id = $Chat -text = $Text -} -Invoke-RestMethod -Uri $url -Body $Body + $endpoint = "sendMessage" + $url = "https://api.telegram.org/bot$Token/$endpoint" + $Body = @{ + chat_id = $Chat + text = $Text + } + if ($keyboard -ne $null) { + $Body += @{reply_markup = $keyboard} + } + Invoke-RestMethod -Uri $url -Body $Body } \ No newline at end of file diff --git a/Scripts/Telegram-Bot-Service.ps1 b/Scripts/Telegram-Bot-Service.ps1 index 10a7e5c..d6a20d7 100644 --- a/Scripts/Telegram-Bot-Service.ps1 +++ b/Scripts/Telegram-Bot-Service.ps1 @@ -1,48 +1,66 @@ function Get-FromTelegram { -param ( - $token = "687...:AAF...", - [switch]$last, - [switch]$date -) -$endpoint = "getUpdates" -$url = "https://api.telegram.org/bot$token/$endpoint" -$result = Invoke-RestMethod -Uri $url -if ($date) { -$Collections = New-Object System.Collections.Generic.List[System.Object] -foreach ($r in $($result.result)) { - $EpochTime = [DateTime]"1/1/1970" - $TimeZone = Get-TimeZone - $UTCTime = $EpochTime.AddSeconds($r.message.date) - $d = $UTCTime.AddMinutes($TimeZone.BaseUtcOffset.TotalMinutes) - #$d - $Collections.Add([PSCustomObject]@{ - Message = $r.message.text; - Date = $d - }) -} -$Collections -} else { -if ($last) { - $result.result.message.text[-1] -} else { - $result.result.message.text -} -} + param ( + $Token = "687...:AAF...", + [switch]$Date, + [switch]$Last, + [switch]$ChatID + ) + $endpoint = "getUpdates" + $url = "https://api.telegram.org/bot$Token/$endpoint" + $result = Invoke-RestMethod -Uri $url + if ($Date) { + $Collections = New-Object System.Collections.Generic.List[System.Object] + foreach ($r in $($result.result)) { + $EpochTime = [DateTime]"1/1/1970" + $TimeZone = Get-TimeZone + $UTCTime = $EpochTime.AddSeconds($r.message.date) + $d = $UTCTime.AddMinutes($TimeZone.BaseUtcOffset.TotalMinutes) + $Collections.Add([PSCustomObject]@{ + Message = $r.message.text; + Date = $d + }) + } + $Collections + } + else { + if ($Last) { + $result.result.message.text[-1] + } + elseif ($ChatID) { + $Collections = New-Object System.Collections.Generic.List[System.Object] + foreach ($r in $($result.result)) { + $Collections.Add([PSCustomObject]@{ + Message = $r.message.text; + UserName = $r.message.chat.username; + ChatID = $r.message.chat.id; + ChatType = $r.message.chat.type + }) + } + $Collections + } + else { + $result.result.message.text + } + } } function Send-ToTelegram { param ( -[Parameter(Mandatory = $True)]$Text, -$token = "687...:AAF...", -$chat = "125468108" + [Parameter(Mandatory = $True)]$Text, + $Token = "687...:AAF...", + $Chat = "125468108", + $Keyboard ) -$endpoint = "sendMessage" -$url = "https://api.telegram.org/bot$token/$endpoint" -$Body = @{ -chat_id = $Chat -text = $Text -} -Invoke-RestMethod -Uri $url -Body $Body + $endpoint = "sendMessage" + $url = "https://api.telegram.org/bot$Token/$endpoint" + $Body = @{ + chat_id = $Chat + text = $Text + } + if ($keyboard -ne $null) { + $Body += @{reply_markup = $keyboard} + } + Invoke-RestMethod -Uri $url -Body $Body } $LastDate = (Get-FromTelegram -date)[-1].Date