Multivit4min

Untitled

Apr 3rd, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.35 KB | None | 0 0
  1. <?php
  2.  
  3. class Bot {
  4.    
  5.     public $token;
  6.    
  7.     public function __construct($botHost = '127.0.0.1', $botPort = 8087, $botId = NULL) {
  8.         $this->Host = $botHost;
  9.         $this->Port = $botPort;
  10.         $this->botId = $botId;
  11.         if ($this->botId == NULL) $this->botId = $this->DefaultBot();
  12.     }
  13.    
  14.     public function DefaultBot() {
  15.         $ch = curl_init();
  16.         curl_setopt_array($ch, [
  17.             CURLOPT_URL => 'http://'.$this->Host.':'.$this->Port.'/api/v1/botId',
  18.             CURLOPT_RETURNTRANSFER => 1
  19.         ]);
  20.         $data = curl_exec($ch);
  21.         curl_close($ch);
  22.         $json = json_decode($data, TRUE);
  23.         return $json['defaultBotId'];
  24.     }
  25.    
  26.     public function Info() {
  27.         $ch = curl_init();
  28.         curl_setopt_array($ch, [
  29.             CURLOPT_URL => 'http://'.$this->Host.':'.$this->Port.'/api/v1/bot/instances',
  30.             CURLOPT_RETURNTRANSFER => 1,
  31.             CURLOPT_HTTPHEADER => ['Authorization: bearer '.$this->token]
  32.         ]);
  33.         $data = curl_exec($ch);
  34.         curl_close($ch);
  35.         return json_decode($data,1)[0];
  36.     }
  37.    
  38.     public function Login($username, $password) {
  39.         $ch = curl_init();
  40.         curl_setopt_array($ch, [
  41.             CURLOPT_URL => 'http://'.$this->Host.':'.$this->Port.'/api/v1/bot/login',
  42.             CURLOPT_RETURNTRANSFER => 1,
  43.             CURLOPT_POSTFIELDS => json_encode(['username' => $username, 'password' => $password, 'botId' => $this->botId]),
  44.             CURLOPT_HTTPHEADER => ['Content-Type: application/json']
  45.         ]);
  46.         $data = curl_exec($ch);
  47.         $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  48.         curl_close($ch);
  49.         if ($code != 200) return $code;
  50.         $this->token = json_decode($data, TRUE)['token'];
  51.         return true;
  52.     }
  53.    
  54.     public function setAvatar($image) {
  55.         $ch = curl_init();
  56.         curl_setopt_array($ch, [
  57.             CURLOPT_URL => 'http://'.$this->Host.':'.$this->Port.'/api/v1/bot/i/'.$this->Info()['uuid'].'/avatar',
  58.             CURLOPT_RETURNTRANSFER => 1,
  59.             CURLOPT_POSTFIELDS => $image,
  60.             CURLOPT_HTTPHEADER => ['Authorization: bearer '.$this->token, 'Content-Type: image/png']
  61.         ]);
  62.         $data = curl_exec($ch);
  63.         $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  64.         curl_close($ch);
  65.         return $code;
  66.     }
  67. }
Add Comment
Please, Sign In to add comment