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"
- -- Wireless modem setup
- local modem = peripheral.wrap("left")
- assert(modem, "Wireless modem not connected.")
- modem.open(1)
- local cable_modem = peripheral.wrap("bottom")
- local link = peripheral.wrap("top")
- -- Function to get all connected speakers
- local function getSpeakers()
- local speakers = {}
- for _, name in ipairs(peripheral.getNames()) do
- if peripheral.getType(name) == "speaker" then
- table.insert(speakers, peripheral.wrap(name))
- end
- end
- return speakers
- end
- -- Function to play text-to-speech on all speakers
- 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()
- local speakers = getSpeakers()
- if #speakers == 0 then
- print("No speakers found!")
- return
- end
- while true do
- local chunk = response.read(16 * 1024)
- if not chunk then break end
- local buffer = decoder(chunk)
- for _, speaker in ipairs(speakers) do
- while not speaker.playAudio(buffer) do
- os.pullEvent("speaker_audio_empty")
- end
- end
- end
- response.close()
- end
- local function sanitizeText(text)
- -- Replace UTF-8 artifacts with proper characters
- text = text:gsub("é", "é"):gsub("è", "è"):gsub("ê", "ê"):gsub("ë", "ë")
- text = text:gsub("Ã ", "à"):gsub("Ã ", "â"):gsub("Ã ", "ä")
- local replacements = {
- ["é"] = "&", ["è"] = "e", ["ê"] = "ai", ["ë"] = "ai",
- ["à"] = "a", ["â"] = "a", ["ä"] = "a",
- ["ç"] = "c",
- ["ù"] = "u", ["û"] = "u", ["ü"] = "u",
- ["î"] = "i", ["ï"] = "i",
- ["ô"] = "o", ["ö"] = "o",
- ["â"] = "a", ["ã"] = "a",
- ["œ"] = "e", ["æ"] = "a",
- ["ÿ"] = "y",
- ["É"] = "er", ["È"] = "ai", ["Ê"] = "ai", ["Ë"] = "ai",
- ["À"] = "A", ["Â"] = "A", ["Ä"] = "A",
- ["Ç"] = "C",
- ["Ù"] = "U", ["Û"] = "U", ["Ü"] = "U",
- ["Î"] = "I", ["Ï"] = "I",
- ["Ô"] = "O", ["Ö"] = "O",
- ["Â"] = "A", ["Ã"] = "A",
- ["Œ"] = "e", ["Æ"] = "a",
- ["Ÿ"] = "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
- local function playSpeakerBlocks(response)
- link.clear()
- link.write(sanitizeText(response))
- link.update()
- rs.setOutput("right", true)
- os.sleep(1)
- rs.setOutput("right", false)
- end
- -- Function to display the last response
- 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
- -- Send message to clients
- modem.transmit(1, 1, responseBody.response)
- else
- modem.transmit(1, 1, "No last response available.")
- end
- end
- -- Main function
- local function main()
- -- Display and send the last response on startup
- displayLastResponse()
- print("Type a message to ask GLaDOS:")
- while true do
- write("> ")
- local userInput = read()
- if userInput and userInput ~= "" then
- print("Asking GLaDOS...")
- modem.transmit(1, 1, "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"))
- modem.transmit(1, 1, "Error contacting GLaDOS.")
- else
- local responseBody = textutils.unserializeJSON(response.readAll())
- response.close()
- if responseBody and responseBody.response then
- modem.transmit(1, 1, responseBody.response)
- playSpeakerBlocks(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