Hi all!
I've been struggling with the REST-API for days now. :-(
Basically my code works like this: I create an array with multiple
pairs of actions and data, like "say" and "text-to-say" or "play" and
"URL to play". The goal is to make Karotz perform each action one
after another without any use of wait-functions like sleep();.
From what I understood of the API-Docs and references on this group,
it should work like this:
1. Initiate a callback and pass an interactiveID. Start interactive
mode
2. On the next callback, call the API to perform an action (e.g. "say
something")
3. On the next callback, check if the last action was cancelled or
terminated. If yes, call API for the next action, if no - quit.
4. repeat 3. untill actions are performed by Karotz
5. Stop interactive mode.
My code should do this:
1st callback (triggered by karotz.com-schedule or nanoz):
- set up the array (works)
- GET interactive id and store it in SESSION-variable (works)
- start interactive mode (works sometimes)
- quit
2nd callback:
- retrieve interactive ID from VoosMsg (works)
- restore SESSION (works, session-id == interactiveID)
- check for VoosMsg->event->code==(TERMINATED || CANCELLED) (works)
- if last action isn't finished, exit and wait for next callback
- if actions are left in the array, pull the next action and send
request to API (works sometimes)
...
nth callback
- stop interactive mode.
My code worked once (and only once!), but I can't get it to work
again. There may be bugs left or the API is just very unreliable, I
don't know. The requests results in 502-Errors most of the time.
Here is my code (the part with the SESSION and the actions works fine
in a karotz-less testscript):
<?php
if($_GET["interactiveid"]) {
session_id($_GET['interactiveid']); // session-name = interactiveID
(so we can reload the data once the Karotz-servers call us again
session_start();
//first callback start interactive mode
$_SESSION['interactiveid'] = $_GET["interactiveid"]; // store
interactiveID
// Set up the action-array
$actions = array(
array("say"=>"tralala"),
array("play"=>urlencode(<somemp3>)),
array("say"=>"bah")
);
$actions = array_reverse($actions);
$_SESSION['actions'] = $actions;
$_SESSION['counter'] = count($actions)+1;
$karotz_apipath = "
http://api.karotz.com/api/karotz/";
$karotz_action_play = "multimedia?action=play&url=";
$karotz_action_say = "tts?action=speak&lang=DE&text=";
$karotz_interactive = "&interactiveid=" .
$_SESSION['interactiveid'];
$result= file_get_contents($karotz_apipath . $karotz_action_say .
"start" .$karotz_interactive); // say "Start" so the API might call
back
session_write_close();
exit(0);
}
else {
$data = file_get_contents("php://input");
//posted event
try {
$voosMsg =simplexml_load_string($data);
$interactiveId = (string) $voosMsg->interactiveId;
session_id($interactiveId); // pick up the session-
cookie
session_start();
$code = (string) $voosMsg->event->code;
if (isset($code) && isset($interactiveId)) {
if(($code == "TERMINATED") || ($code == "CANCELLED")) { // last
action finished?
$_SESSION['counter'] = $_SESSION['counter']-1;
$karotz_apipath = "
http://api.karotz.com/api/karotz/";
$karotz_action_play = "multimedia?action=play&url=";
$karotz_action_say = "tts?action=speak&lang=DE&text=";
$karotz_interactive = "&interactiveid=" .
$_SESSION['interactiveid'];
if ($_SESSION['counter'] != 0) { // any actions left?
$counter = 1;
foreach ($_SESSION['actions'] as $param) { // grab
the next action
if ($counter == $_SESSION['counter']) {
$action = array_keys($param);
$parameter = array_values($param);
//
generate the API-call
if ($action[0] =="say") {
$result= file_get_contents($karotz_apipath .
$karotz_action_say . urlencode($parameter[0] .$karotz_interactive));
}
elseif ($action[0] =="play") {
$result= file_get_contents($karotz_apipath .
$karotz_action_play . urlencode($parameter[0] .$karotz_interactive));
}
session_write_close();
exit(0);
} else {
$counter++;
}
}
} else { // no more actions? delete session and stop
Karotz
$_SESSION = array();
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000, $params["path"],
$params["domain"], $params["secure"], $params["httponly"]
);
}
session_destroy();
file_get_contents("
http://api.karotz.com/api/karotz/
interactivemode?action=stop&interactiveid=" . $interactiveId);
}
}
}
}
catch (Exception $e) {
echo $h . 'Exception : ' . $e->getMessage() . "\n";
}
I hope someone of you can help me to get this working. Implementing
this on a Nabaztag:tag took me 10(!) minutes - on the Karotz, it's a
nightmare.
Best wishes,
Marcus