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

From SmartBots Developers Docs
Jump to: navigation, search
(Created page with "{{DISPLAYTITLE: Sending Instant Message to avatar}} <syntaxhighlight> string sbApiKey="..."; string sbBotName="..."; string sbBotAccessCode="..."; key httpReq=NULL_KEY; defa...")
 
Line 1: Line 1:
 
{{DISPLAYTITLE: Sending Instant Message to avatar}}
 
{{DISPLAYTITLE: Sending Instant Message to avatar}}
<syntaxhighlight>
+
<syntaxhighlight lang="lsl">
 
string sbApiKey="...";
 
string sbApiKey="...";
 
string sbBotName="...";
 
string sbBotName="...";

Revision as of 16:19, 29 June 2016

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);
        }
    }    
}