Skip to content

Instantly share code, notes, and snippets.

@eip
Last active November 8, 2024 12:47
Show Gist options
  • Save eip/bc6cb1c7b012d5c7517a8257909b2463 to your computer and use it in GitHub Desktop.
Save eip/bc6cb1c7b012d5c7517a8257909b2463 to your computer and use it in GitHub Desktop.
Send messages via Telegram Bot API
Option Explicit
Public Function StrFormat(format, arguments())
Dim value, num
StrFormat = format
num = 0
For Each value In arguments
StrFormat = Replace(StrFormat, "{" & num & "}", value)
num = num + 1
Next
End Function
Public Function StrEscape(value)
StrEscape = value
StrEscape = Replace(StrEscape, "\", "\\")
StrEscape = Replace(StrEscape, """", "\""")
StrEscape = Replace(StrEscape, "/", "\/")
StrEscape = Replace(StrEscape, vbCr, "\r")
StrEscape = Replace(StrEscape, vbLf, "\n")
StrEscape = Replace(StrEscape, vbTab, "\t")
End Function
Class TGMessage
Public chat_id
Public text
Public Function ToJson()
ToJson = StrFormat("{""chat_id"":{0},""text"":""{1}""}", Array(chat_id, StrEscape(text)))
End Function
End Class
Dim token, url, args, msg_file, msg, msgBody, httpRequest, debugMode
debugMode = False
Set msg = New TGMessage
Dim arg, cmd
For Each arg In WScript.Arguments
If LCase(arg) = "/debug" Or LCase(arg) = "-debug" Then
debugMode = True
Else
cmd = Split(arg, ":", 2)
If UBound(cmd) = 1 Then
Select Case LCase(cmd(0))
Case "/token", "-token"
token = Trim(cmd(1))
Case "/chat_id", "-chat_id"
msg.chat_id = Trim(cmd(1))
Case "/msg", "-msg"
msg.text = Trim(cmd(1))
Case "/msg_file", "-msg_file"
msg_file = Trim(cmd(1))
End Select
End If
End If
Next
If Len("" & token) = 0 Or Len("" & msg.chat_id) = 0 Or (Len("" & msg.text) = 0 And Len("" & msg_file) = 0) Then
WScript.Echo "Sends messages via Telegram Bot API." & vbCrLf & vbCrLf & _
"Usage:" & vbCrLf & _
"cscript tg_send.vbs /token:123456789:ABCdeFG1hij2klmnOP3-_4qr5StuV6wxYz7 /chat_id:123456789 /msg:""Hello, World!""" & vbCrLf & _
"or" & vbCrLf & _
"cscript tg_send.vbs /token:123456789:ABCdeFG1hij2klmnOP3-_4qr5StuV6wxYz7 /chat_id:123456789 /msg_file:C:\Folder\Message.txt"
WScript.Quit -1
End If
url = StrFormat("https://api.telegram.org/bot{0}/sendMessage", Array(token))
If Len("" & msg.text) = 0 Then
Dim fso, file
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(msg_file) Then
msg.text = Trim(fso.OpenTextFile(msg_file, 1).ReadAll)
End If
End If
If Len("" & msg.text) = 0 Then
WScript.Echo "The message is empty. Nothing to send."
WScript.Quit -1
End If
msgBody = msg.ToJson()
If debugMode Then
WScript.Echo StrFormat("Sending message" & vbCrLf & "url: {0}" & vbCrLf & "msgBody: {1}" & vbCrLf, Array(url, msgBody))
End If
Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
httpRequest.Open "POST", url, False
httpRequest.setRequestHeader "Content-Type", "application/json; charset=utf-8"
httpRequest.SetTimeouts 10000, 10000, 10000, 10000
httpRequest.Send msgBody
WScript.Echo StrFormat("Response from server: ({0}) {1}", Array(httpRequest.Status, httpRequest.StatusText))
If debugMode Then
WScript.Echo httpRequest.ResponseText
End If
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment