Created
October 3, 2018 16:19
-
-
Save mtrimarchi/f3f106dcdff926bb496e5f2a65d68d9a to your computer and use it in GitHub Desktop.
Telegram Bot send message with Emoji
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function U | |
{ | |
param | |
( | |
[int] $Code | |
) | |
if ((0 -le $Code) -and ($Code -le 0xFFFF)) | |
{ | |
return [char] $Code | |
} | |
if ((0x10000 -le $Code) -and ($Code -le 0x10FFFF)) | |
{ | |
return [char]::ConvertFromUtf32($Code) | |
} | |
throw "Invalid character code $Code" | |
} | |
$token_id = "123456789:ABCdefGHijKlMnOpQRSTuvwxYz123456789" | |
$chat_id = "-1001234567890" | |
$payload = @{ | |
"chat_id" = $chat_id; | |
"text" = "This is a test from curl $(U 0x1F525)"; | |
"parse_mode" = "Markdown"; | |
"disable_web_page_preview" = "True"; | |
} | |
Try { | |
$res = Invoke-WebRequest ` | |
-Uri ("https://api.telegram.org/bot{0}/sendMessage" -f $token_id) ` | |
-Method Post ` | |
-ContentType "application/json;charset=utf-8" ` | |
-Body (ConvertTo-Json -Compress -InputObject $payload) | |
Write-host "Status Code: " $res.StatusCode | |
Write-host $res.Content | |
} | |
Catch { | |
$result = $_.Exception.Response.GetResponseStream() | |
$reader = New-Object System.IO.StreamReader($result) | |
$reader.BaseStream.Position = 0 | |
$reader.DiscardBufferedData() | |
$responseBody = $reader.ReadToEnd(); | |
Write-host "Status Code: " $_.Exception.Response.StatusCode | |
Write-host $responseBody | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment