Getting a response from Cleverbot (Tutorial)

So I thought I would share how you could make your own Cleverbot on ROBLOX after it’s popularity from this post: http://devforum.roblox.com/t/cleverbot-powered-bot-on-roblox/24255

It’s actually fairly simple to do:

First, what you need to do is download an API. The one I used can be found here: https://github.com/pierredavidbelanger/chatter-bot-api

Create a directory on web server and extract the php file from the API into the directory. Next, what you want to do is create an file named “index.php” You should have something that looks like this:

Inside the file index.php, you would want to paste this code:

<?php
    require 'chatterbotapi.php';

    $factory = new ChatterBotFactory();

        $bot = $factory->create(ChatterBotType::CLEVERBOT);
        $botsession = $bot->createSession();

        $s = $_GET['message'];;
        $s = $botsession->think($s);
        echo $s;
?>

What this does is takes anything written after ?message= in the URL, send it to the API to process, then prints it to the screen.

Now, for the ROBLOX part. I’m not going to supply my exact code, but the function on how to retrieve the response:

bannedWords = {".com","download","app","clever"}

function CheckForBadWords(str)
	for _,words in pairs(bannedWords) do
		if string.match(string.lower(str), words) then
			return true
		end
	end
	return false
end

function GetResponse(player,str)
	repeat
		message = game.HttpService:GetAsync("YOUR URL?message="..str)
		message = game:GetService("Chat"):FilterStringForPlayerAsync(message,player)
	until CheckForBadWords(message) == false
	return message
end

To get a response, just call the function GetResponse(). Example of what I did:

game.Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(msg)
		message = GetResponse(player,msg)
		game.ReplicatedStorage.ReceivedChat:FireClient(player,message)
	end)
end)

It is important that this function is located in a script parented to ServiceScriptService! The response must be filtered in order to make sure it is age-appropriate for the user.

Also, CheckForBadWords() just filters any potential advertisements or URLs that might pop up. It started happening quite often for me, so I would recommend keeping it.

That’s about it. It’s very easy to use so I hope this has been useful. :smiley:

9 Likes

What you should do is start a new session for each and every player. That way someone else’s conversation isn’t leaking into another person’s.

You should URL encode the player’s chat before you send it to the server. Change this line:

to:

4 Likes