Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configuration
- local TEXT_SCALE = 2 -- Text size (0.5, 1, 2, ...)
- local TEXT_COLOR = colors.orange -- Text color (use colors.<color>)
- -- HTTP API setup
- assert(http, "HTTP API is not enabled. Check your ComputerCraft config.")
- local API_URL = "https://lordfinn.fr/api/ask-glados"
- -- Setup monitors and speaker
- local monitors = {}
- local speaker = peripheral.wrap("bottom")
- assert(speaker, "No speaker connected on the bottom side!")
- for _, name in ipairs(peripheral.getNames()) do
- if peripheral.getType(name) == "monitor" and peripheral.isPresent(name) then
- local monitor = peripheral.wrap(name)
- monitor.setTextScale(TEXT_SCALE)
- table.insert(monitors, monitor)
- end
- end
- local function splitIntoLines(text, maxWidth)
- local lines = {}
- while #text > maxWidth do
- local splitPoint = maxWidth
- for i = maxWidth, 1, -1 do
- if text:sub(i, i):match("%s") then
- splitPoint = i
- break
- end
- end
- local line = text:sub(1, splitPoint):gsub("^%s+", ""):gsub("%s+$", "")
- table.insert(lines, line)
- text = text:sub(splitPoint + 1):gsub("^%s+", "")
- end
- if #text > 0 then
- table.insert(lines, text)
- end
- return lines
- end
- local function sanitizeText(text)
- -- Replace UTF-8 artifacts with proper characters
- text = text:gsub("é", "é")
- text = text:gsub("è", "è")
- text = text:gsub("ê", "ê")
- text = text:gsub("ë", "ë")
- text = text:gsub("Ã ", "à")
- text = text:gsub("Ã ", "â")
- text = text:gsub("Ã ", "ä")
- local replacements = {
- ["é"] = "e", ["è"] = "e", ["ê"] = "e", ["ë"] = "e",
- ["à"] = "a", ["â"] = "a", ["ä"] = "a",
- ["ç"] = "c",
- ["ù"] = "u", ["û"] = "u", ["ü"] = "u",
- ["î"] = "i", ["ï"] = "i",
- ["ô"] = "o", ["ö"] = "o",
- ["â"] = "a", ["ã"] = "a",
- ["œ"] = "oe", ["æ"] = "ae",
- ["ÿ"] = "y",
- ["É"] = "E", ["È"] = "E", ["Ê"] = "E", ["Ë"] = "E",
- ["À"] = "A", ["Â"] = "A", ["Ä"] = "A",
- ["Ç"] = "C",
- ["Ù"] = "U", ["Û"] = "U", ["Ü"] = "U",
- ["Î"] = "I", ["Ï"] = "I",
- ["Ô"] = "O", ["Ö"] = "O",
- ["Â"] = "A", ["Ã"] = "A",
- ["Œ"] = "OE", ["Æ"] = "AE",
- ["Ÿ"] = "Y"
- }
- -- Replace each character in the text based on the replacements table
- for original, replacement in pairs(replacements) do
- text = text:gsub(original, replacement)
- end
- return text
- end
- -- Display message centered on monitors
- local function displayMessageCentered(message)
- message = sanitizeText(message)
- for _, monitor in ipairs(monitors) do
- monitor.clear()
- monitor.setTextColor(TEXT_COLOR)
- local width, height = monitor.getSize()
- local lines = splitIntoLines(message, width)
- local yStart = math.floor((height - #lines) / 2) + 1
- for i, line in ipairs(lines) do
- local xStart = math.floor((width - #line) / 2) + 1
- monitor.setCursorPos(xStart, yStart + i - 1)
- monitor.write(line)
- end
- end
- end
- local function playTextToSpeech()
- local url = API_URL .. "/last-audio"
- local response, err = http.get({ url = url, binary = true })
- if not response then
- print("Error fetching TTS audio: " .. err)
- return
- end
- local decoder = require("cc.audio.dfpwm").make_decoder()
- while true do
- local chunk = response.read(16 * 1024)
- if not chunk then break end
- local buffer = decoder(chunk)
- while not speaker.playAudio(buffer) do
- os.pullEvent("speaker_audio_empty")
- end
- end
- response.close()
- end
- -- Fetch and display the last response on startup
- local function displayLastResponse()
- local url = API_URL .. "/last"
- local response, err = http.get(url)
- if not response then
- print("Error fetching last response: " .. err)
- return
- end
- local responseBody = textutils.unserializeJSON(response.readAll())
- response.close()
- if responseBody and responseBody.response then
- displayMessageCentered(responseBody.response)
- else
- displayMessageCentered("No last response available.")
- end
- end
- -- Main program loop
- local function main()
- -- Call the /last route on startup and display the response if it exists
- displayLastResponse()
- print("Type a message to ask GLaDOS:")
- while true do
- write("> ")
- local userInput = read()
- if userInput and userInput ~= "" then
- print("Asking GLaDOS...")
- displayMessageCentered("Thinking...")
- local response, err = http.post(API_URL, textutils.serializeJSON({ question = userInput }), { ["Content-Type"] = "application/json" })
- if not response then
- print("Error: " .. (err or "Unknown error"))
- displayMessageCentered("Error contacting GLaDOS.")
- else
- local responseBody = textutils.unserializeJSON(response.readAll())
- response.close()
- if responseBody and responseBody.response then
- displayMessageCentered(responseBody.response)
- playTextToSpeech()
- end
- end
- else
- print("Please enter a valid question.")
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement