Skip to content

Instantly share code, notes, and snippets.

@thoraxe
Last active May 9, 2021 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoraxe/313bd7adf9a12d085698a08131329402 to your computer and use it in GitHub Desktop.
Save thoraxe/313bd7adf9a12d085698a08131329402 to your computer and use it in GitHub Desktop.
func _ready():
# the bedroom is the first scene, so set up the bedroom assistant
var bedroom_assistant = MyAssistant.new()
add_child(bedroom_assistant)
bedroom_assistant.connect("session_obtained", self, "_on_session_obtained")
bedroom_assistant.get_session("idstring")
func _on_session_obtained():
# hide the loading button and unhide the start button
$HBoxContainer/CenterContainer/VBoxContainer/LoadingLabel.hide()
$HBoxContainer/CenterContainer/VBoxContainer/StartButton.show()
class_name MyAssistant
extends HTTPRequest
var session_id
signal session_obtained
func get_session(assistant_id):
connect("request_completed", self, "_session_request_completed")
# set up authentication
var auth=str("Basic ", Marshalls.utf8_to_base64(str("apikey", ":", Globals.WATSON_API_KEY)))
var headers=["Content-Type: application/json","Authorization: "+auth]
var session_format_url = Globals.ASSISTANT_BASEURL + "/%s/sessions?version=2020-04-01"
var session_url = session_format_url % assistant_id
var error = request(session_url, headers, true, HTTPClient.METHOD_POST, "{}")
if error != OK:
push_error("An error occurred in the HTTP request.")
func _session_request_completed(_result, _response_code, _headers, body):
var json = JSON.parse(body.get_string_from_utf8())
if Globals.DEBUG_BUILD:
print(json.result)
session_id = json.result["session_id"]
emit_signal("session_obtained", session_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment