$URL_get = "https://api.telegram.org/bot$token/getUpdates"
$URL_set = "https://api.telegram.org/bot$token/sendMessage"
$token = "TOKEN"
# timeout sec
$timeout = 1
function getUpdates($URL) {
$json = Invoke-RestMethod -Uri $URL
$data = $json.result | Select-Object -Last 1
#Make variable nulliable
$text = $null
$callback_data = $null
$chat_id = $null
# Нажатие на кнопку
if ($data.callback_query) {
$callback_data = $data.callback_query.data
$chat_id = $data.callback_query.chat.id
$f_name = $data.callback_query.from.first_name
$l_name = $data.callback_query.from.last_name
$username = $data.callback_query.chat.username
}
# Обычное сообщение
elseif ($data.message) {
$chat_id = $data.message.chat.id
$text = $data.message.text
$f_name = $data.message.chat.first_name
$l_name = $data.message.chat.last_name
$type = $data.message.chat.type
$username = $data.message.chat.username
}
$ht = @{}
$ht["chat_id"] = $chat_id
$ht["text"] = $text
$ht["f_name"] = $f_name
$ht["l_name"] = $l_name
$ht["username"] = $username
$ht["callback_data"] = $callback_data
# confirm
Invoke-RestMethod "$($URL)?offset=$($($data.update_id)+1)" -Method Get | Out-Null
return $ht
}
function sendMessage($URL, $chat_id, $text) {
#Create a Hash Table
$ht = @{
text = $text
parse_mode = "Markdown"
chat_id = $chat_id
}
#Convert data to JSON
$json = $ht | ConvertTo-Json
Invoke-RestMethod $URL -Method Post -ContentType 'application/json; charset=utf-8' -Body $json | Out-Null
}
function sendMessagePr($URL, $chat_id, $text) {
#Create a Hash Table
$ht = @{
text = $text
parse_mode = "HTML"
chat_id = $chat_id
}
#Convert data to JSON
$json = $ht | ConvertTo-Json
Invoke-RestMethod $URL -Method Post -ContentType 'application/json; charset=utf-8' -Body $json | Out-Null
}
#Buttons (MAIN)
###############
$buttonsmain = @()
$button0 = @{ "text" = "Text1"; callback_data = "Data1" }
$button1 = @{ "text" = "Text2"; callback_data = "Data2" }
$button2 = @{ "text" = "Website"; "url" = 'https://google.com/' }
$button3 = @{ "text" = "Text3"; callback_data = "Data3" }
$rowmain1 = @($button0, $button1)
$rowmain2 = @($button3, $button2)
$buttons = ($rowmain1, $rowmain2)
#$buttons = ($button0, $button1, $button2, $button3)
$buttonsmain = @{"inline_keyboard" = $buttons } | ConvertTo-Json -Depth 10
while ($true) { # вечный цикл
# вызываем функцию
$returns = @()
$returns = getUpdates $URL_get
foreach ($return in $returns) {
If ($return.text -eq "B") {
$textt = "Hello, *$($return["f_name"])*! Please, choose the option you need:"
$chat_id = $return.chat_id
$chat_id
$username = $return.username
$keybrd = sendKeyboardMain $URL_set $buttonsmain $chat_id $textt
}
ElseIf ($return.callback_data -eq "Data2") {
$text = "Please, $username choose the option you need:"
$keybrd = sendKeyboardMain $URL_set $buttonsmain $chat_id $text
}
#Else {}
Start-Sleep -s $timeout
}
}
while ($true) {
$return = getUpdates $URL_get
#If the MSG is in default text
if ($return.text) {
#write-host "$($return["chat_id"])"
#Write-Host "$($return["msg_id"])"
switch -Wildcard ($return["text"]) {
"*" {
$text = "Hello, *$($return["f_name"])*! Please, choose the option you need:"
$chat_id = $return.chat_id
$keybrd = sendKeyboardMain $URL_set $buttons $chat_id $text
#Get MSG ID from MAIN keybrd
$last_bot_msg = $keybrd.result.message_id
}
}
}
#If button has been pressed
elseif ($return.callback_data) {
#If GET REPORT button pressed
If ($return.callback_data -eq "SQLPredReport") {
#Check if user has access (subscription)
$useraccessbtns = UserAccessToButtons
If ($useraccessbtns -eq $true) {
$keybrd = Invoke-RestMethod -Uri "https://api.telegram.org/bot$($token)/editMessageText?chat_id=$($return.chat_id)&message_id=$($last_bot_msg)&text=Choose period:&reply_markup=$buttonreport" -ContentType "application/json; charset=utf-8"
$last_bot_msg = $keybrd.result.message_id
#Loop until Back button will be pressed
Do {
$return = getUpdates $URL_get
#If TODAY btn pressed
If ($return.callback_data -eq "reptoday") {
$reportdate = (Get-Date).ToString('yyyy-MM-dd')
#Run SQL Report (TODAY) function
$SQLReport = GetReportTodayYesterday
sendMessage $URL_set $($return.chat_id) $SQLReport
#Delete previous MSG with keyboard
Invoke-RestMethod -Uri "https://api.telegram.org/bot$($token)/deleteMessage?chat_id=$($return.chat_id)&message_id=$($last_bot_msg)"
#Send Report keybrd again
$text = "Choose period:"
$keybrd = sendKeyboardReport $URL_set $buttonreport $chat_id $text
$last_bot_msg = $keybrd.result.message_id
}
Пример,