{"id":8367,"date":"2023-12-15T00:10:11","date_gmt":"2023-12-15T00:10:11","guid":{"rendered":"https:\/\/cheapwindowsvps.com\/blog\/how-to-send-telegram-messages-from-a-powershell-script\/"},"modified":"2025-01-20T10:21:08","modified_gmt":"2025-01-20T10:21:08","slug":"how-to-send-telegram-messages-from-a-powershell-script","status":"publish","type":"post","link":"https:\/\/cheapwindowsvps.com\/blog\/how-to-send-telegram-messages-from-a-powershell-script\/","title":{"rendered":"How to Send Telegram Messages from a PowerShell Script"},"content":{"rendered":"<p><p>You can use your Telegram messenger as a notification tool to get instant reports on various infrastructure events, script execution results, or scheduler tasks. This article shows you how to use PowerShell to send a text notification to a Telegram channel or group through the Bot API.<\/p>\n<\/p>\n<p><p>First, create a new Telegram bot using <code>@BotFather<\/code>. Find it in your Telegram client and send it the following commands:<\/p>\n<\/p>\n<p><p><code>\/start<\/code><br \/><code>\/newbot<\/code><\/p>\n<\/p>\n<p><p>Specify the bot\u2019s name and username. BotFather will generate an HTTP token that you must copy and save.<\/p>\n<\/p>\n<p><p>To send a message to a Telegram chat or a specific user, you need to know their ID. In this example, I will send notifications to myself, so use <code>@my_id_bot<\/code> to find my Telegram ID:<\/p>\n<p><code>\/start<\/code><\/p>\n<pre>Your user ID: 987654321<\/pre>\n<\/p>\n<p><p>To send a message to any Telegram chat, you will need to designate the bot token as well as the ID of your intended user (or conversation):<\/p>\n<\/p>\n<p><p><code>$tg_token=\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"<br \/> $tg_chat_id=\"987654321\"<\/code><\/p>\n<\/p>\n<p><p>Connection to the Telegram API necessitates the TLS 1.2 protocol. Make sure to enable the <a href=\"https:\/\/woshub.com\/enable-tls-1-2-windows\/\" rel=\"nofollow noopener\" target=\"_blank\">version of the TLS 1.2 protocol<\/a> in&nbsp; Windows. Ordinarily, PowerShell establishes connections using the aged SSL 3.0, TLS 1.0, or TLS 1.1 protocols. Execute the command shown below to engage the TLS version 1.2 within the current PowerShell session:<\/p>\n<\/p>\n<p><p><code>[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12<\/code><\/p>\n<\/p>\n<p><p>And here\u2019s a command to send a message to Telegram:<\/p>\n<\/p>\n<p><p><code>$message=\"Test message alert from PowerShell\"<br \/> $Response = Invoke-RestMethod -Uri \"https:\/\/api.telegram.org\/bot$($tg_token)\/sendMessage?chat_id=$($tg_chat_id)&amp;text=$($Message)\" rel=\"nofollow\" target=\"_blank\"&gt;<\/code><\/p>\n<\/p>\n<p><p>Once you run that one, you should get a message from the bot.<\/p>\n<\/p>\n<p><p>You can use emoji and HTML text formatting to make notifications more visually appealing and readable:<\/p>\n<\/p>\n<p><pre>&lt;code&gt;$message= $currend_data + \"\u26a0\ufe0f Update Script &lt;b&gt;SAP_DB_Update&lt;\/b&gt; completed with errors\"<br> $Response = Invoke-RestMethod -Uri \"https:\/\/api.telegram.org\/bot$($tg_token)\/sendMessage?chat_id=$($tg_chat_id)&amp;text=$($Message)&amp;parse_mode=html\"&lt;\/code&gt;<\/pre>\n<\/p>\n<p><p> To work with PowerShell scripts, I recommend using the VS Code editor.<\/p>\n<\/p>\n<p><p>If you are accessing the Internet through a proxy server, you can use the &lt;code&gt;-Proxy&lt;\/code&gt; parameter of the <a href=\"https:\/\/woshub.com\/parsing-html-webpages-with-powershell\/\" rel=\"nofollow noopener\" target=\"_blank\">Invoke-WebRequest cmdlet<\/a> to specify the proxy settings. Use the &lt;code&gt;-ProxyCredential&lt;\/code&gt; argument to authenticate to a proxy.<\/p>\n<\/p>\n<p><p><code>$Response = Invoke-RestMethod -Uri \"https:\/\/api.telegram.org\/bot$($Telegramtoken)\/sendMessage?chat_id=$($Telegramchatid)&amp;text=$($Message)\" \u2013Proxy \"http:\/\/192.168.13.155:3128\"<\/code><\/p>\n<\/p>\n<p><p>In <a href=\"https:\/\/woshub.com\/install-update-powershell-windows\/\" rel=\"nofollow noopener\" target=\"_blank\">version PowerShell Core 7.x<\/a>+, the Invoke-WebRequest cmdlet uses the proxy settings specified in the environment variables. Learn more about <a href=\"https:\/\/woshub.com\/using-powershell-behind-a-proxy\/\" rel=\"nofollow noopener\" target=\"_blank\">using a proxy in PowerShell<\/a>.<\/p>\n<\/p>\n<p><p>[\/alert]<\/p>\n<\/p>\n<p><p>You have the ability to construct a function from a script that transmits a message to Telegram and attach it to the PowerShell profile file located in Windows:<\/p>\n<\/p>\n<p><pre><\/p><p>function Send-Telegram {<\/p><p>        [CmdletBinding()]<\/p><p>        param(<\/p><p>            [Parameter()]<\/p><p>            [string] $Message<\/p><p>        )    <\/p><p>        $tg_token=\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"<\/p><p>        $tg_chat_id=\"987654321\"<\/p><p>        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12<\/p><p>        $Response = Invoke-RestMethod -Uri \"https:\/\/api.telegram.org\/bot$($tg_token)\/sendMessage?chat_id=$($tg_chat_id)&amp;text=$($Message)&amp;parse_mode=html\" <\/p><p>        return $Response    <\/p><p> }<\/p><p><\/pre>\n<\/p>\n<p><p>Launch a text document with a <a href=\"https:\/\/woshub.com\/powershell-profile-files\/\" rel=\"nofollow noopener\" target=\"_blank\">PowerShell profile<\/a> that gets automatically implemented when you start powershell.exe\/pwsh.exe:<\/p>\n<\/p>\n<p><p><code>notepad $PSHOMEProfile.ps1<\/code><\/p>\n<\/p>\n<p><p>With this, you now have the means to send a message to a Telegram channel originating from any PowerShell script.<\/p>\n<\/p>\n<div>\n<p><p><code>Send-Telegram \"My test message\"<\/code><\/p>\n<\/p>\n<div>\n<p>    If you use Teams as your primary messenger, you can also <\/p>\n<p>    <a href=\"https:\/\/woshub.com\/send-message-teams-channel-powershell\/\" rel=\"nofollow noopener\" target=\"_blank\">use PowerShell to send a message to a Teams channel<\/a>.<\/p>\n<\/div>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>You can use your Telegram messenger as a notification tool to get instant reports on various infrastructure events, script execution results, or scheduler tasks. This article shows you how to use PowerShell to send a text notification to a Telegram channel or group through the Bot API. First, create a new Telegram bot using @BotFather. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8368,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117,98,100],"tags":[],"class_list":["post-8367","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","category-windows-10","category-windows-server-2019"],"_links":{"self":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/8367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/comments?post=8367"}],"version-history":[{"count":2,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/8367\/revisions"}],"predecessor-version":[{"id":10369,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/8367\/revisions\/10369"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media\/8368"}],"wp:attachment":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media?parent=8367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/categories?post=8367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/tags?post=8367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}