图灵机器人文字聊天Demo Autohotkey

本文来自于Thinkai’s Blog,thinkai也是我在ahk上面的引路人,对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
}
图灵机器人文字聊天Demo Autohotkey

需要如下函数,请从往期文章查找:

json_fromobj(obj)
json_toobj(str)
Ansi2UTF8(sString)
UTF82Ansi(zString)
Ansi2Unicode(ByRef sString, ByRef wString, CP = 0)
Unicode2Ansi(ByRef wString, ByRef sString, CP = 0)
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA捐赠
共{{data.count}}人
人已捐赠
其他教程

生成随机时间 日期加减 Autohotkey

2020-2-18 9:35:36

其他教程

自动修改本地连接 网卡IP地址,INI保存配置 AutoHotkey

2020-2-18 9:38:36

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
有新私信 私信列表
搜索