corrade-lsl-templates

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 15  →  ?path2? @ 24
/source/eggdrop/ai.lsl
@@ -179,6 +179,8 @@
string URL = "";
// store message over state.
string data = "";
string jump_table = "";
string messageHash = "";
 
default {
state_entry() {
@@ -189,7 +191,10 @@
if(id != "configuration") return;
llOwnerSay("[AI] Got configuration...");
configuration = message;
state listen_group;
// Subscribe to MQTT messages.
jump_table = "subscribe";
state url;
}
timer() {
llOwnerSay("[AI] Requesting configuration...");
@@ -210,6 +215,135 @@
}
}
 
state url {
state_entry() {
// DEBUG
llOwnerSay("[AI] Requesting URL...");
llRequestURL();
}
http_request(key id, string method, string body) {
if(method != URL_REQUEST_GRANTED) return;
URL = body;
// DEBUG
llOwnerSay("[AI] Got URL...");
if(jump_table == "subscribe")
state subscribe;
if(jump_table == "publish")
state publish;
}
on_rez(integer num) {
llResetScript();
}
changed(integer change) {
if((change & CHANGED_INVENTORY) ||
(change & CHANGED_REGION_START) ||
(change & CHANGED_OWNER)) {
llResetScript();
}
}
}
 
state subscribe {
state_entry() {
// DEBUG
llOwnerSay("[AI] Subscribing to Corrade AI...");
llInstantMessage(
wasKeyValueGet(
"corrade",
configuration
),
wasKeyValueEncode(
[
"command", "MQTT",
"group", wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"password", wasURLEscape(
wasKeyValueGet(
"password",
configuration
)
),
// Subscribe to Corrade AI
"action", "subscribe",
"id", wasURLEscape(
wasKeyValueGet(
"ai subscription",
configuration
)
),
// Corrade AI listening host.
"host", wasURLEscape(
wasKeyValueGet(
"ai host",
configuration
)
),
// Corrade AI listening port.
"port", wasURLEscape(
wasKeyValueGet(
"ai port",
configuration
)
),
// Corrade AI credentials.
"username", wasURLEscape(
wasKeyValueGet(
"ai username",
configuration
)
),
"secret", wasURLEscape(
wasKeyValueGet(
"ai secret",
configuration
)
),
// Use the SIML module of Corrade AI.
"topic", "SIML",
// Send the result of the MQTT command to this URL.
"callback", wasURLEscape(URL)
]
)
);
}
http_request(key id, string method, string body) {
llHTTPResponse(id, 200, "OK");
llReleaseURL(URL);
if(wasKeyValueGet("command", body) != "MQTT" ||
wasKeyValueGet("success", body) != "True") {
// DEBUG
llOwnerSay("[AI] Unable to subscribe to MQTT topic: " +
wasURLUnescape(
wasKeyValueGet("error", body)
)
);
llResetScript();
}
state listen_group;
}
on_rez(integer num) {
llResetScript();
}
changed(integer change) {
if((change & CHANGED_INVENTORY) ||
(change & CHANGED_REGION_START) ||
(change & CHANGED_OWNER)) {
llResetScript();
}
}
state_exit() {
llSetTimerEvent(0);
}
}
 
state listen_group {
state_entry() {
// DEBUG
@@ -220,7 +354,7 @@
if(id != "notification")
return;
// This script only processes group notifications.
// Listen to group message notifications.
if(wasKeyValueGet("type", message) != "group")
return;
@@ -251,6 +385,7 @@
data = llDumpList2String(command, " ");
 
// Get an URL.
jump_table = "publish";
state url;
}
on_rez(integer num) {
@@ -265,35 +400,13 @@
}
}
 
state url {
state publish {
state_entry() {
// DEBUG
llOwnerSay("[AI] Requesting URL...");
llRequestURL();
}
http_request(key id, string method, string body) {
if(method != URL_REQUEST_GRANTED) return;
URL = body;
// DEBUG
llOwnerSay("[AI] Got URL...");
state version;
}
on_rez(integer num) {
llResetScript();
}
changed(integer change) {
if((change & CHANGED_INVENTORY) ||
(change & CHANGED_REGION_START) ||
(change & CHANGED_OWNER)) {
llResetScript();
}
}
}
 
state version {
state_entry() {
// DEBUG
llOwnerSay("[AI] Sending to AI for processing...");
messageHash = llSHA1String(data);
llInstantMessage(
wasKeyValueGet(
"corrade",
@@ -301,7 +414,7 @@
),
wasKeyValueEncode(
[
"command", "ai",
"command", "MQTT",
"group", wasURLEscape(
wasKeyValueGet(
"group",
@@ -314,8 +427,48 @@
configuration
)
),
"action", "process",
"message", wasURLEscape(data),
"action", "publish",
// Corrade AI listening host.
"host", wasURLEscape(
wasKeyValueGet(
"ai host",
configuration
)
),
// Corrade AI listening port.
"port", wasURLEscape(
wasKeyValueGet(
"ai port",
configuration
)
),
// Corrade AI credentials.
"username", wasURLEscape(
wasKeyValueGet(
"ai username",
configuration
)
),
"secret", wasURLEscape(
wasKeyValueGet(
"ai secret",
configuration
)
),
// Use the SIML module of Corrade AI.
"topic", "SIML",
"payload", wasURLEscape(
wasKeyValueEncode(
[
// The hash is an identifier that will allow responses from Corrade AI
// for various messages to be distinguished. It can be any identifier
// but a handy way of generating an identifier is to hash the message.
"Hash", messageHash,
// Note the double escaping!
"Message", wasURLEscape(data)
]
)
),
"callback", wasURLEscape(URL)
]
)
@@ -325,20 +478,63 @@
http_request(key id, string method, string body) {
llHTTPResponse(id, 200, "OK");
llReleaseURL(URL);
if(wasKeyValueGet("command", body) != "ai" ||
if(wasKeyValueGet("command", body) != "MQTT" ||
wasKeyValueGet("success", body) != "True") {
// DEBUG
llOwnerSay("[AI] Unable to get processed message: " +
llOwnerSay("[AI] Unable to publish message: " +
wasURLUnescape(
wasKeyValueGet("error", body)
wasKeyValueGet("data", body)
)
);
state listen_group;
}
// Get the processed message.
data = wasKeyValueGet("data", body);
// DEBUG
llOwnerSay("[AI] Message published successfully...");
}
link_message(integer sender, integer num, string message, key id) {
// We only care about notifications now.
if(id != "notification")
return;
// Listen to MQTT messages.
if(wasKeyValueGet("type", message) != "MQTT")
return;
// Get the sent message.
data = wasURLUnescape(
wasKeyValueGet(
"payload",
message
)
);
string hash = wasURLUnescape(
wasKeyValueGet(
"Hash",
data
)
);
string serverMessage = wasURLUnescape(
wasKeyValueGet(
"ServerMessage",
data
)
);
// Skip generated messages that are not for the published message.
if(hash != messageHash ||
serverMessage != "True")
return;
data = wasURLUnescape(
wasKeyValueGet(
"Message",
data
)
);
state tell;
}
timer() {
@@ -385,7 +581,7 @@
)
),
"entity", "group",
"message", data // message is already encoded
"message", wasURLEscape(data)
]
)
);