Advertisement
Gayngel

Persistent_URL_Greeter_Server

Oct 22nd, 2021
1,712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Script by Gayngel of The Script Yard.
  2.  
  3. //Join The Script Yard Group: secondlife:///app/group/4921b4f0-c21e-7866-c0c2-791d10bb5502/about
  4. //Visit The Script Yard Marketplace: https://marketplace.secondlife.com/stores/149734
  5.  
  6. // This is a barebones script to store the scripts url persistently in a Google Sheet.
  7.  
  8. // The server will send a greeting when requested by a client terminal.
  9.  
  10.  
  11.  
  12. // The server saves it's url to a Google Sheet via Google App Script. The client will request the url of the server from the spreadsheet and then request the server to greet the avatar who touched the object and requested a greeting from the menu.
  13.  
  14.  
  15. // Get the Google App Script from https://pastebin.com/Cc9ZbYyj and follow the instructions in the app script to setup.
  16.  
  17. // Once set up paste the app url below.  
  18.  
  19. string app_url = "https://script.google.com/macros/s/AKfycbz-rAtEqm5clysDSL3l3-9kYqfwrDjSpFg7u4PuF9208F-ON3g/exec"; // place the url of Google App Script here.
  20.  
  21. string spreadsheet = ""; // The Google App Script will create a spreadsheet in your Google Drive to store the url of the drop box. You can write a name here or leave it blank. If you leave the string blank the spreadsheet will automatically be called the "NameOfParcelDropBoxIsOn Dropbox".
  22.  
  23.  
  24. list parceldetails;
  25. string parcelname;
  26.  
  27. string url;
  28. key reqURL;
  29. key http_req;
  30. key sheet_req;
  31. integer allow =FALSE;
  32.  
  33. default
  34. {
  35.    
  36.     on_rez(integer start_param)
  37.     {
  38.      llSetObjectDesc("");  
  39.      llResetScript();  
  40.        
  41.     }
  42.    
  43.    
  44.     state_entry()
  45.     {
  46.       allow = FALSE;                  // verbosity just in case
  47.       llReleaseURL(url);             // verbosity just in case
  48.      
  49.       parceldetails = llGetParcelDetails(llGetPos(),[PARCEL_DETAILS_NAME]);
  50.       parcelname = llList2String(parceldetails,0);
  51.      
  52.       reqURL =llRequestURL();
  53.    
  54.     }
  55.    
  56.    
  57.    http_request(key id, string method, string body)
  58.     {
  59.          
  60.      
  61.     if (id == reqURL)
  62.     {
  63.         if (method == URL_REQUEST_DENIED)
  64.         llOwnerSay("The following error occurred while attempting to get a free URL for this device:\n \n" + body);
  65.  
  66.         else if (method == URL_REQUEST_GRANTED)
  67.         {
  68.         url = body;
  69.        
  70.        
  71.            
  72.         }
  73.        
  74.          if(app_url != "")
  75.         {
  76.            
  77.              http_req = llHTTPRequest(app_url+"?server_url="+url+"&obj_key="+(string)llGetKey(),[HTTP_METHOD,"GET"],"");  // send the server's url to the spreadsheet to be stored.
  78.        
  79.        
  80.         }
  81.         else
  82.         llOwnerSay("Please add the URL of the Google App Script at the top of the script");
  83.        
  84.        
  85.        
  86.      } //  if (id == reqURL)
  87.        
  88.        
  89.        
  90.          else if (method == "POST")
  91.         {
  92.            
  93.             list tmp = llParseString2List(body,["="],[""]);
  94.             string cmd = llToLower(llList2String(tmp,0));
  95.             string ref = llList2String(tmp,1);
  96.            
  97.            
  98.            
  99.             if(cmd == "url_ok")
  100.          {
  101.          
  102.            
  103.          
  104.            
  105.          string ssID = llGetObjectDesc();
  106.          
  107.          if(ssID == "")  // if the spreadsheet doesn't exist create one
  108.          {
  109.          
  110.          
  111.          
  112.          if(spreadsheet == "")
  113.          {
  114.              
  115.            spreadsheet = parcelname + " greeter.";    
  116.              
  117.          }
  118.          
  119.          llOwnerSay("Creating spreadsheet named " + spreadsheet);
  120.          
  121.          
  122.          if(app_url != "")
  123.         sheet_req = llHTTPRequest(app_url+ "?url="+url+"&spreadsheet=" + llEscapeURL(spreadsheet),[HTTP_METHOD,"POST"],"Create");   // instruct the App Script to create a spreadsheet
  124.          }
  125.          
  126.         else if(ssID != "") // if the spreadsheet exists
  127.         {
  128.            
  129.             llOwnerSay("The spreadsheet database is ready.");    
  130.            
  131.            llOwnerSay("This server can now be contacted");
  132.            allow = TRUE;
  133.              sheet_req = llHTTPRequest(app_url+ "?url="+url,[HTTP_METHOD,"POST"],"Send_URL");   // update the url of the server and store in the spreadsheet
  134.         }
  135.            
  136.          } //if(cmd == "url_ok")
  137.          
  138.          
  139.          else if(cmd == "url_invalid")
  140.          {
  141.               llSetObjectDesc("");  
  142.            
  143.             llOwnerSay("Can not save this server's URL to the spreadsheet as the Google App Script is tied to another server. Please create a new app script and paste it's url at the top of the script.");
  144.              
  145.          }
  146.            
  147.              
  148.            
  149.            
  150.             else if(cmd == "spreadsheetready")
  151.             {
  152.              llSetObjectDesc(ref);
  153.              
  154.              llOwnerSay("The spreadsheet database has been created.");  
  155.              
  156.              
  157.                allow = TRUE;
  158.                 sheet_req = llHTTPRequest(app_url+ "?url="+url,[HTTP_METHOD,"POST"],"Send_URL");
  159.                
  160.                 llOwnerSay("This server can now be contacted");  
  161.             }
  162.            
  163.             else if(cmd == "url_added")
  164.             {
  165.                
  166.                 llOwnerSay("The url of your server has been updated in the spreadsheet database.");
  167.                
  168.             }
  169.            
  170.             else if(cmd == "request_greeting")
  171.             {
  172.                
  173.          
  174.                
  175.               key av = (key)ref;
  176.              
  177.              
  178.                
  179.               if(allow == TRUE)  
  180.               {
  181.                  
  182.                  
  183.                  
  184.                 llInstantMessage(av,"Hello World!");
  185.                  
  186.                  
  187.               }
  188.              
  189.              }  //else if(cmd == "request_redelivery")
  190.            
  191.            
  192.                  
  193.         } // method is post
  194.        
  195.        
  196.        
  197.        
  198.     }
  199.    
  200.    
  201.  
  202.    
  203.    
  204.    
  205.     changed(integer change)
  206.     {
  207.        
  208.       if(change & CHANGED_REGION_START || change & CHANGED_REGION || change & CHANGED_INVENTORY || change & CHANGED_TELEPORT)
  209.       {
  210.        llReleaseURL(url);    // verbosity just in case
  211.       llResetScript();
  212.       }
  213.        
  214.        
  215.     }
  216.    
  217. }
  218.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement