Thursday, June 2, 2022

Telegram - Send Photo via VBS

Const URL = "https://api.telegram.org/"
Const botID = "botxxxxxxxxx"
Const chatID = "xxxxxx"
methodeName = "/sendPhoto?chat_id=" & chatID
Const sFolder = "C:\"
Const sFile = "GO.png"

Dim data
Set data = CreateObject("Scripting.Dictionary")
data.Add "chatID", chatID
data.Add "text", "TES"

Dim BOUNDARY, s, n 
For n = 1 To 16: s = s & Chr(65 + Int(Rnd * 25)): Next
BOUNDARY = s & CDbl(Now)

Dim part, ado
For Each key In data.keys
part = part & "--" & BOUNDARY & vbCrLf
part = part & "Content-Disposition: form-data; name=""" & key & """" & vbCrLf & vbCrLf
part = part & data(key) & vbCrLf
Next

' filename
part = part & "--" & BOUNDARY & vbCrLf
part = part & "Content-Disposition: form-data; name=""photo""; filename=""" & sFile & """" & vbCrLf & vbCrLf
'msgbox part

' read jpg file as binary
Dim jpg
Set ado = CreateObject("ADODB.Stream")
ado.Type = 1 'binary
ado.Open
ado.LoadFromFile sFolder & sFile
ado.Position = 0
jpg = ado.read
ado.Close
' combine part, jpg , end
ado.Open
ado.Position = 0
ado.Type = 1 ' binary
ado.Write ToBytes(part)
ado.Write jpg
ado.Write ToBytes(vbCrLf & "--" & BOUNDARY & "--")
ado.Position = 0

Dim req, reqURL
Set req = CreateObject("MSXML2.XMLHTTP")
reqURL = URL & botID & methodeName

'MsgBox reqURL
With req
.Open "POST", reqURL, False
.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & BOUNDARY
.send ado.read
'MsgBox BOUNDARY
'MsgBox .responseText
End With

Function ToBytes(str)
    Dim ado
    Set ado = CreateObject("ADODB.Stream")
    ado.Open
    ado.Type = 2 ' text
    ado.Charset = "_autodetect"
    ado.WriteText str
    ado.Position = 0
    ado.Type = 1
    ToBytes = ado.read
    ado.Close
End Function

Telegram - Get Message via VBS

 Dim http,URL


' you can get this json script from post: VBS - Extract JSON

Set fso = CreateObject("Scripting.FileSystemObject")

executeGlobal fso.openTextFile(strPath & "ExtractJSON.vbs" ).readAll()

'=====================


botID = "botxxxxxxx"


URL = "https://api.telegram.org/" & botID & "/getUpdates"

Set http = CreateObject("Msxml2.XMLHTTP")

http.open "GET",URL,False

'http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

http.send


strJson = http.responseText

msgbox strJson ' showing real data from telegram


Result = Extract(strJson,"(\x22(.*)\x22)")

msgbox Result ' showing data from telegram after extract by ExtractJSON

VBS - Extract JSON

 '******************************************

Function Extract(Data,Pattern)

   Dim oRE,oMatches,Match,Line

   set oRE = New RegExp

   oRE.IgnoreCase = True

   oRE.Global = True

   oRE.Pattern = Pattern

   set oMatches = oRE.Execute(Data)

   If not isEmpty(oMatches) then

       For Each Match in oMatches  

           Line = Line & Trim(Match.Value) & vbCrlf

       Next

       Extract = Line

   End if

End Function

'******************************************

Telegram - Send Message via VBS

read first to get chatID and botID from : https://core.telegram.org/bots


' ===== Start Script =====

Dim http,URL

strTxt = "Tes Message"

chatID = "xxxxxxxx"

botID = "botxxxxxxxxxxxxxxx"


URL = "https://api.telegram.org/" & botID & "/sendMessage?chat_id=" & chatID & "&text=" & strTxt

Set http = CreateObject("Msxml2.XMLHTTP")

http.open "GET",URL,False

http.send