<?php
include dirname(__FILE__,1).'/PostFromInternet.php';
class PostBaker {
private $botDatabaseId;
private $botToken;
private $botUsername;
private $api = "https://api.telegram.org/bot";
private $method ="sendMessage";
private $sign;
private $channelId= '@witzigi';
private $receiverId= '@bilderbuch';
private $newsRoomId= '@khabaralman';
private $approveGroup= -197856501;
// private $newsRoomId= '@botposter';
function __construct($botUsername,$botToken,$sign){
$this->botToken = $botToken;
$this->botUsername = $botUsername;
$this->sign = $sign;
}
public function sendMessage(){
$this->method = "sendMessage";
$url = $this->api.$this->botToken."/".$this->method;
$data = array('chat_id'=>$this->channelId,'text'=>"hi");
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
}
public function postVideo(){
$this->method = "sendVideo";
$url = $this->api.$this->botToken."/".$this->method;
$dbPostHandler = new DatabaseHandler($this->botUsername);
$post = $dbPostHandler->getPost();
print_r($post);
$data = array('chat_id'=>$this->channelId,'video'=>$post['video'],
'caption'=>$post['caption']."\n\n".$this->sign);
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$dbPostHandler->markSent($post);
}
public function postPhoto(){
$this->method = "sendPhoto";
$url = $this->api.$this->botToken."/".$this->method;
$postInternetFinder = new PostFromInternet($this->botUsername);
$photUrl=$postInternetFinder->getPost();
// $dbPostHandler = new DatabaseHandler($this->botUsername);
// $post = $dbPostHandler->getPost();
$data = array('chat_id'=>$this->receiverId,'photo'=>$photUrl,
'caption'=>"hallolaa"."\n\n".$this->sign);
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// $dbPostHandler->markSent($post);
}
public function postPhotoWithURL($photoUrl,$caption){
$this->method = "sendPhoto";
$url = $this->api.$this->botToken."/".$this->method;
// $dbPostHandler = new DatabaseHandler($this->botUsername);
// $post = $dbPostHandler->getPost();
// $testMessage = var_export($photoUrl, true);
// file_put_contents("phototest.html", $testMessage, FILE_APPEND | LOCK_EX);
$data = array('chat_id'=>$this->receiverId,'photo'=>$photoUrl,
'caption'=>$caption."\n\n".$this->sign);
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// $dbPostHandler->markSent($post);
}
public function postNews($newsMessage){
$this->method = "sendMessage";
$url = $this->api.$this->botToken."/".$this->method;
// $NewsParts = explode(PHP_EOL, $newsMessage);
$data = array('chat_id'=>$this->newsRoomId,
'text'=> $newsMessage['text'],
'parse_mode'=>'html');
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
}
public function sendToApproveGroup($targetNews,$translation){
$this->method = "sendMessage";
$url = $this->api.$this->botToken."/".$this->method;
$targetNewsParts = explode(PHP_EOL, $targetNews['text']);
$targetNews['text'] = "<b>".$targetNewsParts[0]."</b>".PHP_EOL.$targetNewsParts[4].PHP_EOL."<pre>".$targetNewsParts[5]."</pre>";
// $dbPostHandler = new DatabaseHandler($this->botUsername);
// $post = $dbPostHandler->getPost();
// $testMessage = var_export($photoUrl, true);
// file_put_contents("targetnews.html", $targetNews, FILE_APPEND | LOCK_EX);
$translatedNews = $translation.PHP_EOL.PHP_EOL
.$targetNews['text'].PHP_EOL.PHP_EOL
. "<a href='".$targetNews['entities'][1]['url']."'> </a>"
.$this->sign;
$keyboard = [
'inline_keyboard' => [[['text' => "تائید و ارسال به کانال", 'callback_data' => "task:approve:".$targetNews ['chat'] ['id']],['text' => "رد خبر", 'callback_data' => "task:deny:".$targetNews ['chat'] ['id']]]],
];
$markup = json_encode($keyboard, true);
$data = array('chat_id'=>$this->approveGroup,
'text'=> $translatedNews,
'reply_markup' => $markup);
// 'parse_mode'=>'html');
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// $dbPostHandler->markSent($post);
}
}
?>