Difference between revisions of "HTTP API/Examples/Sending Instant Message"

From SmartBots Developers Docs
Jump to: navigation, search
Line 1: Line 1:
 
{{DISPLAYTITLE: Sending Instant Message to avatar}}
 
{{DISPLAYTITLE: Sending Instant Message to avatar}}
 +
The following example demonstrates the usage of [[HTTP_API/Bot_Commands/im|im]] API command.
 +
 +
It sends to everyone who touches your object an instant message.
 +
 
<syntaxhighlight lang="lsl">
 
<syntaxhighlight lang="lsl">
 
string sbApiKey="...";
 
string sbApiKey="...";

Revision as of 16:21, 29 June 2016

The following example demonstrates the usage of im API command.

It sends to everyone who touches your object an instant message.

string sbApiKey="...";
string sbBotName="...";
string sbBotAccessCode="...";

key httpReq=NULL_KEY;

default {
    touch_start(integer total_number) {
        string params = llDumpList2String([
            "action="  + "im",
            "apikey="  + llEscapeURL(sbApiKey),
            "botname=" + llEscapeURL(sbBotName),
            "secret="  + llEscapeURL(sbBotAccessCode),
            "slname="  + llEscapeURL(llDetectedName(0)),
            "message=" + llEscapeURL("Hello there!")
            ],            "&");

        httpReq=llHTTPRequest("http://api.mysmartbots.com/api/bot.html",
            [HTTP_METHOD,"POST"], params);
    }
    
    // This event is required only if you want to process the SmartBots's reply
    http_response(key request_id, integer status, list metadata, string body) {
        if(request_id!=httpReq) return;
        
        // Store result here
        string result;
        string action="";
        string resulttext="";
        
        // Parse server reply
        list pairs=llParseString2List(body,["&"],[]);
        integer i=0;
        for(i=0;i<llGetListLength(pairs);i++) {
            list keyv=llParseString2List(llList2String(pairs,i),["="],[]);
            string keyname=llList2String(keyv,0);
            string value=llUnescapeURL(llList2String(keyv,1));
            
            if(keyname=="result")       result=value;
            if(keyname=="resulttext")   resulttext=value;
            if(keyname=="action")       action=value;
        }
        
        if(result=="FAIL") {
            llOwnerSay("Command '"+action+"' failed: "+resulttext);
        }
    }    
}