Last active
October 4, 2017 10:38
-
-
Save sebsel/05368dd1d75b1b65cd878d241ed46bd6 to your computer and use it in GitHub Desktop.
Bot Framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-Type: application/json'); | |
class Bot { | |
const xrayUrl = ''; // host your own XRay! | |
const logfile = 'log.txt'; | |
const names = [ | |
// add names for your bot here, so it can ignore itself | |
// IMPORTANT! because it will loop and flood if you're not careful | |
]; | |
protected $message; | |
protected $http_code; | |
protected $error; | |
public function __construct() | |
{ | |
$this->message = json_decode(file_get_contents("php://input")); | |
} | |
public function respond($message, $ignoreSelf = true) | |
{ | |
if($ignoreSelf) $this->ignoreSelf(); | |
if(is_array($message)) { | |
$message = $message[mt_rand(0, count($message) - 1)]; | |
} | |
echo json_encode([ | |
'content' => substr($message,0,255), | |
]); | |
} | |
public function message() | |
{ | |
return $this->message->content; | |
} | |
public function getMatch($number = 0) | |
{ | |
return isset($this->message->match[$number]) ? $this->message->match[$number] : null; | |
} | |
public function ignoreSelf() | |
{ | |
if (in_array($this->message->author->uid, self::names)) exit(); | |
} | |
public function lookup($url) | |
{ | |
return json_decode($this->get(self::xrayUrl . urlencode($url))); | |
} | |
public function last_http_code() | |
{ | |
return $this->http_code; | |
} | |
public function last_error() | |
{ | |
return $this->error; | |
} | |
public function get($url) | |
{ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$body = curl_exec($ch); | |
$this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
if(!$body) { | |
$this->error = curl_error($ch); | |
} | |
curl_close($ch); | |
return $body; | |
} | |
public function post($url, $fields, $bearer = null) | |
{ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
if ($bearer) { | |
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer '.$bearer]); | |
} | |
$body = http_build_query($fields); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); | |
$resp = curl_exec($ch); | |
$this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
if(!$resp) { | |
$this->error = curl_error($ch); | |
} | |
curl_close($ch); | |
return $resp; | |
} | |
public function logMessage() | |
{ | |
$log = json_encode([$this->message], JSON_PRETTY_PRINT); | |
file_put_contents(self::logfile, strftime('%T')."\n\n".$log."\n\n".$this->message->response_url); | |
} | |
} | |
$bot = new Bot; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment