# Performing actions

## 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 Alexander Pixels with a message of "Hello world!".

```php
$bot->im("Alexander Pixels", "Hello World!"); // Send an IM to Alexander Pixels. 
```

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.

```php
print_r($bot->response()); // Get the bot's response in array format. 
```

```php
Array (
  [result] => OK
  [action] => im
) 
```

or in string format

```php
echo $bot->response(TRUE); // Get the bot's response in query string format. 
```

```none
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`.

```php
// If the action was successful.
if($bot->im("GTASkinCentral Resident", "Hello World!") == TRUE) {
  echo "Success!";
// If the action failed.
} else {
  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](https://www.mysmartbots.com/dev/docs/HTTP_API/PHP/Available_Actions "HTTP API/PHP/Available Actions")).

```php
// Send the key2name command.
$bot->key2name("4d9ce772-d3ee-4cce-9555-bfb06ffcb228", "Custom Text");

// Print the result. 
print_r($bot->response());
```

```php
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
<?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("Alexander Pixels", "Hello World!"); // Send an IM to Alexander Pizels.
  $bot->key2name("4d9ce772-d3ee-4cce-9555-bfb06ffcb228", "Custom Text"); // Send the key2name command.
  print_r($bot->response()); // Print the result.
?> 
```