我使用以下代码发送本地图像。
将图像文件转换为base64>通过http multipart / form-data表单发布
您知道为什么失败吗?请回答我。谢谢
响应的文本= {“确定”:false,“错误代码”:504,“描述”:“网关超时”}
Sub telegram_SendPhoto()
botid = Cells(1, 2) 'my bot id
apikey = Cells(2, 2) 'token
boundary = "287032381131322"
filebase64 = ConvertFileToBase64("C:\Users\youngsu.jeong\Desktop\3.png") 'local file to send
strPostData = "--" & boundary & "\r\n" & "Content-Disposition: form-data; name=""chat_id""" & "\r\n" & "\r\n" & botid & "\r\n" & _
"--" & boundary & "\r\n" & "Content-Disposition: form-data; name=""photo""; filename=""1.txt""" & "\r\n" & _
"Content-Type: image/png" & "\r\n" & "\r\n" & filebase64 & "\r\n" & _
"--" & boundary & "--"
Set objRequest = CreateObject("MSXML2.XMLHTTP")
With objRequest
.Open "POST", "https://api.telegram.org/bot" & apikey & "/sendPhoto?", False
.SetRequestHeader "Content-Type", "multipart/form-data; boundary=287032381131322"
.Send (strPostData)
GetSessionId = .ResponseText
Debug.Print GetSessionId
End With
End Sub
Public Function ConvertFileToBase64(strFilePath As String) As String
Const UseBinaryStreamType = 1
Dim streamInput: Set streamInput = CreateObject("ADODB.Stream")
Dim xmlDoc: Set xmlDoc = CreateObject("Microsoft.XMLDOM")
Dim xmlElem: Set xmlElem = xmlDoc.createElement("tmp")
streamInput.Open
streamInput.Type = UseBinaryStreamType
streamInput.LoadFromFile strFilePath
xmlElem.DataType = "bin.base64"
xmlElem.nodeTypedValue = streamInput.Read
ConvertFileToBase64 = Replace(xmlElem.Text, vbLf, "")
Set streamInput = Nothing
Set xmlDoc = Nothing
Set xmlElem = Nothing
End Function
strPostData如下所示
'-287032381131322内容处置:表格数据; name =“ chat_id”
842411059
-287032381131322
Content-Disposition:表单数据; name =“ photo”; filename =“ 1.png” 内容类型:image / png
iVBORw0KGgoAAAANSUhEUgAAAEAAAAAvCAYAAABTy8xRAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAA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FoPXsUXGeOoCwWlmBxdVkIjW8 / 82iessLItEQAtkgRE7PT6POsxIo9oBocAXtbAPmniAisR40jQCNgC / 4ANLqlr8Ak7vkAAAAASUVORK5CYII =
-287032381131322-