SHOW:
|
|
- or go back to the newest paste.
| 1 | -- | |
| 2 | -- CCleverBot | |
| 3 | -- Made by 1lann and GravityScore | |
| 4 | -- | |
| 5 | ||
| 6 | ||
| 7 | -- Variables | |
| 8 | ||
| 9 | local version = "1.1" | |
| 10 | ||
| 11 | local responseURL = "http://firewolfcc.com/ccleverbot/response.php" | |
| 12 | local event_updateChat = "ccleverbot_updateChatEvent" | |
| 13 | local event_continue = "ccleverbot_continueEvent" | |
| 14 | local event_exit = "ccleverbot_exitEvent" | |
| 15 | ||
| 16 | local chatHistory = {}
| |
| 17 | local chatLog = {}
| |
| 18 | local w, h = term.getSize() | |
| 19 | ||
| 20 | -- Drawing | |
| 21 | ||
| 22 | local function centerPrint(text, y) | |
| 23 | if type(text) == "table" then for _, v in pairs(text) do centerPrint(v) end | |
| 24 | else | |
| 25 | local x, y = term.getCursorPos() | |
| 26 | term.setCursorPos((w + 2)/2 - text:len()/2, ny or y) | |
| 27 | print(text) | |
| 28 | end | |
| 29 | end | |
| 30 | ||
| 31 | local function drawChat(chatData, offset, thinking, scrolled) | |
| 32 | local a, b = false, false | |
| 33 | for i = 1, 10 do | |
| 34 | term.setCursorPos(3, 16 - i) | |
| 35 | term.clearLine() | |
| 36 | end | |
| 37 | ||
| 38 | for i = 1, 10 do | |
| 39 | local c = chatData[#chatData + 1 - i + offset] | |
| 40 | ||
| 41 | if #chatData + 1 - i + offset < 1 then break end | |
| 42 | term.setCursorPos(3, 16 - i) | |
| 43 | ||
| 44 | if thinking and i == 1 then | |
| 45 | term.setTextColor(colors.lightGray) | |
| 46 | write("...")
| |
| 47 | offset = offset + 1 | |
| 48 | else | |
| 49 | if c[2] == "user" then | |
| 50 | term.setTextColor(colors.black) | |
| 51 | write(c[1]) | |
| 52 | else | |
| 53 | term.setTextColor(colors.lightBlue) | |
| 54 | if i == 1 and scrolled ~= true then | |
| 55 | a = true | |
| 56 | elseif i == 2 and scrolled ~= true then | |
| 57 | b = true | |
| 58 | else | |
| 59 | write(c[1]) | |
| 60 | end | |
| 61 | end | |
| 62 | end | |
| 63 | end | |
| 64 | ||
| 65 | if a then | |
| 66 | term.setTextColor(colors.lightBlue) | |
| 67 | if b then | |
| 68 | term.setCursorPos(3, 14) | |
| 69 | textutils.slowWrite(chatData[#chatData - 1][1]) | |
| 70 | end | |
| 71 | ||
| 72 | term.setCursorPos(3, 15) | |
| 73 | textutils.slowWrite(chatData[#chatData][1]) | |
| 74 | end | |
| 75 | ||
| 76 | os.queueEvent(event_continue) | |
| 77 | end | |
| 78 | ||
| 79 | ||
| 80 | -- Interface | |
| 81 | ||
| 82 | local function interface() | |
| 83 | local scrollPos = 0 | |
| 84 | local updateChatLog = nil | |
| 85 | while true do | |
| 86 | local e, p1, p2 = os.pullEvent() | |
| 87 | if e == event_updateChat then | |
| 88 | updateChatLog = textutils.unserialize(p1) | |
| 89 | scrollPos = 0 | |
| 90 | drawChat(updateChatLog, 0, p2) | |
| 91 | elseif e == event_exit then | |
| 92 | return | |
| 93 | elseif e == "mouse_scroll" then | |
| 94 | if scrollPos + p1 <= 0 and updateChatLog then | |
| 95 | if #updateChatLog > 10 and #updateChatLog >= (scrollPos+p1)*-1+10 then | |
| 96 | scrollPos = scrollPos+p1 | |
| 97 | local scrollChat = {}
| |
| 98 | for i = 10, 1, -1 do | |
| 99 | scrollChat[i] = updateChatLog[#updateChatLog-(10-i)+scrollPos] | |
| 100 | end | |
| 101 | local x, y = term.getCursorPos() | |
| 102 | drawChat(scrollChat, 0, nil, true) | |
| 103 | term.setCursorPos(x, y) | |
| 104 | term.setTextColor(colors.gray) | |
| 105 | end | |
| 106 | end | |
| 107 | end | |
| 108 | end | |
| 109 | end | |
| 110 | ||
| 111 | ||
| 112 | -- Input | |
| 113 | ||
| 114 | local function input() | |
| 115 | while true do | |
| 116 | -- Read | |
| 117 | term.setCursorPos(3, 17) | |
| 118 | term.setTextColor(colors.gray) | |
| 119 | term.clearLine() | |
| 120 | write("> ")
| |
| 121 | local inputData = read(nil, chatHistory):gsub("^%s*(.-)%s*$", "%1")
| |
| 122 | table.insert(chatHistory, inputData) | |
| 123 | if inputData == "/exit" then | |
| 124 | os.queueEvent(event_exit) | |
| 125 | return | |
| 126 | end | |
| 127 | ||
| 128 | -- Parse input | |
| 129 | if inputData == "" then inputData = "Hello." end | |
| 130 | inputData = inputData:sub(1, 1):upper() .. inputData:sub(2, -1) | |
| 131 | if not inputData:sub(-1, -1):find("[\.\?!,]") then inputData = inputData .. "." end
| |
| 132 | if inputData:len() > 45 and not inputData:sub(1, 45):find(" ") then
| |
| 133 | inputData = inputData:sub(1, 45) .. " " .. inputData:sub(46, -1) | |
| 134 | end | |
| 135 | ||
| 136 | -- Clear | |
| 137 | term.setCursorPos(3, 17) | |
| 138 | term.clearLine() | |
| 139 | write("> ")
| |
| 140 | ||
| 141 | if inputData:len() > 46 then | |
| 142 | local spaceData = {}
| |
| 143 | local loopNum = 0 | |
| 144 | while true do | |
| 145 | loopNum = inputData:find(" ", loopNum + 1, 1, true)
| |
| 146 | table.insert(spaceData, loopNum) | |
| 147 | if type(loopNum) ~= "number" then | |
| 148 | table.insert(spaceData, 47) | |
| 149 | break | |
| 150 | end | |
| 151 | end | |
| 152 | ||
| 153 | for k, v in ipairs(spaceData) do | |
| 154 | if v > 46 then | |
| 155 | chatLog[#chatLog + 1] = {inputData:sub(1, spaceData[k - 1] - 1), "user"}
| |
| 156 | chatLog[#chatLog + 1] = {inputData:sub(spaceData[k - 1] + 1, -1), "user"}
| |
| 157 | break | |
| 158 | end | |
| 159 | end | |
| 160 | else | |
| 161 | chatLog[#chatLog + 1] = {inputData, "user"}
| |
| 162 | end | |
| 163 | ||
| 164 | os.queueEvent(event_updateChat, textutils.serialize(chatLog), true) | |
| 165 | ||
| 166 | -- Get response | |
| 167 | local response = http.post(responseURL, "input=" .. textutils.urlEncode(inputData)) | |
| 168 | if response then | |
| 169 | local responseData = response.readAll() | |
| 170 | if responseData:len() > 46 then | |
| 171 | local spaceData = {}
| |
| 172 | local loopNum = 0 | |
| 173 | while true do | |
| 174 | loopNum = responseData:find(" ", loopNum + 1, 1, true)
| |
| 175 | table.insert(spaceData, loopNum) | |
| 176 | if type(loopNum) ~= "number" then | |
| 177 | table.insert(spaceData, 47) | |
| 178 | break | |
| 179 | end | |
| 180 | end | |
| 181 | ||
| 182 | for k, v in ipairs(spaceData) do | |
| 183 | if v > 46 then | |
| 184 | chatLog[#chatLog + 1] = {responseData:sub(1, spaceData[k - 1] - 1), "bot"}
| |
| 185 | chatLog[#chatLog + 1] = {responseData:sub(spaceData[k - 1]+1, -1), "bot"}
| |
| 186 | break | |
| 187 | end | |
| 188 | end | |
| 189 | else | |
| 190 | chatLog[#chatLog + 1] = {responseData, "bot"}
| |
| 191 | end | |
| 192 | else | |
| 193 | chatLog[#chatLog + 1] = {"CCBot: An error has ocurred!", "bot"}
| |
| 194 | end | |
| 195 | ||
| 196 | os.queueEvent(event_updateChat, textutils.serialize(chatLog)) | |
| 197 | os.pullEvent(event_continue) | |
| 198 | end | |
| 199 | end | |
| 200 | ||
| 201 | ||
| 202 | -- Main | |
| 203 | ||
| 204 | local function main() | |
| 205 | -- Top logo | |
| 206 | term.setBackgroundColor(colors.white) | |
| 207 | term.clear() | |
| 208 | term.setCursorPos(19, 2) | |
| 209 | ||
| 210 | term.setTextColor(colors.lightBlue) | |
| 211 | write("CClever")
| |
| 212 | term.setTextColor(colors.lime) | |
| 213 | write("B")
| |
| 214 | term.setTextColor(colors.yellow) | |
| 215 | write("o")
| |
| 216 | term.setTextColor(colors.red) | |
| 217 | write("t ")
| |
| 218 | term.setTextColor(colors.lightGray) | |
| 219 | write(version) | |
| 220 | term.setTextColor(colors.gray) | |
| 221 | term.setCursorPos(1, 3) | |
| 222 | centerPrint("- www.cleverbot.com -")
| |
| 223 | ||
| 224 | -- Run | |
| 225 | parallel.waitForAll(input, interface) | |
| 226 | end | |
| 227 | ||
| 228 | -- Check | |
| 229 | if not http then | |
| 230 | print("HTTP API Needs to be Enabled!")
| |
| 231 | print("CCleverBot heavily orties on it!")
| |
| 232 | error() | |
| 233 | end | |
| 234 | ||
| 235 | -- Run | |
| 236 | local _, err = pcall(main) | |
| 237 | if err then | |
| 238 | term.setTextColor(colors.white) | |
| 239 | term.setBackgroundColor(colors.black) | |
| 240 | term.clear() | |
| 241 | term.setCursorPos(1, 1) | |
| 242 | print("Error!")
| |
| 243 | print(err) | |
| 244 | print("\nPress any key to exit...")
| |
| 245 | os.pullEvent("key")
| |
| 246 | end | |
| 247 | ||
| 248 | -- Exit | |
| 249 | term.setTextColor(colors.white) | |
| 250 | term.setBackgroundColor(colors.black) | |
| 251 | term.clear() | |
| 252 | term.setCursorPos(1, 1) | |
| 253 | centerPrint("Thank You for Using CCleverBot " .. version)
| |
| 254 | centerPrint("Made by 1lann and GravityScore") |