This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
hbar's profile picture

Cleverbot client in pure Lua (up to date)

Started by hbar, 12 March 2015 - 09:16 PM
hbar #1
Posted 12 March 2015 - 10:16 PM
About a year ago I wrote a client for the Cleverbot backend in pure Lua (the other existing client at the time used an intermediary server that later went down permanently). Link to the old reddit post.

Yesterday I happened to watch the latest modded Minecraft video from Etho and he spoke about plans to build a base with a ComputerCraft "AI" assistant. That prompted me to check if my program still worked. Well, it didn't. But luckily I was able to update it and get it working again. I did search the forums for any new clients and found one that also was written in pure Lua. However that seemed to be outdated and it didn't work, at least for me, so I figure my code is worth posting.

EDIT: It seems that the client got used in the series, cool :)/>

Screenshot.

For anyone thats interested

pastebin get Dv9x1ppc cleverbot

or direct link to the pastebin.

The file works either as a simple standalone client, or you can import it as a wrapper class to use in your own code. Below is an example of how to import and use the code:


os.loadAPI("cleverbot")
bot = cleverbot.Cleverbot.new()
msg = "Hello"
response = bot:send(msg)
print(response)

Do report any bugs you might encounter.
Edited on 27 March 2015 - 07:35 PM
Bomb Bloke #2
Posted 12 March 2015 - 11:54 PM
Y'know what? I'm gonna hook up a ChatBox to this thing and hide it in another player's base, rigging it to record the ensuing conversation. Could be fun.

Edit:

Here we go (without the spying code):

os.loadAPI("cleverbot")

local chatbox = peripheral.find("chatbox")
chatbox.setLabel("TheGhost")

local chatSession = {}

while true do
	local myEvent = {os.pullEvent("chat_message")}
	
	if not chatSession[myEvent[3]] then chatSession[myEvent[3]] = cleverbot.Cleverbot.new() end
		
	chatbox.tell(myEvent[3], chatSession[myEvent[3]]:send(myEvent[4]))
end

By the way, you might consider switching this (around line 605):

-- If any arguments were supplied, we start the standalone client.
if #args > 0 then

… to something like this:

-- If launched as a script, we start the standalone client.
if shell then

Edit 2:

Do these connections time out, or something? Should there not be a way to check if they're still valid, or to close them when they're no longer wanted?
Edited on 13 March 2015 - 01:45 AM
hbar #3
Posted 13 March 2015 - 10:59 AM
By the way, you might consider switching this (around line 605):

-- If any arguments were supplied, we start the standalone client.
if #args > 0 then

… to something like this:

-- If launched as a script, we start the standalone client.
if shell then

So that is how you should properly check if the file was loaded as an API or run from the shell. Good to know, thanks!

Edit 2:

Do these connections time out, or something? Should there not be a way to check if they're still valid, or to close them when they're no longer wanted?

As far as I know, the default http.get and http.post methods have really long timeout times (which is good for certain applications). I'm not sure on how to correctly implement shorter timeouts. One way would be to send the request with http.request, set a timer event to launch after a while, and then wait for either a http_success, http_failure or the timer event. But I'm not sure what would happen if I timeout one connection and then start another and suddenly the first one goes through and raises a http event… This might be an issue, or it might not. Ideas?

EDIT: or do you mean they timeout on the Cleverbot side? I'm not sure on how to handle that as I'm not that familiar with the protocol (I just basically ported the Python program to Lua). I'll have to test it more, for sure.
Edited on 13 March 2015 - 10:01 AM
biggest yikes #4
Posted 15 March 2015 - 06:26 PM
For anyone interested in the variables of the Cleverbot api, when you make a new bot with

os.loadAPI("cleverbot")
local bot = cleverbot.Cleverbot.new()
you have three different variables you can access.

local conv = bot.conversation
local vars = bot.vars
local response = bot.response
"conv" (bot.conversation) is a table with the whole conversation
"vars" (bot.vars) is a table with all the vars that are being used when you send a request, or essentially a sort of environment table
"response" (bot.response) is a string with response given when you send a request with bot:send (although you should use the return result of bot:send)
SpoilerThe following code will use bot.conversation to save a conversation to a file:

open = fs.open("_conv_", "w")
open.writeLine("--Begin Conversation--")
local num = 0
for k, v in pairs(bot.conversation) do
  num = num + 1
  if tonumber(num) % 2 == 0 then
	open.writeLine("Cleverbot: " .. v)
  else
	open.writeLine("You: " .. v)
  end
end
open.writeLine("--End Conversation--")
open.close()
open = nil
I made a small client that does exactly this, you can chat and type _conv_ to save a conversation ( http://pastebin.com/Px9N7si4 )
Edited on 15 March 2015 - 05:38 PM
hbar #5
Posted 17 March 2015 - 12:11 AM
For anyone interested in the variables of the Cleverbot api, when you make a new bot with

os.loadAPI("cleverbot")
local bot = cleverbot.Cleverbot.new()
you have three different variables you can access.

local conv = bot.conversation
local vars = bot.vars
local response = bot.response
"conv" (bot.conversation) is a table with the whole conversation
"vars" (bot.vars) is a table with all the vars that are being used when you send a request, or essentially a sort of environment table
"response" (bot.response) is a string with response given when you send a request with bot:send (although you should use the return result of bot:send)

Those are internal variables used by the bot class and not intended to be accessed by the user, but I can see why using the conversation-variable could be handy (no need to track the conversation yourself, if you want to save a history). The variables vars and response probably have no use outside the class, but I guess it doesn't hurt knowing about them.

Thanks for contributing the program!
biggest yikes #6
Posted 17 March 2015 - 12:35 AM
For anyone interested in the variables of the Cleverbot api, when you make a new bot with

os.loadAPI("cleverbot")
local bot = cleverbot.Cleverbot.new()
you have three different variables you can access.

local conv = bot.conversation
local vars = bot.vars
local response = bot.response
"conv" (bot.conversation) is a table with the whole conversation
"vars" (bot.vars) is a table with all the vars that are being used when you send a request, or essentially a sort of environment table
"response" (bot.response) is a string with response given when you send a request with bot:send (although you should use the return result of bot:send)

Those are internal variables used by the bot class and not intended to be accessed by the user, but I can see why using the conversation-variable could be handy (no need to track the conversation yourself, if you want to save a history). The variables vars and response probably have no use outside the class, but I guess it doesn't hurt knowing about them.

Thanks for contributing the program!
Out of curiosity, is there a way to change the emotion of the bot, like you can on cleverbot.com?
Also, I made a program that makes two cleverbots talk to each other.. Start it with something like "Yolo!" and you can get some weird conversations! ( http://pastebin.com/aeqPcd9h ) Why is this possible?
Edited on 16 March 2015 - 11:56 PM
hbar #7
Posted 17 March 2015 - 01:22 PM
Out of curiosity, is there a way to change the emotion of the bot, like you can on cleverbot.com?

Apparently changing the emotion on the site requires you to sign in? At least I couldn't find any controls on the default page and didn't want to make an account just for that. I'm assuming the emotion uses the "emotionaloutput" and "emotionalhistory" fields of the variables sent to the service, but I don't know how. If you have an account, you can use browser developer tools to see what kind of requests the website itself sends to the backend (in Chrome it's the Network tab of Developer Tools). I'm not sure if that requires you to send some authentication headers or data also, but if not, you could use the same emotion variables with this too. Maybe I'll make an account later and try to find out how it works.

Also, I made a program that makes two cleverbots talk to each other.. Start it with something like "Yolo!" and you can get some weird conversations! ( http://pastebin.com/aeqPcd9h ) Why is this possible?

I'm not sure if I understand the question…
biggest yikes #8
Posted 17 March 2015 - 07:13 PM
Out of curiosity, is there a way to change the emotion of the bot, like you can on cleverbot.com?

Apparently changing the emotion on the site requires you to sign in? At least I couldn't find any controls on the default page and didn't want to make an account just for that. I'm assuming the emotion uses the "emotionaloutput" and "emotionalhistory" fields of the variables sent to the service, but I don't know how. If you have an account, you can use browser developer tools to see what kind of requests the website itself sends to the backend (in Chrome it's the Network tab of Developer Tools). I'm not sure if that requires you to send some authentication headers or data also, but if not, you could use the same emotion variables with this too. Maybe I'll make an account later and try to find out how it works.
Okay, thanks!
And yes, on the site itself you need an account to use the tweaks.
EDIT: I'm not sure if so, but I think the bot.vars 'tweak1' 'tweak2' and 'tweak3' effect Cleverbot's emotion, I'm certainly seeing some wackyness when setting 'tweak1' to '100'.

Also, I made a program that makes two cleverbots talk to each other.. Start it with something like "Yolo!" and you can get some weird conversations! ( http://pastebin.com/aeqPcd9h ) Why is this possible?

I'm not sure if I understand the question…
It's not really important, I meant that making two cleverbots talk to each other is insane. :P/>
Edited on 17 March 2015 - 06:25 PM
nomad856 #9
Posted 04 June 2015 - 10:52 PM
this isn't working for me it used to but now all i get is "error communicating with cleverbot"
biggest yikes #10
Posted 05 June 2015 - 12:41 AM
Seems like the program never gets a response from the web service.
Even with the URL and headers the result is always nil
TheOddByte #11
Posted 05 June 2015 - 08:13 PM
Seems like the program never gets a response from the web service.
Even with the URL and headers the result is always nil
Well it isn't exactly weird, if you try to visit the link on line 454 you'll see that you get a 404 error message.
Scratch that, something else is wrong.
Edited on 05 June 2015 - 06:15 PM
biggest yikes #12
Posted 05 June 2015 - 09:14 PM
Seems like the program never gets a response from the web service.
Even with the URL and headers the result is always nil
Well it isn't exactly weird, if you try to visit the link on line 454 you'll see that you get a 404 error message.
Scratch that, something else is wrong.
it would still return something in that case, and it isn't a 404 on my browser, so you're right, that's not it
TheOddByte #13
Posted 06 June 2015 - 10:57 AM
Seems like the program never gets a response from the web service.
Even with the URL and headers the result is always nil
Well it isn't exactly weird, if you try to visit the link on line 454 you'll see that you get a 404 error message.
Scratch that, something else is wrong.
it would still return something in that case, and it isn't a 404 on my browser, so you're right, that's not it
Well it was weird, the first time I visited the link I got a 404 or 403 error, then I got something like "DENIED+3000_cbfull".
And after some googling it seems that the API is denying post requests: https://github.com/fojas/cleverbot-node/issues/6
nomad856 #14
Posted 06 June 2015 - 12:35 PM
Seems like the program never gets a response from the web service.
Even with the URL and headers the result is always nil
Well it isn't exactly weird, if you try to visit the link on line 454 you'll see that you get a 404 error message.
Scratch that, something else is wrong.
it would still return something in that case, and it isn't a 404 on my browser, so you're right, that's not it
Well it was weird, the first time I visited the link I got a 404 or 403 error, then I got something like "DENIED+3000_cbfull".
And after some googling it seems that the API is denying post requests: https://github.com/f...t-node/issues/6

That was opened in march 2014, it was working a few days back so something else must of happened.
TheOddByte #15
Posted 06 June 2015 - 12:37 PM
That was opened in march 2014, it was working a few days back so something else must of happened.
I know when it was posted, thought the problem still was the same, but after looking at when this was posted I can assume that this isn't the problem.
And I also didn't know that it was working a few days ago, maybe it will automatically work in a few days :P/>
nomad856 #16
Posted 06 June 2015 - 12:43 PM
I know when it was posted, thought the problem still was the same, but after looking at when this was posted I can assume that this isn't the problem.
And I also didn't know that it was working a few days ago, maybe it will automatically work in a few days :P/>

Lets hope so this was really great program especially with Peripherals++
hbar #17
Posted 08 June 2015 - 11:18 AM
Holy bananas, Batman! That was tricky to debug. I managed to get it working again, however.

It seems the cleverbot site had started to require a cookie to be sent with the request. I updated the original version to send the required cookie, but I'm not sure how long the cookie will stay the same. As I can't inspect the response headers within ComputerCraft (as far as I know, dan200 pls), I have no way of automatically updating the cookie if/when it changes. Suggestions to overcome this are welcome.
TheOddByte #18
Posted 08 June 2015 - 07:03 PM
Well it's working alright :P/>
biggest yikes #19
Posted 09 June 2015 - 11:32 PM
woo!
now I can witness bot1 and bot2 talking to each other!
Edited on 09 June 2015 - 09:33 PM
nomad856 #20
Posted 11 June 2015 - 02:24 PM
Holy bananas, Batman! That was tricky to debug. I managed to get it working again, however.

It seems the cleverbot site had started to require a cookie to be sent with the request. I updated the original version to send the required cookie, but I'm not sure how long the cookie will stay the same. As I can't inspect the response headers within ComputerCraft (as far as I know, dan200 pls), I have no way of automatically updating the cookie if/when it changes. Suggestions to overcome this are welcome.

thank you hbar for you time on keeping this amazing program going
LeDark Lua #21
Posted 21 June 2015 - 09:06 PM
Just perfect !!! :DDDD I had a blast

Edited on 21 June 2015 - 07:10 PM
LeDark Lua #22
Posted 22 June 2015 - 04:20 PM
Ok sorry for seperate post, but can I implement this to my OS? your app tag would be: love.hbar.Cleverbot.app
hbar #23
Posted 23 June 2015 - 11:36 PM
Ok sorry for seperate post, but can I implement this to my OS? your app tag would be: love.hbar.Cleverbot.app

You are welcome to put it into your OS :)/> Do note that at the moment I can't guaranteed 100% uptime as the Cleverbot team seems to be changing their serverside stuff periodically. In case stuff breaks, try to reload the program from pastebin in case I have updated it and report the issue to me if the problem persists.
LeDark Lua #24
Posted 25 June 2015 - 09:28 PM
Ok sorry for seperate post, but can I implement this to my OS? your app tag would be: love.hbar.Cleverbot.app

You are welcome to put it into your OS :)/> Do note that at the moment I can't guaranteed 100% uptime as the Cleverbot team seems to be changing their serverside stuff periodically. In case stuff breaks, try to reload the program from pastebin in case I have updated it and report the issue to me if the problem persists.
Sure thing, and thanks