Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local side = "left"
- local publiccmdprefix = "#"
- local privatecmdprefix = "!"
- local chatbotprefix = "-"
- --local convoid = math.random(100000000,999999999)
- local convoid = 573194735 --Remember the conversation
- local chatbotindex = 13 --6/10/13 -> Original,Shakespeare,Comedy
- local command1alias = {"ping","Ping","ping!","Ping!"}
- local command1answers = {"Pong!","I'm really not in the mood today.","I'm bored...","1:0"}
- local command2alias = {"time","Time"}
- local x,y = term.getSize()
- local status = "Waiting..."
- local lastcmd = "-"
- local lastusr = "-"
- local answer = "-"
- local range = 128
- local connection = false
- local responsecode = "-"
- local latency = 0
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function drawLine(text)
- term.setTextColor(colors.white)
- if text == nil then
- for i=1,x do
- write("-")
- end
- else
- length = ((x-string.len(text))/2)
- for i=1,length do
- write("-")
- end
- write(text)
- for i=1,length do
- write("-")
- end
- end
- end
- function outputcolored(part1,part2,part1color,part2color)
- term.setTextColor(part1color)
- write(part1)
- term.setTextColor(part2color)
- write(part2)
- print("")
- end
- function testconnection(_url)
- http.request(_url)
- tmp1 = os.time()
- event,adress,handle = os.pullEvent()
- if event == "http_success" then
- tmp2 = os.time()
- latency = (tmp2-tmp1)*10000
- connection = true
- responsecode = handle.getResponseCode()
- handle:close()
- else
- latency = -1
- connection = false
- responsecode = "-"
- end
- end
- function display()
- clear()
- drawLine("Ralph - The Talking Bot v0.2.7b")
- outputcolored("Status: ",status,colors.white,colors.red)
- outputcolored("Last Command: ",lastcmd,colors.white,colors.red)
- outputcolored("Last User: ",lastusr,colors.white,colors.blue)
- outputcolored("Last Output: ",answer,colors.white,colors.red)
- drawLine()
- outputcolored("Output Range: ",range,colors.white,colors.red)
- if connection == true then
- outputcolored("Connection: ","Connected",colors.white,colors.lime)
- if latency == -1 then
- outputcolored("Latency: ","-",colors.white,colors.red)
- elseif latency > 0 and latency < 100 then
- outputcolored("Latency: ",latency.."ms",colors.white,colors.lime)
- elseif latency > 100 and latency < 200 then
- outputcolored("Latency: ",latency.."ms",colors.white,colors.orange)
- elseif latency > 200 then
- outputcolored("Latency: ",latency.."ms",colors.white,colors.red)
- end
- else
- outputcolored("Connection: ","Not Connected",colors.white,colors.red)
- end
- if responsecode == "200" then
- outputcolored("Response Code: ",responsecode,colors.white,colors.lime)
- else
- outputcolored("Response Code: ",responsecode,colors.white,colors.orange)
- end
- if not responsetext == nil then
- outputcolored("Response Text: ",responsetext,colors.white,colors.red)
- end
- drawLine()
- end
- function setup()
- clear()
- chatbox = peripheral.wrap(side)
- os.loadAPI("json")
- status = "Testing Connection..."
- display()
- testconnection("http://api.program-o.com/")
- status = "Waiting for input..."
- main()
- end
- function valueExists(tbl,value)
- for k,v in pairs(tbl) do
- if value == publiccmdprefix..v then
- return "public"
- elseif value == privatecmdprefix..v then
- return "private"
- end
- end
- return nil
- end
- function url_encode(str)
- if (str) then
- str = string.gsub (str, "\n", "\r\n")
- str = string.gsub (str, "([^%w %-%_%.%~])",
- function (c) return string.format ("%%%02X", string.byte(c)) end)
- str = string.gsub (str, " ", "+")
- end
- return str
- end
- function main()
- while true do
- status = "Waiting for input..."
- display()
- while redstone.getInput("right") do
- status = "Idle"
- display()
- sleep(10)
- end
- event,player,arg1,arg2 = os.pullEvent() --"chat(player,message)","chat_death(player,killer,cause)"
- if event == "chat" then
- status = "Processing input..."
- display()
- if valueExists(command1alias,arg1) == "public" then
- lastcmd = arg1.." - public"
- lastusr = player
- --answer = command1answers[math.random(#command1answers)]
- testconnection("http://api.program-o.com/")
- answer = tostring(latency)
- chatbox.say(answer,range)
- elseif valueExists(command1alias,arg1) == "private" then
- lastcmd = arg1.." - private"
- lastusr = player
- --answer = command1answers[math.random(#command1answers)]
- testconnection("http://api.program-o.com/")
- answer = tostring(latency)
- chatbox.tell(player,answer,range)
- elseif valueExists(command2alias,arg1) == "public" then
- lastcmd = arg1.." - public"
- lastusr = player
- answer = "It's "..textutils.formatTime(os.time(),true).."."
- chatbox.say(answer,range)
- elseif valueExists(command2alias,arg1) == "private" then
- lastcmd = arg1.." - private"
- lastusr = player
- answer = "It's "..textutils.formatTime(os.time(),true).."."
- chatbox.tell(player,answer,range)
- elseif string.sub(arg1,1,1) == chatbotprefix then
- status = "Chatting..."
- display()
- local usrtext = url_encode(string.sub(arg1,2,string.len(arg1)))
- local url = "http://api.program-o.com/v2.3.1/chatbot/?say="..usrtext.."&bot_id="..chatbotindex.."&convo_id="..convoid
- local output = http.get(url).readAll()
- local tmp = json.decode(output)
- lastcmd = arg1.." - chatbot"
- lastusr = player
- answer = tmp.botsay
- chatbox.say(tmp.botsay,range)
- end
- elseif event == "chat_death" then --Currently bugged
- if arg1 == nil and not arg2 == nil then
- answer = "Not so luck today, eh? Dying because of "..arg2.." is lame!"
- chatbox.say(answer,range)
- elseif not arg1 == nil then
- answer = "So you got beaten up by "..arg1.."? Better luck next time :P"
- chatbox.say(answer,range)
- end
- end
- end
- end
- setup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement