Character Conversation API
Character Conversation API - Code Example for Roblox integration with Convai.
function my_functions.callConvai(msg, sessionID, apiKey, charID)
local httpSrv = game:GetService("HttpService")
local baseUrl = "https://api.convai.com/character/getResponse"
local gpt_response
local function request()
-- We restrict from making unnecessary calls if the following values are not set properly
if apiKey == "" then
print("Please provide a valid API Key")
return
end
if charID == "" then
print("Please enter a valid Character ID")
return
end
-- REST API Cal
local response = httpSrv:RequestAsync({
Url = baseUrl,
Method = "POST",
Headers = {
-- This is the most convenient way to send form-data in Roblox
["Content-Type"] = "application/x-www-form-urlencoded",
["CONVAI-API-KEY"] = apiKey
},
Body = string.format([[&userText=%s&sessionID=%s&charID=%s&voiceResponse=%s]], msg, sessionID, charID, 'False')
})
-- Inspect the response table
if response.Success then
print("Status code:", response.StatusCode, response.StatusMessage)
print("Response body:\n", response.Body)
local data = httpSrv:JSONDecode(response.Body)
print("Response Text: ", data.text)
print("Response Session ID: ", data.sessionID)
gpt_response = response.Body --data.text
else
print("The request failed:", response.StatusCode, response.StatusMessage)
end
end
-- Wrap the function in a 'pcall' to prevent the script from breaking if the request fails
local success, message = pcall(request)
--print("Status code: ", message)
if gpt_response == nil then
gpt_response = httpSrv:JSONEncode({sessionID = sessionID, text = "Sorry, I can't chat right now."})
end
return gpt_response
end
Last updated
Was this helpful?