Sending Instant Message

From SmartBots Developers Docs
Jump to: navigation, search

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("https://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);
        }
    }    
}