0

Recently i am doing project where i need to get images from my local storage and send to telegram using Microsoft Excel VBA.

I cant find any solution to this matter, however i can send text message to telegram using VBA but just not photos.

Below is what i did

Private Sub CommandButton1_Click()
' Program Excel Messenger
' Purpose : Send Message to Telegram

Dim objRequest As Object
Dim strChatId As String
Dim strMessage As String
Dim strPhoto As String
Dim messageDate As Variant
Dim strPostData As String
Dim strResponse As String

    strChatId ='Group chat ID
    strMessage = Range("A2").Value 'Get value of Cell A2
    strPhoto = "<local image file path>" 'Get value of Cell A3
    messageDate = Now() 'Get current Date and Time
    
        'Detect the value in Cell A1
    If IsEmpty(Range("A1").Value) Then
        'Do Nothing
    Else
        'Send Messsage
        strPostData = "chat_id=" & strChatId & "&text=" & messageDate & vbNewLine & strMessage
        strPostPhoto = "chat_id=" & strChatId & "&photo=" & strPhoto
        
        Set objRequest = CreateObject("MSXML2.XMLHTTP")
        
        With objRequest
            .Open "POST", "https://api.telegram.org/bot<token>/sendMessage?", False
            .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
            .Send (strPostData)
            GetSessionId = .responseText
            MsgBox GetSessionId
        End With
        
        With objRequest
            .Open "POST", "https://api.telegram.org/bot<token>/sendPhoto?", False
            .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
            .Send (strPostPhoto)
            GetSessionId = .responseText
            MsgBox GetSessionId
        End With
        
    End If


End Sub

However i get this error when i click on the button to run the above code

-Bad request wrong HTTP URL specified

Steve Kush
  • 143
  • 1
  • 13

0 Answers0