Jump to content
Sign in to follow this  
dragan

Cleverbot interaction

Recommended Posts

dragan

Too bad the Cleverbot is getting dumbe... less.. clever, but I guess that's just a result of talking to bunch of people who talk gibberish with the Cleverbot.

Anyway, here's the script that interacts with the Cleverbot:

 

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <IE.au3>
#include <GuiRichEdit.au3>
#include <WinAPI.au3>
#include <GuiMenu.au3>
#include <GuiEdit.au3>

Global Const $VK_RETURN = 0x0D
Global Const $GUIMINWID = 300
Global Const $GUIMINHT = 200

Global $oIE
Global $WaitReply = False
Global $LastMessage = ''
Global $CleverBotURL = 'http://www.cleverbot.com/'

Global $__ver = '0.0.2'
Global $__Title = 'Cleverbot chat v' & $__ver

Global $__Width = 500
Global $__Height = 400

Global $Form1 = GUICreate($__Title, $__Width, $__Height, -1, -1, BitOR($WS_MINIMIZEBOX, $WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_CAPTION, $WS_POPUPWINDOW, $WS_GROUP, $WS_BORDER, $WS_CLIPSIBLINGS))
_IEToggleImages(False)
$oIE = _IECreateEmbedded()
GUICtrlCreateObj($oIE, -1000, -1000, 10, 10)

Global $Input_Chat = GUICtrlCreateInput("", 0, $__Height-21, $__Width, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlSetResizing(-1, $GUI_DOCKLEFT+$GUI_DOCKRIGHT+$GUI_DOCKBOTTOM+$GUI_DOCKHEIGHT)
Global $Edit = _GUICtrlRichEdit_Create($Form1, '', 0, 0, $__Width, $__Height-21, BitOR($ES_AUTOVSCROLL, $ES_MULTILINE, $ES_READONLY, $WS_VSCROLL), 0)
GUISetState(@SW_SHOW)

OnAutoItExitRegister("_exit_")
GUIRegisterMsg($WM_SYSCOMMAND, "WM_SYSCOMMAND")
GUIRegisterMsg($WM_SIZE, "WM_SIZE")
GUIRegisterMsg($WM_GETMINMAXINFO, "WM_GETMINMAXINFO")
Global $wProcHandle = DllCallbackRegister("_EditWindowProc", "ptr", "hwnd;uint;wparam;lparam")
Global $wProc = _WinAPI_SetWindowLong(GUICtrlGetHandle($Input_Chat), $GWL_WNDPROC, DllCallbackGetPtr($wProcHandle))
AdlibRegister ( "_Wait4ReplyFromCleverBot", 1000)

_InitiateCleverBot()

While 1
    Sleep(100)
WEnd

Func _Wait4ReplyFromCleverBot()
    If $WaitReply = True then
        If IsObj($oIE) Then
            Local $oForm = _IEFormGetObjByName($oIE, 'stimulusForm')
            Local $AllTRs = _IETagNameGetCollection($oForm, 'tr')
            Local $LastTextObj = _IETagNameGetCollection($oForm, 'tr', @extended-5)
            Local $OutPutCleverBot = $LastTextObj.innertext
            If $OutPutCleverBot <> '|' AND $OutPutCleverBot <> '' then
                WinSetTitle($Form1, '', $__Title & '    [Cleverbot is typing...]')
                If $OutPutCleverBot <> $LastMessage Then
                    $LastMessage = $OutPutCleverBot
                Else
                    $WaitReply = False
                    _AppendText($LastMessage, 1)
                EndIf
            EndIf
        EndIf
    EndIf
EndFunc

Func _InitiateCleverBot()
    _AppendText('Please wait, initiating Cleverbot...', 2)
    _IENavigate($oIE, $CleverBotURL)
    Sleep(500)
    _IEToggleImages(True)
    _AppendText('Cleverbot initiated.', 3)
    _AppendText('You may chat with the Cleverbot now...', 3)
    _AppendText('', 3)
    GUICtrlSetState($Input_Chat, $GUI_ENABLE)
    GuiCtrlSetState($Input_Chat, $GUI_FOCUS)
EndFunc

Func _IEToggleImages($f_State = True)
    If $f_State Then
        $s_Status = "yes"
    Else
        $s_Status = "no"
    EndIf
    RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main", _
            "Display Inline Images", "REG_SZ", $s_Status)
EndFunc   ;==>_IEToggleImages

Func _AppendText($text, $type = 0)
    Switch $type
        Case 0;user
            _AddToRichEdit($Edit, '[Me]' & @TAB & $text, 11, 0xBA5C34)
            GUICtrlSetState($Input_Chat, $GUI_DISABLE)
        Case 1;bot
            _AddToRichEdit($Edit, '[Bot]' & @TAB & $text, 11, 0x0863BB)
            GUICtrlSetState($Input_Chat, $GUI_ENABLE)
            WinSetTitle($Form1, '', $__Title)
        Case 2;info blue
            _AddToRichEdit($Edit, $text, 8, 0x0000FF)
        Case 3;info green
            _AddToRichEdit($Edit, $text, 8, 0x137D1A)
        Case 4;error
            _AddToRichEdit($Edit, $text, 8, 0xFF0000)
    EndSwitch
    If WinGetHandle("[ACTIVE]") = $Form1 Then GuiCtrlSetState($Input_Chat, $GUI_FOCUS)
