将照片发送到Telegram(API / Bot)

时间:2018-06-02 09:44:29

标签: excel vba excel-vba telegram telegram-bot

我将Excel中的消息发送到电报。它很好用。 但是我该如何发送照片呢?我不明白(https://core.telegram.org/bots/api#sendphoto

感谢您的帮助!

我的发送消息:

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 个答案:

答案 0 :(得分:1)

如果您的代码按原样用于纯文本消息,那么您只需要对其进行一些更改。

您目前可能正在使用API​​的sendMessage方法,该方法采用chat_idtext参数。

您想要使用sendPhoto方法,该方法会记录chat_idphoto参数(但没有text参数)。

所以这是黑暗中的一点点,因为我从来没有使用或听说过电报,而且我没有钥匙,所以我无法测试它,但理论上,你可以发一张照片这样的网址:

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
  

file_id作为字符串传递给Telegram服务器上存在的照片(推荐),将HTTP URL作为电子邮件的字符串传递给从互联网上方获取照片(上图),或使用multipart/form-data上传新照片。 More info on Sending Files »