• If you need help or want to discuss things, you now can also join us on our Discord Server!
  • A first preview of the unlimited version of SinusBot can be found in the Upcoming Changes thread. A version for Windows will follow, but we don't have a release date, yet.

EN Php Api WebPanel Coding

irgendwr

no longer active, "retired" staff member
is awesome!
V.I.P.
is uber awesome!
Contributor
Insider
It's really simple just follow these 5 steps:

Step 1: learn programming
Step 2: read the api docs
Step 3: write code
Step 4: ...
Step 5: profit!
 

literary.tr

New Member
It's really simple just follow these 5 steps:

Step 1: learn programming
Step 2: read the api docs
Step 3: write code
Step 4: ...
Step 5: profit!

I know a bit of programming, I But for the first time with api system is a trying coding. Do you or sinusbot.com have examples?
 

cakemasher

Well-Known Member
Contributor
The API exists out of HTTP calls of which most of them require a authentication header. This authentication header exists out of a token which you receive after using the login call explained here.

I would recommend using curl to perform the HTTP requests. The data content should be a JSON string, as stated in the API-docs.

See the following (not tested!) for an example of using a CURL post request for the login function:
PHP:
<?php

    /* Array containing the information you would like to send to the API server. */
    $data = Array (
        'username'    => 'the users account name',
        'password'    => 'the users password',
        'botId'        => 'the bot id you want to log in to'
    );
    
    /* Convert the array to a json string. */
    $data_string = json_encode ($data);
    
    /* Create a curl object, providing the API url. */
    $ch = curl_init ('http://127.0.0.1/api/v1/bot/login');
    
    /* Define we want to perform a POST request. */
    curl_setopt ($ch, CURLOPT_POST, true);
    
    /* Enter the data we want to send with the POST request (in our case, the json string we've generated). */
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $data_string);
    
    /* Set the returntransfer op true, basically saying we want to receive data. */
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    
    /* Create HTTP headers, containing the Content-Type (telling the server we're sending JSON data) and Content-Length (telling the server how long our data is). */
    curl_setopt ($ch, CURLOPT_HTTPHEADER, Array (
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
    );
    
    /* Execute the request, and save the data in $result. */
    $result = curl_exec ($ch);
    
    /* Show the results. */
    print_r ($result);
    
?>

Executing a login call with valid login information will return a token. This token is required for future commands as you need it to authorize with the API server. The token must be added to each http header as 'Authorization: bearer here-your-token' as described in the API-docs.

Good luck!
 

Lala Sabathil

Donor
is awesome!
Contributor
Insider
here a little php class for it

PHP:
<?php
    class Sinusbot {

        var $token;
        var $botId;
        var $host;
        var $api = "/api/v1/bot";

        function __construct($sinushost, $bot) {
            $this->host = $sinushost;
            $this->botId = $bot;
            $this->token = null;
        }

        function login($username, $password) {

            $data = array(
                'username' => $username,
                'password' => $password,
                'botId' => $this->botId
            );

            $data_string = json_encode($data);

            $ch = curl_init($this->host.$this->api."/login");

            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

            $result = json_decode(curl_exec($ch), true);
            curl_close($ch);

            if($result['success']) {
                $token = $result['token'];
                $this->token = $token;

                $botId = $result['botId'];
                $this->botid = $botId;

                $result = array(
                    "success" => 1,
                    "message"=> null,
                    "data" => array(
                        "token"=> $token,
                        "botid"=> $botId
                    )
                );
            } else {
                $result = array(
                    "success" => 0,
                    "message"=> "Exception",
                    "data" => null
                );
            }

            return $result;
        }

        function disconnect($instanceId) {
            $ch = curl_init($this->host.$this->api."/i/".$instanceId."/kill");

            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization: bearer '.$this->token));

            $result = json_decode(curl_exec($ch), true);
            curl_close($ch);

            if($result['success']) {
                $result = array(
                    "success" => 1,
                    "message"=> null
                );
            } else {
                $result = array(
                    "success" => 0,
                    "message"=> "Exception"
                );
            }

            return $result;
        }
        
        function connect($instanceId) {
            $ch = curl_init($this->host.$this->api."/i/".$instanceId."/spawn");

            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization: bearer '.$this->token));

            $result = json_decode(curl_exec($ch), true);
            curl_close($ch);

            if($result['success']) {
                $result = array(
                    "success" => 1,
                    "message"=> null
                );
            } else {
                $result = array(
                    "success" => 0,
                    "message"=> "Exception"
                );
            }

            return $result;
        }

        function eventCall($instanceId, $event, $uid) {

            $data = array(
                'uid' => $uid
            );

            $data_string = json_encode($data);

            $ch = curl_init($this->host.$this->api."/i/".$instanceId."/event/".$event);

            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization: bearer '.$this->token));

            $result = json_decode(curl_exec($ch), true);
            curl_close($ch);

            if($result['success']) {
                $result = array(
                    "success" => 1,
                    "message"=> null
                );
            } else {
                $result = array(
                    "success" => 0,
                    "message"=> "Exception"
                );
            }

            return $result;
        }
    }

?>
 

literary.tr

New Member
Thank you. I Want to Ask. with 'serverPort' => $changePort The command not running. But when I'm send data with 'ServerPort' => 9987 it works.
 
Top