1
votes

I send messages form Excel to telegram. It works nice. But how can I send a photo? I don't understand it (https://core.telegram.org/bots/api#sendphoto)

Thanks for help!

My send Message:

Dim objRequest As Object
Dim strChatId As String
Dim strMessage As String
Dim strPostData As String
Dim strResponse As String

 strChatId = Worksheets("Einstellungen").Cells(3, "AB")
 strMessage = Report
 APIcode = Worksheets("Einstellungen").Cells(2, "AB")

strPostData = "chat_id=" & strChatId & "&text=" & strMessage

 Set objRequest = CreateObject("MSXML2.XMLHTTP")
With objRequest
  .Open "POST", "https://api.telegram.org/" & APIcode & "/sendMessage?", False
  .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  .send (strPostData)
   GetSessionId = .responseText
End With
1

1 Answers

2
votes

If your code is working as-is for plain text messages then you should only need to make a couple changes to it.

You're probably currently using the API's sendMessage method, which takes the chat_id and text parameters.

You want to use the sendPhoto method, which tales the chat_id and photo parameters (but no text parameter).

So this is a bit of a shot in the dark since I've never used or heard of Telegram and I don't have a key, so I can't test it, but theoretically, you could send a photo from a URL like this:

Sub telegram_SendPhoto()

    Const photoURL = "https://i.imgur.com/0eH6d1v.gif" 'URL of photo

    Dim objRequest As Object, strChatId As String, APIcode As String
    Dim strPostData As String, strResponse As String

    strChatId = Worksheets("Einstellungen").Cells(3, "AB")
    APIcode = Worksheets("Einstellungen").Cells(2, "AB")

    strPostData = "chat_id=" & strChatId & "&photo=" & photoURL

    Set objRequest = CreateObject("MSXML2.XMLHTTP")
    With objRequest
        .Open "POST", "https://api.telegram.org/" & APIcode & "/sendPhoto?", False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .send (strPostData)
        strResponse = .responseText
    End With

    MsgBox strResponse

End Sub

Pass a file_id as String to send a photo that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a photo from the Internet (above), or upload a new photo using multipart/form-data. More info on Sending Files ยป