How to make an AI chat bot for your game

Creating Roblox Basic Chatbot


Hey everyone, in this tutorial I am going to show you how to make your own Chatbot using google’s newly released bard.

Heres the preview of the end result.


We will be using repl.it as our backend cloud.

Creating the project

First head over to https://replit.com/ and create a account, once created

click on

Create Repl Button

Or directly click on this link, then this window will appear.

( We will be using python as our backend )

Once you decide the name and everything, click on create repl button.


Configuring backend

Now once our project is created, head over to your project terminal/shell.

There is no shell tab..

Tools > Shell

Now install the following packages

pip install bardapi
pip install flask

once they are installed, click on image button and name the file index.py

and then type the following code:

import os
from bardapi import BardCookies
from flask import Flask, request, jsonify

app = Flask(__name__)

cookies = {
    "__Secure-1PSID": os.environ['PSID'],
    "__Secure-1PSIDTS": os.environ['PSIDTS'],
    "__Secure-1PSIDCC": os.environ['PSIDCC']
}

print('starting..')

bard = BardCookies(cookie_dict=cookies)

@app.route('/', methods=['GET'])
def home():
  if "txt" in request.args:
    txt = request.args['txt']
    response = ""
    try:
      response = bard.get_answer(txt)['content']
    except:
      return jsonify({"status": "ERROR", "text": "Invalid cookies"})
    return jsonify({"status": "OK", "text": response})
  else:
    return jsonify({"status": "OK", "text": ""})


app.run(debug=False,port=3000,host="0.0.0.0")

Perfect! now lets head towards the next step.


Env and Cookies

Now head towards https://bard.google.com/ and login with your google account.
Then press F12 or Ctrl + Shift + I

Then click Application > Cookies > https://bard.google.com

From there we need these three cookies:

image
then copy their values

(NOTE: Dont share these cookies with anyone, these cookies contains sensitive information such as Session Token, Password, Account Info etc…)

Once you get their values put them in JSON likes so:

{
  "PSIDTS": "sidts-XXXXXXXXXXXXXX-XXXXXX",
  "PSID": "XXXXXXXXXX-XXXXXXXXXXX",
  "PSIDCC": "XXXXXX-XXXXXX"
}

Put __Secure-1PSIDTS cookie value in PSIDTS
Put __Secure-1PSID cookie value in PSID
Put __Secure-1PSIDCC cookie value in PSIDCC

then copy the json, once done head over to your replit project and click Secrets.
image

Then this tab will open

Click on Edit as JSON then paste the thing youve copied and click save.

image

Once you click save it will look something like this:

and thats it! youre back end configured, click the run button to start your project

image

The webview tab will appear as soon as the project start running.
It will look something like this:

copy this url image
(The url will be different on your side).


Implementing backend to our roblox game

First make sure http requests is enabled.

Game Settings > Security > Allow Http Request

Create a script in ServerScriptService, and type the following code:

local http = game:GetService("HttpService")
local remote = replicatedStorage:WaitForChild("RemoteFunction")
local chat = game:GetService("Chat")

local url = "YOUR_PROJECT_URL"
local query = "Hello, how are you!?"

local function filterText(plr:Player,txt:string)
	return chat:FilterStringForBroadcast(txt,plr)
end

local newUrl = `{url}?txt={query}`
local res = http:GetAsync(newUrl)
local decoded = http:JSONDecode(res)

if decoded.status == "OK" then
    local filtered = filterText(plr,decoded.text)
    if not filtered then return end
    print(filtered) -- I am doing good, what about you?
end

Run the game and boom! you got chatbot working.


Pros and Cons

Heres are some pros and cons

Pros

+ Uses google's data
+ Fast
+ Accurate
... etc

Cons

- Cookies will reset once in a while, so you have to do the setting cookies process again
- Long results which may get filtered if used in game.

(Keep in mind, brad is still in development so things might change in future).

If you think this post is helpful then you can donate me and test the npc chat bot here:

What tutorial you want next?
  • Using Chat GPT instead of Bard
  • Managing prompts (Making bot act as our assistant)

0 voters

Heres how making bot act as our assistant will look like:

Thanks!

27 Likes

Damn didn’t know that was possible

1 Like

Man this is so underrated! I’m gonna check it out asap!

1 Like

I think making the your version work faster is more important than switching to ChatGPT for no reason

1 Like

ChatGPT has more capabilites over Bard. So theres actually many reasons to switch!

Making things fast is not in my hand, its basically depends on the GPU usage that ai uses to generate answer.

The best reason to switch from bard to chatgpt is that you don’t have to put cookies every hour or so, as you can see that bard session cookies changes time to time so you have to update cookies on the server, which is annoying thing to do.

I like how you used replit to create it, though the cookie reset is actually roblox’s problem

also it’s funny how you have 69 subscribers

IMG_1487

though cool tutorial

1 Like

Not really, bard have session cookies which reset time to time for security purposes, in this tutorial we are basically making a BOT that logins in with our account details to access bard and get information…

Because they haven’t released their public api.

1 Like

There is an API for it. Its a beta only and you have to sign up for it but basically anyone gets accepted.

I am talking about Bard’s api, not PaLM.

Testing place has been updated to CHAT-GPT.
And also uses prompt engineering.

(Added facial expressions)

I was wondering when this would show up. Used oobabooga webui with a local 13b model with the API and openai flags to query my own computer.

Using it to auto generate descriptions of source code. Hadn’t gotten it to my liking yet prompt wise but it does work.

Can you uncopylock the example place?

Wow how does the NPC know what it’s wearing?

Facial expressions and npc identification are done by prompt engineering.
You can either get npc’s clothing by using script or you can manually put it in the prompt.

Example:

I want you to act like a friendly support chat bot, your appearance can be identified as a dude wearing a black and gold themed clothing with a tophat. (...so on)

For any newcomers who realize that Bard has since become Gemini:

Yes, this doesn’t work anymore, at least I don’t believe so.

But, if you’re tech savvy enough, you can do something similar with Gemini and this Python API: dsdanielpark/Gemini-API

I tried it myself and it works like a charm. Thank you for this tutorial! :​)

P.S. If you want it to remember previous chats, just save it somewhere and repeat the past messages every time you send a new one. I don’t believe Gemini has a defined character limit. I did this as well, and it was rather functional.

2 Likes