EndFunc

Func RGB2BGR($iColor)
    Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc   ;==>RGB2BGR

Func _AddToRichEdit($hWnd, $sText, $sizee = 8, $color = 0, $bold = False, $underline = False, $italic = False)
    If $sizee = Default then $sizee = 8
    If $color = Default then $color = 0
    If $bold = Default then $bold = False
    If $underline = Default then $underline = False
    If $italic = Default then $italic = False
    If $color <> 0 then $color = RGB2BGR($color)
    Local $iLength = _GUICtrlRichEdit_GetTextLength($hWnd, True, True)
    Local $iCp = _GUICtrlRichEdit_GetCharPosOfNextWord($hWnd, $iLength)
    _GUICtrlRichEdit_AppendText($hWnd, $sText & @CRLF)
    _GUICtrlRichEdit_SetSel($hWnd, $iCp-1, $iLength + StringLen($sText))
    If $bold = True then
        _GUICtrlRichEdit_SetCharAttributes($hWnd, "+bo")
    Else
        _GUICtrlRichEdit_SetCharAttributes($hWnd, "-bo")
    EndIf
    If $underline = True Then
        _GUICtrlRichEdit_SetCharAttributes($hWnd, "+un")
    Else
        _GUICtrlRichEdit_SetCharAttributes($hWnd, "-un")
    EndIf
    If $italic = True Then
        _GUICtrlRichEdit_SetCharAttributes($hWnd, "+it")
    Else
        _GUICtrlRichEdit_SetCharAttributes($hWnd, "-it")
    EndIf
    _GUICtrlRichEdit_SetCharColor($hWnd, $color)
    _GUICtrlRichEdit_SetFont($hWnd, $sizee, "Segoe UI")
    _GuiCtrlRichEdit_Deselect($hWnd)
EndFunc

Func _exit_()
    _IEToggleImages(True)
    AdlibUnRegister()
    _WinAPI_SetWindowLong(GUICtrlGetHandle($Input_Chat), $GWL_WNDPROC, $wProc)
    DllCallbackFree($wProcHandle)
    _GUICtrlRichEdit_Destroy($Edit)
EndFunc

Func _InputChat($text)
    If NOT IsObj($oIE) Then
        Return SetError(1, 0, 0)
    EndIf
    Local $InputID = _IEGetObjById($oIE, 'stimulus')
    _IEFormElementSetValue($InputID, $text)
    $oIE.document.parentWindow.execScript('lCBtn("Say");')
    _AppendText($text, 0)
    $WaitReply = True
EndFunc

Func _EditWindowProc($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    Switch $hWnd
        case GUICtrlGetHandle($Input_Chat)
            Switch $iMsg
                Case $WM_GETDLGCODE
                    Switch $wParam
                        Case $VK_RETURN
                            _InputChat(GUICtrlRead($Input_Chat))
                    EndSwitch
            EndSwitch
    EndSwitch
    Return _WinAPI_CallWindowProc($wProc, $hWnd, $iMsg, $wParam, $lParam)
EndFunc

Func WM_SYSCOMMAND($hWnd, $iMsg, $WParam, $LParam)
    Switch $hWnd
        Case $Form1
            Switch $WParam
                Case $SC_CLOSE
                    exit
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_SYSCOMMAND

Func WM_SIZE($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam, $ilParam
    Switch $hWnd
        Case $Form1
            Local $iWidth = BitAND($ilParam, 0x0000FFFF)
            Local $iHeight = BitShift($ilParam, 16)
            _WinAPI_MoveWindow($Edit, 0, 0, $iWidth, $iHeight-21)
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_SIZE

Func WM_GETMINMAXINFO($hWnd, $Msg, $WParam, $LParam)
    Switch $hWnd
        Case $Form1
            $tagMaxinfo = DllStructCreate("int;int;int;int;int;int;int;int;int;int", $LParam)
            DllStructSetData($tagMaxinfo, 7, $GUIMINWID) ; min X
            DllStructSetData($tagMaxinfo, 8, $GUIMINHT) ; min Y
    EndSwitch
    Return 0
EndFunc   ;==>WM_GETMINMAXINFO
Edited by dragan

Share this post


Link to post
Share on other sites
BinaryBrother
2013-08-27_1809.png
 
This thing has gotten pretty dumb, but "hiccups" intelligent conversations at random. Pretty cool script, you caused me to go check out the Cleverbot again. ^.^

Share this post


Link to post
Share on other sites
Biatu

I overloaded this thing once, found out im actually talking to random people.

afaik, when cleverbot runs out of responses, your questions and/or responses are the questions and/or responses to other users

Neat script btw

Edited by Biatu

What is what? What is what.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×