Accessing Bot Info via PHP - flyth/ts3soundbot GitHub Wiki

Libraries


This is just a quick'n'dirty script to get some information out of the bot. You might want to cache some of the results (botId, token...).

<?php

class Bot {
    public $botHost = '127.0.0.1';
    public $botPort = 8087;
    public $botId = NULL;
    public $token = NULL;
    public function __construct($botHost = '127.0.0.1', $botPort = 8087, $botId = NULL) {
        $this->botHost = $botHost;
        $this->botPort = $botPort;
        if ($botId == NULL) {
            $botId = $this->DefaultBot();
        }
        $this->botId = $botId;
    }
    public function DefaultBot() {
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => 'http://'.$this->botHost.':'.$this->botPort.'/api/v1/botId',
            CURLOPT_RETURNTRANSFER => 1
        ));
        $data = curl_exec($ch);
        curl_close($ch);
        $json = json_decode($data, TRUE);
        return $json['defaultBotId'];
    }
    public function Login($username, $password) {
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => 'http://'.$this->botHost.':'.$this->botPort.'/api/v1/bot/login',
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_POSTFIELDS => json_encode(array('username' => $username, 'password' => $password, 'botId' => $this->botId)),
            CURLOPT_HTTPHEADER => array('Content-Type: application/json')
        ));
        $data = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($code != 200) return NULL;
        $this->token = json_decode($data, TRUE)['token'];
    }
    public function GetInstances() {
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => 'http://'.$this->botHost.':'.$this->botPort.'/api/v1/bot/instances',
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_HTTPHEADER => array('Authorization: bearer '.$this->token)
        ));
        $data = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($code != 200) return NULL;
        $json = json_decode($data, TRUE);
        return $json;
    }
    public function GetInstanceStatus($instanceId) {
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => 'http://'.$this->botHost.':'.$this->botPort.'/api/v1/bot/i/'.$instanceId.'/status',
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_HTTPHEADER => array('Authorization: bearer '.$this->token)
        ));
        $data = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($code != 200) return NULL;
        $json = json_decode($data, TRUE);
        return $json;
    }
}
	
$bot = new Bot('127.0.0.1', 8087);
$bot->Login('admin', 'foobar');
$instances = $bot->GetInstances();

echo '<link href="css location" rel="stylesheet" type="text/css" />';

for ($i = 0; $i < count($instances); $i++) {
    $info = $bot->GetInstanceStatus($instances[$i]['uuid']);
    if ($info['currentTrack'] != NULL && $info['playing']) {
        
		echo '<div class="output'.$i.'">';
  printf("%s is playing %s by %s\n", $instances[$i]['nick'], $info['currentTrack']['title'], $info['currentTrack']['artist']);
  echo '</div>'; 
    } else {
        
		echo '<div class="output'.$i.'">';
  printf("%s is not playing anything right now\n", $instances[$i]['nick']);
  echo '</div>'; 
    }
}
⚠️ **GitHub.com Fallback** ⚠️