Skip to content

Instantly share code, notes, and snippets.

@vyach-vasiliev
Created April 9, 2022 10:50
Show Gist options
  • Save vyach-vasiliev/eef25097efe3e94cee9f27d90ad24758 to your computer and use it in GitHub Desktop.
Save vyach-vasiliev/eef25097efe3e94cee9f27d90ad24758 to your computer and use it in GitHub Desktop.
Example notification bot with PS script

Создание бота:

  1. Пройдите регистрацию в telegram
  2. Получите идентификатор нового бота (далее <Bot_token>): обратитесь к боту @BotFather c требованием создать нового бота (команда /newbot)
  3. Получите Идентификатор беседы с ботом (далее <chat_id>):
  4. Откройте диалог со своим созданным ботом и напишите ему произвольное сообщение
  5. Откройте в браузере ссылку, заменив <Bot_token> на полученный идентификатор от @BotFather https://api.telegram.org/bot<Bot_token>/getUpdates
  6. В полученном json-ответе найдите значение в параметре result->message->chat->id, это и есть
  7. Откройте браузер и перейдите по ссылке, заменив <Bot_token> и <chat_id> на свои данные https://api.telegram.org/bot<Bot_token>/sendMessage?chat_id=<chat_id>&text=Привет%20мир

Скрипт LogonNotificationWS19.ps1 можно пометсить в планировщик задач Windows OS:

  • C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe LogonNotification.ps1
[String]$var = (Get-ChildItem Env:\USERNAME).Value
[String]$comp = hostname
[String]$date = Get-Date -DisplayHint Date
$JSONBody = [PSCustomObject][Ordered]@{
"@type" = "MessageCard"
"@context" = "<http://schema.org/extensions>"
"chat_id" = '111111'
"text" = "
Server Logon Alert!
$var logged into $comp on $date.
"
}
$MessageBody = ConvertTo-Json $JSONBody
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
$parameters = @{
"URI" = 'https://api.telegram.org/bot111:AAA/sendmessage'
"Method" = 'POST'
"Body" = $MessageBody
"ContentType" = 'application/json; charset=utf-8'
}
Invoke-RestMethod @parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment