Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- CCleverBot
- -- Made by 1lann and GravityScore
- --
- -- Modified by PixelToast to use chat :3
- --
- -- Variables
- local cmdb=peripheral.wrap("left") -- command block ( NEEDS TO BE ENNABLED IN CONFIG! )
- local advp=peripheral.wrap("right") -- adventure map interface
- local version = "1.1"
- local responseURL = "http://firewolf.dyndns.org:8080/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()
- -- snip (PixelToast)
- -- Input
- local function read()
- while true do
- local p={os.pullEvent()}
- if p[1]=="chat_message" then
- return p[3]
- end
- end
- end
- local function input()
- while true do
- -- Read
- local inputData = read(nil, chatHistory):gsub("^%s*(.-)%s*$", "%1")
- print("Received "..inputData)
- 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
- 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
- -- 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
- cmdb.setCommand("/say "..responseData)
- cmdb.runCommand()
- print("Sent "..responseData)
- end
- else
- chatLog[#chatLog + 1] = {"CCBot: An error has ocurred!", "bot"}
- end
- end
- end
- -- Main
- local function main()
- -- snip (PixelToast)
- parallel.waitForAll(input)
- 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)
- print("Thank You for Using CCleverBot " .. version)
- print("Made by 1lann and GravityScore")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement