通过VBA将本地照片发送到Telegram

时间:2019-11-22 08:39:11

标签: vba telegram telegram-bot

我正在尝试使用VBA或VBScript发送本地照片,但是我发现的所有解决方案都是发送URL而不是上传实际文件,或者是VBA或VBScript以外的任何其他解决方案。到目前为止,这是我的代码:

Sub TelegramAuto()

Dim ws As Worksheet
Set ws = Sheets("hidden")
Set ws1 = Sheets("Dashboard")

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

 strChatId = <id>
 strMessage = ws.Range("J5") & Format(ws1.Range("D2"), "mm/dd/yyyy") & " " & ws1.Range("D4") & " " & ws1.Range("D6") _
                & " " & ws1.Range("K6")
 strPhoto = "C:/Users/mhjong/Desktop/GP_FS_Breakdown.png"

strPostData = "chat_id=" & strChatId & "&text=" & 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)
End With

With objRequest
  .Open "POST", "https://api.telegram.org/bot<token>/sendPhoto?", False
  .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  .Send (strPostPhoto)
End With

End Sub

我只能发送找到的消息,但是我一生都找不到合适的语法来上传本地图像并将其发送到Telegram。

1 个答案:

答案 0 :(得分:0)

strPhoto = "image link"
strPostPhoto = "chat_id=" & strChatId & "&photo=" & strPhoto
With objRequest
  .Open "POST", "https://api.telegram.org/bot<Token>/sendPhoto?" & strPostPhoto, False
  .send
End With