Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- CCleverBot
- -- Made by 1lann and GravityScore
- --
- -- Variables
- local version = "1.1"
- local responseURL = "http://firewolfcc.com/ccleverbot/response.php"
- local event_updateChat = "ccleverbot_updateChatEvent"
- local event_continue = "ccleverbot_continueEvent"
- local event_exit = "ccleverbot_exitEvent"
- local chatHistory = {}
- local chatLog = {}
- local w, h = term.getSize()
- -- Drawing
- local function centerPrint(text, y)
- if type(text) == "table" then for _, v in pairs(text) do centerPrint(v) end
- else
- local x, y = term.getCursorPos()
- term.setCursorPos((w + 2)/2 - text:len()/2, ny or y)
- print(text)
- end
- end
- local function drawChat(chatData, offset, thinking, scrolled)
- local a, b = false, false
- for i = 1, 10 do
- term.setCursorPos(3, 16 - i)
- term.clearLine()
- end
- for i = 1, 10 do
- local c = chatData[#chatData + 1 - i + offset]
- if #chatData + 1 - i + offset < 1 then break end
- term.setCursorPos(3, 16 - i)
- if thinking and i == 1 then
- term.setTextColor(colors.lightGray)
- write("...")
- offset = offset + 1
- else
- if c[2] == "user" then
- term.setTextColor(colors.black)
- write(c[1])
- else
- term.setTextColor(colors.lightBlue)
- if i == 1 and scrolled ~= true then
- a = true
- elseif i == 2 and scrolled ~= true then
- b = true
- else
- write(c[1])
- end
- end
- end
- end
- if a then
- term.setTextColor(colors.lightBlue)
- if b then
- term.setCursorPos(3, 14)
- textutils.slowWrite(chatData[#chatData - 1][1])
- end
- term.setCursorPos(3, 15)
- textutils.slowWrite(chatData[#chatData][1])
- end
- os.queueEvent(event_continue)
- end
- -- Interface
- local function interface()
- local scrollPos = 0
- local updateChatLog = nil
- while true do
- local e, p1, p2 = os.pullEvent()
- if e == event_updateChat then
- updateChatLog = textutils.unserialize(p1)
- scrollPos = 0
- drawChat(updateChatLog, 0, p2)
- elseif e == event_exit then
- return
- elseif e == "mouse_scroll" then
- if scrollPos + p1 <= 0 and updateChatLog then
- if #updateChatLog > 10 and #updateChatLog >= (scrollPos+p1)*-1+10 then
- scrollPos = scrollPos+p1
- local scrollChat = {}
- for i = 10, 1, -1 do
- scrollChat[i] = updateChatLog[#updateChatLog-(10-i)+scrollPos]
- end
- local x, y = term.getCursorPos()
- drawChat(scrollChat, 0, nil, true)
- term.setCursorPos(x, y)
- term.setTextColor(colors.gray)
- end
- end
- end
- end
- end
- -- Input
- local function input()
- while true do
- -- Read
- term.setCursorPos(3, 17)
- term.setTextColor(colors.gray)
- term.clearLine()
- write("> ")
- local inputData = read(nil, chatHistory):gsub("^%s*(.-)%s*$", "%1")
- table.insert(chatHistory, inputData)
- if inputData == "/exit" then
- os.queueEvent(event_exit)
- return
- end
- -- Parse input
- if inputData == "" then inputData = "Hello." end
- inputData = inputData:sub(1, 1):upper() .. inputData:sub(2, -1)
- if not inputData:sub(-1, -1):find("[\.\?!,]") then inputData = inputData .. "." end
- if inputData:len() > 45 and not inputData:sub(1, 45):find(" ") then
- inputData = inputData:sub(1, 45) .. " " .. inputData:sub(46, -1)
- end
- -- Clear
- term.setCursorPos(3, 17)
- term.clearLine()
- write("> ")
- if inputData:len() > 46 then
- local spaceData = {}
- local loopNum = 0
- while true do
- loopNum = inputData:find(" ", loopNum + 1, 1, true)
- table.insert(spaceData, loopNum)
- if type(loopNum) ~= "number" then
- table.insert(spaceData, 47)
- break
- end
- end
- for k, v in ipairs(spaceData) do
- if v > 46 then
- chatLog[#chatLog + 1] = {inputData:sub(1, spaceData[k - 1] - 1), "user"}
- chatLog[#chatLog + 1] = {inputData:sub(spaceData[k - 1] + 1, -1), "user"}
- break
- end
- end
- else
- chatLog[#chatLog + 1] = {inputData, "user"}
- end
- os.queueEvent(event_updateChat, textutils.serialize(chatLog), true)
- -- Get response
- local response = http.post(responseURL, "input=" .. textutils.urlEncode(inputData))
- if response then
- local responseData = response.readAll()
- if responseData:len() > 46 then
- local spaceData = {}
- local loopNum = 0
- while true do
- loopNum = responseData:find(" ", loopNum + 1, 1, true)
- table.insert(spaceData, loopNum)
- if type(loopNum) ~= "number" then
- table.insert(spaceData, 47)
- break
- end
- end
- for k, v in ipairs(spaceData) do
- if v > 46 then
- chatLog[#chatLog + 1] = {responseData:sub(1, spaceData[k - 1] - 1), "bot"}
- chatLog[#chatLog + 1] = {responseData:sub(spaceData[k - 1]+1, -1), "bot"}
- break
- end
- end
- else
- chatLog[#chatLog + 1] = {responseData, "bot"}
- end
- else
- chatLog[#chatLog + 1] = {"CCBot: An error has ocurred!", "bot"}
- end
- os.queueEvent(event_updateChat, textutils.serialize(chatLog))
- os.pullEvent(event_continue)
- end
- end
- -- Main
- local function main()
- -- Top logo
- term.setBackgroundColor(colors.white)
- term.clear()
- term.setCursorPos(19, 2)
- term.setTextColor(colors.lightBlue)
- write("CClever")
- term.setTextColor(colors.lime)
- write("B")
- term.setTextColor(colors.yellow)
- write("o")
- term.setTextColor(colors.red)
- write("t ")
- term.setTextColor(colors.lightGray)
- write(version)
- term.setTextColor(colors.gray)
- term.setCursorPos(1, 3)
- centerPrint("- www.cleverbot.com -")
- -- Run
- parallel.waitForAll(input, interface)
- end
- -- Check
- if not http then
- print("HTTP API Needs to be Enabled!")
- print("CCleverBot heavily orties on it!")
- error()
- end
- -- Run
- local _, err = pcall(main)
- if err then
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- print("Error!")
- print(err)
- print("\nPress any key to exit...")
- os.pullEvent("key")
- end
- -- Exit
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- centerPrint("Thank You for Using CCleverBot " .. version)
- centerPrint("Made by 1lann and GravityScore")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement