Advertisement
m3Zz

Chatbot

Mar 9th, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.72 KB | None | 0 0
  1. local side = "left"
  2. local publiccmdprefix = "#"
  3. local privatecmdprefix = "!"
  4. local chatbotprefix = "-"
  5. --local convoid = math.random(100000000,999999999)
  6. local convoid = 573194735 --Remember the conversation
  7. local chatbotindex = 13 --6/10/13 -> Original,Shakespeare,Comedy
  8. local command1alias = {"ping","Ping","ping!","Ping!"}
  9. local command1answers = {"Pong!","I'm really not in the mood today.","I'm bored...","1:0"}
  10. local command2alias = {"time","Time"}
  11. local x,y = term.getSize()
  12. local status = "Waiting..."
  13. local lastcmd = "-"
  14. local lastusr = "-"
  15. local answer = "-"
  16. local range = 128
  17. local connection = false
  18. local responsecode = "-"
  19. local latency = 0
  20.  
  21. function clear()
  22.  term.clear()
  23.  term.setCursorPos(1,1)
  24. end
  25.  
  26. function drawLine(text)
  27.  term.setTextColor(colors.white)
  28.  if text == nil then
  29.   for i=1,x do
  30.    write("-")
  31.   end
  32.  else
  33.   length = ((x-string.len(text))/2)
  34.   for i=1,length do
  35.    write("-")
  36.   end
  37.   write(text)
  38.   for i=1,length do
  39.    write("-")
  40.   end
  41.  end
  42. end
  43.  
  44. function outputcolored(part1,part2,part1color,part2color)
  45.  term.setTextColor(part1color)
  46.  write(part1)
  47.  term.setTextColor(part2color)
  48.  write(part2)
  49.  print("")
  50. end
  51.  
  52. function testconnection(_url)
  53.  http.request(_url)
  54.  tmp1 = os.time()
  55.  event,adress,handle = os.pullEvent()
  56.  if event == "http_success" then
  57.   tmp2 = os.time()
  58.   latency = (tmp2-tmp1)*10000
  59.   connection = true
  60.   responsecode = handle.getResponseCode()
  61.   handle:close()
  62.  else
  63.   latency = -1
  64.   connection = false
  65.   responsecode = "-"
  66.  end
  67. end
  68.  
  69. function display()
  70.  clear()
  71.  drawLine("Ralph - The Talking Bot v0.2.7b")
  72.  outputcolored("Status: ",status,colors.white,colors.red)
  73.  outputcolored("Last Command: ",lastcmd,colors.white,colors.red)
  74.  outputcolored("Last User: ",lastusr,colors.white,colors.blue)
  75.  outputcolored("Last Output: ",answer,colors.white,colors.red)
  76.  drawLine()
  77.  outputcolored("Output Range: ",range,colors.white,colors.red)
  78.  if connection == true then
  79.   outputcolored("Connection: ","Connected",colors.white,colors.lime)
  80.   if latency == -1 then
  81.    outputcolored("Latency: ","-",colors.white,colors.red)
  82.   elseif latency > 0 and latency < 100 then
  83.    outputcolored("Latency: ",latency.."ms",colors.white,colors.lime)
  84.   elseif latency > 100 and latency < 200 then
  85.    outputcolored("Latency: ",latency.."ms",colors.white,colors.orange)
  86.   elseif latency > 200 then
  87.    outputcolored("Latency: ",latency.."ms",colors.white,colors.red)
  88.   end
  89.  else
  90.   outputcolored("Connection: ","Not Connected",colors.white,colors.red)
  91.  end
  92.  if responsecode == "200" then
  93.   outputcolored("Response Code: ",responsecode,colors.white,colors.lime)
  94.  else
  95.   outputcolored("Response Code: ",responsecode,colors.white,colors.orange)
  96.  end
  97.  if not responsetext == nil then
  98.   outputcolored("Response Text: ",responsetext,colors.white,colors.red)
  99.  end
  100.  drawLine()
  101. end
  102.  
  103. function setup()
  104.  clear()
  105.  chatbox = peripheral.wrap(side)
  106.  os.loadAPI("json")
  107.  status = "Testing Connection..."
  108.  display()
  109.  testconnection("http://api.program-o.com/")
  110.  status = "Waiting for input..."
  111.  main()
  112. end
  113.  
  114. function valueExists(tbl,value)
  115.  for k,v in pairs(tbl) do
  116.   if value == publiccmdprefix..v then
  117.    return "public"
  118.   elseif value == privatecmdprefix..v then
  119.    return "private"
  120.   end
  121.  end
  122.  return nil
  123. end
  124.  
  125. function url_encode(str)
  126.   if (str) then
  127.     str = string.gsub (str, "\n", "\r\n")
  128.     str = string.gsub (str, "([^%w %-%_%.%~])",
  129.         function (c) return string.format ("%%%02X", string.byte(c)) end)
  130.     str = string.gsub (str, " ", "+")
  131.   end
  132.   return str   
  133. end
  134.  
  135. function main()
  136.  while true do
  137.   status = "Waiting for input..."
  138.   display()
  139.   while redstone.getInput("right") do
  140.    status = "Idle"
  141.    display()
  142.    sleep(10)
  143.   end
  144.   event,player,arg1,arg2 = os.pullEvent() --"chat(player,message)","chat_death(player,killer,cause)"
  145.   if event == "chat" then
  146.    status = "Processing input..."
  147.    display()
  148.    if valueExists(command1alias,arg1) == "public" then
  149.     lastcmd = arg1.." - public"
  150.     lastusr = player
  151.     --answer = command1answers[math.random(#command1answers)]
  152.     testconnection("http://api.program-o.com/")
  153.     answer = tostring(latency)
  154.     chatbox.say(answer,range)
  155.    elseif valueExists(command1alias,arg1) == "private" then
  156.     lastcmd = arg1.." - private"
  157.     lastusr = player
  158.     --answer = command1answers[math.random(#command1answers)]
  159.     testconnection("http://api.program-o.com/")
  160.     answer = tostring(latency)
  161.     chatbox.tell(player,answer,range)
  162.    elseif valueExists(command2alias,arg1) == "public" then
  163.     lastcmd = arg1.." - public"
  164.     lastusr = player
  165.     answer = "It's "..textutils.formatTime(os.time(),true).."."
  166.     chatbox.say(answer,range)
  167.    elseif valueExists(command2alias,arg1) == "private" then
  168.     lastcmd = arg1.." - private"
  169.     lastusr = player
  170.     answer = "It's "..textutils.formatTime(os.time(),true).."."
  171.     chatbox.tell(player,answer,range)
  172.    elseif string.sub(arg1,1,1) == chatbotprefix then
  173.     status = "Chatting..."
  174.     display()
  175.     local usrtext = url_encode(string.sub(arg1,2,string.len(arg1)))
  176.     local url = "http://api.program-o.com/v2.3.1/chatbot/?say="..usrtext.."&bot_id="..chatbotindex.."&convo_id="..convoid
  177.     local output = http.get(url).readAll()
  178.     local tmp = json.decode(output)
  179.     lastcmd = arg1.." - chatbot"
  180.     lastusr = player
  181.     answer = tmp.botsay
  182.     chatbox.say(tmp.botsay,range)
  183.    end
  184.   elseif event == "chat_death" then --Currently bugged
  185.    if arg1 == nil and not arg2 == nil then
  186.     answer = "Not so luck today, eh? Dying because of "..arg2.." is lame!"
  187.     chatbox.say(answer,range)
  188.    elseif not arg1 == nil then
  189.     answer = "So you got beaten up by "..arg1.."? Better luck next time :P"
  190.     chatbox.say(answer,range)
  191.    end
  192.   end
  193.  end
  194. end
  195.  
  196. setup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement