Difference between revisions of "HTTP API/LSL Helper Functions"
From SmartBots Developers Docs
								 (Created page with "The following functions will help you controlling bots from LSL scripts:  {{API HTTP Example Boilerplate}}   {{NavMenu}}  __NOTOC__")  | 
				|||
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
| − | The following functions will help you controlling bots from LSL scripts:  | + | The following functions will help you controlling bots from LSL scripts.  | 
| + | |||
| + | <syntaxhighlight lang="lsl">  | ||
| + | /**  | ||
| + | * Send SmartBots HTTP API command.  | ||
| + | */  | ||
| + | smartbotsAPI(string command, list params) {  | ||
| + |   // You need to adjust the vars below:  | ||
| + |   string sbApiKey = "...";  | ||
| + |   string sbBotName = "...";  | ||
| + |   string sbBotAccessCode = "...";  | ||
| + | |||
| + |   // Populate the query data  | ||
| + |   list query = [  | ||
| + |     "action="  + command,  | ||
| + |     "apikey="  + llEscapeURL(sbApiKey),  | ||
| + |     "botname=" + llEscapeURL(sbBotName),  | ||
| + |     "secret="  + llEscapeURL(sbBotAccessCode)  | ||
| + |   ];  | ||
| + | |||
| + |   integer i;  | ||
| + |   for(i = 0; i<llGetListLength(params); i += 2) {  | ||
| + |     query += [ llList2String(params, i) + "=" + llEscapeURL(llList2String(params, i+1)) ];  | ||
| + |   }  | ||
| + | |||
| + |   string queryString = llDumpList2String(query, "&");  | ||
| + | |||
| + |   llHTTPRequest("https://api.mysmartbots.com/api/bot.html",  | ||
| + |     [HTTP_METHOD,"POST"], queryString);  | ||
| + | }  | ||
| + | |||
| + | /**  | ||
| + | * Send request to SmartBots API and expect the JSON response.  | ||
| + | */  | ||
| + | smartbotsAPIJSON(string command, list params) {  | ||
| + |     smartbotsAPI(command, params + ["dataType", "json"]);  | ||
| + | }  | ||
| + | </syntaxhighlight>  | ||
| + | |||
| + | Usage example:  | ||
| + | |||
| + | <syntaxhighlight lang="lsl">  | ||
| + | smartbotsAPI("im", [  | ||
| + |   "slname", "Glaznah Gassner",  | ||
| + |   "message", "Hello in-world!"  | ||
| + | ]);  | ||
| + | </syntaxhighlight>  | ||
| − | |||
Latest revision as of 14:35, 14 October 2019
The following functions will help you controlling bots from LSL scripts.
/**
* Send SmartBots HTTP API command.
*/
smartbotsAPI(string command, list params) {
  // You need to adjust the vars below:
  string sbApiKey = "...";
  string sbBotName = "...";
  string sbBotAccessCode = "...";
  // Populate the query data
  list query = [
    "action="  + command,
    "apikey="  + llEscapeURL(sbApiKey),
    "botname=" + llEscapeURL(sbBotName),
    "secret="  + llEscapeURL(sbBotAccessCode)
  ];
  integer i;
  for(i = 0; i<llGetListLength(params); i += 2) {
    query += [ llList2String(params, i) + "=" + llEscapeURL(llList2String(params, i+1)) ];
  }
  string queryString = llDumpList2String(query, "&");
 
  llHTTPRequest("https://api.mysmartbots.com/api/bot.html",
    [HTTP_METHOD,"POST"], queryString);
}
/**
* Send request to SmartBots API and expect the JSON response.
*/
smartbotsAPIJSON(string command, list params) {
    smartbotsAPI(command, params + ["dataType", "json"]);
}
Usage example:
smartbotsAPI("im", [
  "slname", "Glaznah Gassner",
  "message", "Hello in-world!"
]);