【发布时间】:2018-08-09 18:27:47
【问题描述】:
我正在尝试从 VBA 应用程序向我的 Telegram 机器人发送内联键盘。我使用的两个字符串导致 HTTP 错误 400 Bad Request。
有谁知道如何构建以下两行来获得工作的内联 Telegram Bot 键盘?
Public Function SendTelegramMessage(strChatID As String, strMessage As String)
Dim objHTTP As Object
Dim strPostData As String
Dim strMarkup As String
strMarkup = "{""inline_keyboard"":[[{""text"":""A"",""callback_data"":""A1""},{""text"":""B"",""callback_data"":""C1""}]]}"
strPostData = "{""chat_id"": """ & strChatID & """, ""text"": """ & strMessage & """, ""reply_markup"": """ & strMarkup & """}"
Set objHTTP = CreateObject("winhttp.winhttprequest.5.1")
On Error Resume Next
objHTTP.Open "POST", gsTelegramAPIURL & gsTelegramAPIKey & "/sendMessage?", False
objHTTP.setRequestHeader "Content-Type", "application/json"
objHTTP.Send (strPostData)
If Err.Number = 0 Then
If objHTTP.Status = "200" Then
Debug.Print "HTTP POST request was succesfull"
ElseIf objHTTP.Status = "502" Then
Debug.Print "HTTP status: " & objHTTP.Status & ". HTTP statustext: " & objHTTP.StatusText
Set objHTTP = Nothing
Exit Function
ElseIf objHTTP.Status = "400" Then
Debug.Print "HTTP status: " & objHTTP.Status & ". HTTP statustext: " & objHTTP.StatusText
Set objHTTP = Nothing
Exit Function
Else
Debug.Print "HTTP status: " & objHTTP.Status & ". HTTP statustext: " & objHTTP.StatusText
End If
Else
'Unknown error, probably no internet connection.
Debug.Print "Error Number: " & Err.Number & " Error Source: " & Err.Source & " Error Description: " & Err.description
End If
Set objHTTP = Nothing
结束函数
【问题讨论】:
-
将有助于发布您的完整代码。
-
如你所问,我已经上传了完整的功能。
-
真的需要在
strPostData中引用strMarkup吗?这将为您提供一个带有嵌套对象的 json 对象,但该嵌套对象将表示为 json string (因此它本身不是对象)。这是你的意图吗? -
没有必要打算创建一个字符串。我只是想让格式正确,所以我知道如何以 VBA 格式创建一个完整的 json 字符串。在所有其他语言中,都有 json 库来创建对象和 VBA 中缺少的参数。
-
在 VBA 中你可以使用这个库:github.com/VBA-tools/VBA-JSON
标签: json string vba bots telegram