How would I make a AI chat bot?

How would I make a ai chat bot that when you say something it will use an Api to search google for a response and then say it in chat, or is this not possible?

2 Likes

I suppose the first steps you’d have to make is, what AI chat solution would you make? You can search on Google for some solutions, but a lot of them cost money and if you’re anticipating a big player base, it will only cost more.

If you manage to find a free solution (retrieve an API key etc), you can use HttpService to call the API endpoints of your found AI chat solution.

I imagine how it would be for a typical AI chat API:

  1. You would send a request to the API (using :GetAsync() or :PostAsync(), depends if the endpoint is a GET or POST) and send off an encoded JSON with the text the player said in the chat (store this request in a variable).

  2. The request should succeed and a response is returned back, most likely containing the text the AI chat bot responded back with. You’ll have to use :JSONDecode to decode the JSON into a table that Roblox recognises so you’re able to access the properties (in this case, the text).

  3. This is where you’d output the text into the chat, whether you’re using Roblox’s core chat system to do this, or you have a custom chat system implemented. Before you output anything to the user though, you should filter the text the API comes back with since Roblox won’t have filtering implemented into this by default. The only option I think you can use is FilterStringAsync and GetChatForUserAsync) methods. We don’t have a sender as much, since this is determined by the AI chat bot, but we can assume the sender was the user themselves. The return of this will return the filtered message (a string) which you can display to the user. An example of using these methods this is shown below:

--[[ Ensure this is done in a server script, all the API calls to the AI chat bot
and the text filtering will need to be done on the server. This means if you
have a custom chat system, you'll need to fire remote events to the client. ]]

--[[ You would call this function when the player sends a message, chances 
are this will be a remote event if handled on the client. 
sentFromUser would be the user sending the message to
the AI chat bot in this case. ]]

local TextService = game:GetService("TextService")

function receivedMessage(message, sentFromUser)
	if message ~= "" then
	
		-- We'll create our TextFilterResult object to use in :GetChatForUserAsync method.
		local messageObject
		
		-- Surround in a pcall() to ensure we can account for any errors and catch them.
		local success, errorMsg = pcall(function()
			messageObject = TextService:FilterStringAsync(message, sentFromUser)
		end)
		
		--[[ If the above pcall succeeds, that means nothing went wrong with the code 
		inside the pcall. So now we can get to filtering the text! ]]
		if success then
			-- We'll then use our messageObject to get the filtered text and then return it.
			return messageObject:GetChatForUserAsync(sentFromUser)
		end
		
		--[[ If the code gets here, the message wasn't filtered correctly,
		so in this case, we'll return blank/nothing. ]]
		return "";
	end
end

I hope this helped or at least given you a basic design of how you’d implement this. If you have any questions, just let me know. Best of luck with your project. :slight_smile:

2 Likes