Bước 1: Tạo file có tên Sendfile_Telegram.ps1
Bước 2: Sau đó mở file copy script bên dưới và chỉnh sửa 1 số chỗ thích hợp:
$host.UI.RawUI.WindowTitle = "Bot Send File to Telegram"
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "I:\CopyDevL"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $false
$action = {
$path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), $changeType, $path"
#Add-content "I:\CopyDevL\log.txt" -value $logline
# File action
$FilePath = $watcher.Path
$LatestFileName = Get-ChildItem -Path $FilePath -File |
Sort-Object LastWriteTime -Descending | Select-Object -First 1
#Start-Process -FilePath $LatestFileName.FullName
#Bot Telegram
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Thay đổi token và chat ID:
$botToken = "631272:AAFWM4LQKKmsdaswXDEDITVREWjqZGoWI"
$chatID = "-10334333112" # Check Bot
# Đường link tới thư mục
$pathLink = "I:\Copy\*"
$messageText = $logline
$url = "https://api.telegram.org/bot$botToken/sendMessage?chat_id=$chatID&text=$messageText"
Invoke-RestMethod -Uri $url -Method Post
#Send File Telegram
Send-TelegramLocalDocument -BotToken $botToken -ChatID $chatID -File $pathLink
}
#Register-ObjectEvent $watcher "Created" -Action $action | Format-Table -AutoSize
Register-ObjectEvent $watcher "Created" -Action $action
Register-ObjectEvent $watcher "Deleted" -Action $action
while ($true) {sleep 0}