本文来自于Thinkai’s Blog,thinkai也是我在ahk上面的引路人,对thinkai感兴趣的朋友请关注他的博客。
thinkai的博客简洁、有趣,即便是转载过来我也尽量保持作者原有的风格,主要是方便大家查阅。
thinkai的博客简洁、有趣,即便是转载过来我也尽量保持作者原有的风格,主要是方便大家查阅。
api := "http://www.tuling123.com/openapi/api" key := "aeaca4b118ae2d0802a214f5ca6cdaf3" userid := "666666" Gui, Add, Edit, x0 y0 w400 h400 ReadOnly vlog Gui, Add, Edit, x0 y400 w400 h100 vwrite Gui, Add, Button, x300 y500 w80 h20 gsend Default, 发送 Gui, +hwndmainhwnd Gui, Show, , 图灵文字测试 return GuiClose: ExitApp #If WinActive("ahk_id " mainhwnd) $Enter:: goto, send return ^Enter:: ControlSend, Edit2, {Enter}, ahk_id %mainhwnd% #if send: Gui, Submit, NoHide if write { AddText(write,"我") GuiControl, , write, % "" if A_IsUnicode { msg := {"key":key,"info":write,"userid":userid} json_msg := json_fromobj(msg) } else json_msg := "{""key"":""" key """,""info"":""" write """,""userid"":" userid "}" ;AddText(json_msg,"SendJSON") ret := HTTPPOST(api, "UTF-8", json_msg) ;AddText(ret,"系统") if (ret<>-1) { retobj := json_toobj(ret) if (retobj.code="100000") ;文字 AddText(retobj.text,"图灵") else if (retobj.code="200000") ;链接类 AddText(retobj.text "`n" retobj.url,"图灵") else if (retobj.code="302000") ;新闻类 { news := "" for k,v in retobj.list news .= "【" v.source "】" v.article " " v.detailurl "`n" AddText(retobj.text "`n" news,"图灵") } else if (retobj.code="308000") ;菜谱类 { cb := "" for k,v in retobj.list cb .= "【" v.name "】" v.info " " v.detailurl "`n" AddText(retobj.text "`n" cb,"图灵") } else AddText(retobj.code ":" retobj.text,"图灵") } } return AddText(text,person){ global log, mainhwnd GuiControlGet, log GuiControl, , log, % log "`n`n" person " (" A_Hour ":" A_Min ":" A_Sec ")`n" text ControlFocus, Edit2, ahk_id %mainhwnd% ControlSend, Edit1, {PGDN 3}, ahk_id %mainhwnd% } HTTPPOST(url, Encoding = "",postData=""){ ;网址,编码,请求方式,post数据 hObject:=ComObjCreate("WinHttp.WinHttpRequest.5.1") Try { hObject.Open("POST",url,False) hObject.SetRequestHeader("Content-Type", "application/json") hObject.Send(postData) } catch e return -1 if (Encoding && hObject.ResponseBody) { oADO := ComObjCreate("adodb.stream") oADO.Type := 1 oADO.Mode := 3 oADO.Open() oADO.Write(hObject.ResponseBody) oADO.Position := 0 oADO.Type := 2 oADO.Charset := Encoding return oADO.ReadText(), oADO.Close() } return hObject.ResponseText }
需要如下函数,请从往期文章查找:
json_fromobj(obj)
json_toobj(str)
Ansi2UTF8(sString)
UTF82Ansi(zString)
Ansi2Unicode(ByRef sString, ByRef wString, CP = 0)
Unicode2Ansi(ByRef wString, ByRef sString, CP = 0)