Difference between revisions of "PHP/Performing Actions"
m (Gtaskincentral moved page HTTP API/PHP/Performing Actions to PHP/Performing Actions) |
|
(No difference)
|
Revision as of 08:07, 25 August 2016
Performing Actions
We've made it really easy to perform actions with your SmartBot, for example it's as simple as $bot->login();
to log in your bot! We'll take a look at all of the different actions and the different ways in which you can use them below:
To send an Instant Message from your bot, you use the im()
method on your $bot
variable. For example the below will send an IM to GTASkinCentral Resident with a message of "Hello world!".
$bot->im("GTASkinCentral Resident", "Hello World!"); // Send an IM to GTASkinCentral Resident.
If you need to see the response that the bot gives after the action has been executed, it's as simple as calling the response()
method. This method returns the result as an array, however if you would like the raw string format, directly from the SmartBots server, simply use response(TRUE)
instead.
print_r($bot->response()); // Get the bot's response in array format.
Array (
[result] => OK
[action] => im
)
or in string format
echo $bot->response(TRUE); // Get the bot's response in query string format.
result=OK&action=im
Again, we can use a simple 'if' statement to see if an acton was successful or not. This is done by checking whether or not the 'result' parameter in the response is set to TRUE
or FALSE
.
if($bot->im("GTASkinCentral Resident", "Hello World!") == TRUE) { // If the action was successful.
echo "Success!".
} else { // If the action failed.
echo "Fail!".
}
As you may be aware, SmartBots also has a number of commands which return a lot of information. For example, to find out an avatar's name from their UUID or key, you can use the key2name()
method. You can also pass in custom variables this way. All methods accept one custom variable at the end of the action's default variables (see Available Actions).
$bot->key2name("4d9ce772-d3ee-4cce-9555-bfb06ffcb228", "Custom Text"); // Send the key2name command.
print_r($bot->response()); // Print the result.
Array (
[action] => key2name
[result] => OK
[key] => 4d9ce772-d3ee-4cce-9555-bfb06ffcb228
[name] => gtaskincentral resident
[custom] => Custom Text
)
Your SmartBot is now able to accept commands from you! At the end of this section, your code could look a little something like this:
<?php
include("smartbots_api.php"); // Include the SmartBots API file.
$apiKey = "e40e365171a99nl05bdmd697273b573t"; // SmartBots API Key.
$botName = "Example Resident"; // The bot's full name.
$botAccessCode = "KbYpnfa"; // The bot's access code.
$bot = new SmartBot(); // Instansiate a new SmartBot class.
$bot->setup($apiKey, $botName, $botAccessCode); // Pass the setup variables to the API.
$bot->im("GTASkinCentral Resident", "Hello World!"); // Send an IM to GTASkinCentral Resident.
$bot->key2name("4d9ce772-d3ee-4cce-9555-bfb06ffcb228", "Custom Text"); // Send the key2name command.
print_r($bot->response()); // Print the result.
?>