Advertisement
Nekrose483

CCleverbot for CC

May 24th, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --  
  2. --  CCleverBot
  3. --  Made by 1lann and GravityScore
  4. --  
  5. --  Modified by PixelToast to use chat :3
  6. --
  7.  
  8. --  Variables
  9.  
  10. local cmdb=peripheral.wrap("left") -- command block ( NEEDS TO BE ENNABLED IN CONFIG! )
  11. local advp=peripheral.wrap("right") -- adventure map interface
  12.  
  13. local version = "1.1"
  14.  
  15. local responseURL = "http://firewolf.dyndns.org:8080/ccleverbot/response.php"
  16. local event_updateChat = "ccleverbot_updateChatEvent"
  17. local event_continue = "ccleverbot_continueEvent"
  18. local event_exit = "ccleverbot_exitEvent"
  19.  
  20. local chatHistory = {}
  21. local chatLog = {}
  22. local w, h = term.getSize()
  23.  
  24. -- snip (PixelToast)
  25.  
  26. --  Input
  27.  
  28. local function read()
  29.     while true do
  30.         local p={os.pullEvent()}
  31.         if p[1]=="chat_message" then
  32.             return p[3]
  33.         end
  34.     end
  35. end
  36.  
  37. local function input()
  38.     while true do
  39.         -- Read
  40.         local inputData = read(nil, chatHistory):gsub("^%s*(.-)%s*$", "%1")
  41.         print("Received "..inputData)
  42.         table.insert(chatHistory, inputData)
  43.         if inputData == "/exit" then
  44.             os.queueEvent(event_exit)
  45.             return
  46.         end
  47.  
  48.         -- Parse input
  49.         if inputData == "" then inputData = "Hello." end
  50.         inputData = inputData:sub(1, 1):upper() .. inputData:sub(2, -1)
  51.         if not inputData:sub(-1, -1):find("[\.\?!,]") then inputData = inputData .. "." end
  52.         if inputData:len() > 45 and not inputData:sub(1, 45):find(" ") then
  53.             inputData = inputData:sub(1, 45) .. " " .. inputData:sub(46, -1)
  54.         end
  55.  
  56.         if inputData:len() > 46 then
  57.             local spaceData = {}
  58.             local loopNum = 0
  59.             while true do
  60.                 loopNum = inputData:find(" ", loopNum + 1, 1, true)
  61.                 table.insert(spaceData, loopNum)
  62.                 if type(loopNum) ~= "number" then
  63.                     table.insert(spaceData, 47)
  64.                     break
  65.                 end
  66.             end
  67.  
  68.             for k, v in ipairs(spaceData) do
  69.                 if v > 46 then
  70.                     chatLog[#chatLog + 1] = {inputData:sub(1, spaceData[k - 1] - 1), "user"}
  71.                     chatLog[#chatLog + 1] = {inputData:sub(spaceData[k - 1] + 1, -1), "user"}
  72.                     break
  73.                 end
  74.             end
  75.         else
  76.             chatLog[#chatLog + 1] = {inputData, "user"}
  77.         end
  78.  
  79.         -- Get response
  80.         local response = http.post(responseURL, "input=" .. textutils.urlEncode(inputData))
  81.         if response then
  82.             local responseData = response.readAll()
  83.             if responseData:len() > 46 then
  84.                 local spaceData = {}
  85.                 local loopNum = 0
  86.                 while true do
  87.                     loopNum = responseData:find(" ", loopNum + 1, 1, true)
  88.                     table.insert(spaceData, loopNum)
  89.                     if type(loopNum) ~= "number" then
  90.                         table.insert(spaceData, 47)
  91.                         break
  92.                     end
  93.                 end
  94.  
  95.                 for k, v in ipairs(spaceData) do
  96.                     if v > 46 then
  97.                         chatLog[#chatLog + 1] = {responseData:sub(1, spaceData[k - 1] - 1), "bot"}
  98.                         chatLog[#chatLog + 1] = {responseData:sub(spaceData[k - 1]+1, -1), "bot"}
  99.                         break
  100.                     end
  101.                 end
  102.             else
  103.                 cmdb.setCommand("/say "..responseData)
  104.                 cmdb.runCommand()
  105.                 print("Sent "..responseData)
  106.             end
  107.         else
  108.             chatLog[#chatLog + 1] = {"CCBot: An error has ocurred!", "bot"}
  109.         end
  110.     end
  111. end
  112.  
  113.  
  114. --  Main
  115.  
  116. local function main()
  117.     -- snip (PixelToast)
  118.     parallel.waitForAll(input)
  119. end
  120.  
  121. -- Check
  122. if not http then
  123.     print("HTTP API Needs to be Enabled!")
  124.     print("CCleverBot heavily orties on it!")
  125.     error()
  126. end
  127.  
  128. -- Run
  129. local _, err = pcall(main)
  130. if err then
  131.     term.setTextColor(colors.white)
  132.     term.setBackgroundColor(colors.black)
  133.     term.clear()
  134.     term.setCursorPos(1, 1)
  135.     print("Error!")
  136.     print(err)
  137.     print("\nPress any key to exit...")
  138.     os.pullEvent("key")
  139. end
  140.  
  141. -- Exit
  142. term.setTextColor(colors.white)
  143. term.setBackgroundColor(colors.black)
  144. term.clear()
  145. term.setCursorPos(1, 1)
  146. print("Thank You for Using CCleverBot " .. version)
  147. print("Made by 1lann and GravityScore")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement