1

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
braX
  • 11,506
  • 5
  • 20
  • 33

1 Answers1

2

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 »

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
  • Thank you very mutch for your answer! It works fine!!! Can you show me how it works with a lokal picture? My pictures are on my disk and not in the web. (multipart/from-data i dont understand) – Connor MacLeod Jun 02 '18 at 13:08
  • yes, but they are allways for libraries (for example python). telegram itself has little internet entries for beginners. you have to upload the picture first. but I do not find real examples. https://core.telegram.org/bots/api#sending-files – Connor MacLeod Jun 02 '18 at 14:19
  • 1
    If you need to submit photos that way, you could do more research to see what others have done, or contact the developer, or submit a request on their GitHub site. Alternatively, you could use workarounds like programmatically uploading the images to the web and then call them via the URL, or else submit them programmatically or manually via their own web interface. Also be sure to check the regular ("non-bot") API section for some clues there. – ashleedawg Jun 02 '18 at 16:03
  • I think you want to post a Byte array, e.g. `Byte()`. Maybe this will help: https://stackoverflow.com/questions/1356118/vba-ws-toolkit-how-to-get-current-file-as-byte-array – Ryan Wildry Jun 02 '18 at 23:38