我想用VBScript创建我的机器人(我可能知道它像巨魔和坏主意,我可以用lua,python,C#,PHP等来实现它,但是我决定尝试从vbscript制作它) 困难的部分是我正在尝试从Telegram getUpdates检索信息
例如,我已经编写了这段代码,并且可以正常工作,我将解释什么有效,什么无效
Dim fso, outFile, TeleTest
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("output.txt", True)
set TeleTest = fso.CreateTextFile("TeleTest.txt", True)
Dim url, req, json
Set req = CreateObject("MSXML2.XMLHTTP")
url = "https://api.telegram.org/bot"[TOKEN]"/getUpdates"
req.open "GET", url, False
req.send
If req.Status = 200 Then
TeleTest.Write req.responseText
End If
' Load the JSON array into a JsonArray:
set jsonArray = CreateObject("Chilkat_9_5_0.JsonArray")
success = jsonArray.Load("TeleTest.txt")
If (success <> 1) Then
outFile.WriteLine(jsonArray.LastErrorText)
WScript.Quit
End If
' Get some information from each record in the array.
numRecords = jsonArray.Size
i = 0
Do While i < numRecords
outFile.WriteLine("------ Record " & i & " -------")
' jsonRecord is a Chilkat_9_5_0.JsonObject
Set jsonRecord = jsonArray.ObjectAt(i)
outFile.WriteLine(" ok: " & jsonRecord.StringOf("ok"))
outFile.WriteLine(" result: " & jsonRecord.SizeOfArray("result"))
' Examine information for this record
u = 0
Do While u < nummessage
nummessage = jsonRecord.SizeOfArray("result[u].message")
Loop
outFile.WriteLine("Number of message: " & nummessage)
j = 0
Do While j < nummessage
jsonRecord.J = j
outFile.WriteLine(" message text: " & jsonRecord.StringOf("result[j].message[j].text"))
j = j + 1
Loop
i = i + 1
Loop
outFile.Close
因此,应该获取更新并将其保存在TeleTest.txt中的第一部分工作正常,可以获取更新,它将json保存到.txt文件中(或其他任何方式,我也可以将其保存到vbs中的字符串中,或.json文件)
问题是我正在使用Chilkat的第二部分给出了错误
Blockquote ChilkatLog:加载: Chilkat版本:9.5.0.78 无法获得索引0处的数组。 --ChilkatLog
任何帮助或任何想法都会受到赞赏,如果奇尔卡特(Chilkat)不擅长这样做,也许告诉我为什么并给我其他东西? (Chilkat是我发现可以与vbscript一起使用的唯一dll,并且可以读取json)
答案 0 :(得分:1)
我开始工作了,我从这个example中发现了这一点 Chilkat需要Json文件喜欢
[{json}]
但是Telegram json就是这样
{json}
因此,只需将TeleTest.Write req.responseText的第15行更改为下面的代码,此修复很容易
TeleTest.Write "[" + req.responseText + "]"
我的代码现在可以正常工作,如果其他人发现了错误或对我的问题的任何答案,将不胜感激 我希望其他需要这个的人找到这个