<?php
class Bot {
public $token;
public function __construct($botHost = '127.0.0.1', $botPort = 8087, $botId = NULL) {
$this->Host = $botHost;
$this->Port = $botPort;
$this->botId = $botId;
if ($this->botId == NULL) $this->botId = $this->DefaultBot();
}
public function DefaultBot() {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'http://'.$this->Host.':'.$this->Port.'/api/v1/botId',
CURLOPT_RETURNTRANSFER => 1
]);
$data = curl_exec($ch);
curl_close($ch);
$json = json_decode($data, TRUE);
return $json['defaultBotId'];
}
public function Info() {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'http://'.$this->Host.':'.$this->Port.'/api/v1/bot/instances',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => ['Authorization: bearer '.$this->token]
]);
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data,1)[0];
}
public function Login($username, $password) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'http://'.$this->Host.':'.$this->Port.'/api/v1/bot/login',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS => json_encode(['username' => $username, 'password' => $password, 'botId' => $this->botId]),
CURLOPT_HTTPHEADER => ['Content-Type: application/json']
]);
$data = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code != 200) return $code;
$this->token = json_decode($data, TRUE)['token'];
return true;
}
public function setAvatar($image) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'http://'.$this->Host.':'.$this->Port.'/api/v1/bot/i/'.$this->Info()['uuid'].'/avatar',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS => $image,
CURLOPT_HTTPHEADER => ['Authorization: bearer '.$this->token, 'Content-Type: image/png']
]);
$data = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $code;
}
}
Comments