<?php
// namespace Yandex\Translate;
class NewsCore extends MessageProcessor {
private $databseFriend;
function __construct($botUsername,$botToken,$webhookUrl,$sign){
parent::__construct($botUsername,$botToken,$webhookUrl,$sign);
$this->databseFriend = new NewsDatabaseHandler($this->botUsername);
}
public function getMoreContent($newsId){
$news=$this->databseFriend->getNewsFromDbById($newsId);
$miniCurl = new MiniCurl($news['link']);
return $miniCurl->getData();
}
public function processCallback($update){
// $testMessage = var_export ( $update, true );
// file_put_contents ( "testbotprocessCallback.html", $testMessage, FILE_APPEND | LOCK_EX );
// $this->apiRequest ( "answerInlineQuery", array (
// 'inline_query_id' => $this->chat_id,
// "results" => $this->callback_data
// ) );
$callBackData= explode(":", $this->callback_data);
if($callBackData[0] == "task" && $callBackData[1] == "more" ){
$message = " ادامه خبر";
$newsId=$callBackData[2];
$newsContent = $this->getMoreContent($newsId);
$regex ='/articleBody\s[^&]*p>(.*)<\/strong>/';
if (preg_match_all ( $regex, $newsContent, $matches )) {
$moreText = $matches [1] [0];
}
$moreText = str_replace("<strong>", "", $moreText);
$moreText = str_replace("<em>", "", $moreText);
$moreText = str_replace("<", "", $moreText);
$moreText = str_replace("</em", "", $moreText);
// $testMessage = var_export ( $newsContent, true );
// file_put_contents ( "testgrabdata.html", $testMessage, FILE_APPEND | LOCK_EX );
$message.=PHP_EOL.PHP_EOL.$moreText;
}
else if($callBackData[0] == "task" && $callBackData[1] == "translate"){
// $key = "trnsl.1.1.20161227T200147Z.ea68ee52a784814b.d57d90672c621ed4b707e054a845d8e0367ad95d";
// $translated = file_get_contents("https://translate.yandex.net/api/v1.5/tr.json/translate?key="
// .$key."&lang=de-fa&text=abmachen");
$key="trnsl.1.1.20161227T200147Z.ea68ee52a784814b.d57d90672c621ed4b707e054a845d8e0367ad95d";
$translator = new Translator($key);
$message = $translator->translate($this->message['text'], "de-fa");
$message .= " ";
}
else if($callBackData[0] == "task" && $callBackData[1] == "approve"){
$postbaker = new PostBaker ( $this->botUsername, $this->BOT_TOKEN, $this->sign );
$postbaker->postNews( $this->message );
$this->apiRequest ( "sendMessage", array (
'chat_id' => $this->chat_id,
"text" => $message,
));
$keyboard = [
'inline_keyboard' => [[['text' => "ارسال شد", 'callback_data' => "task:nothing:nothing"]]],
];
// $keyboard = [
// 'inline_keyboard' => [[]],
// ];
$markup = json_encode($keyboard, true);
$this->apiRequest ( "editMessageReplyMarkup", array (
'chat_id' => $this->chat_id,
"message_id" => $this->message['message_id'],
'reply_markup' => $markup
) );
$message = "ترجمه خبر در کانال قرار داده شد";
$this->apiRequest ( "sendMessage", array (
'chat_id' => $callBackData[2],
"text" => $message
) );
}
else if($callBackData[0] == "task" && $callBackData[1] == "deny"){
$message = "ترجمه خبر نیاز به اصلاح دارد";
$this->apiRequest ( "sendMessage", array (
'chat_id' => $callBackData[2],
"text" => $message
) );
}
$this->apiRequest ( "answerCallbackQuery", array (
'callback_query_id' => $this->callback_id,
"text" => "اضافه شد"
) );
$this->apiRequest ( "sendMessage", array (
'chat_id' => $this->chat_id,
"text" => $message
) );
}
public function processText($update) {
// incoming text message
$text = $this->message ['text'];
if(!empty($this->message ['reply_to_message']))
$repliedTomessage = $this->message ['reply_to_message'];
if (! empty ( $repliedTomessage ['text'] ) && !empty($repliedTomessage['entities'][1]['url'])) {
$postbaker = new PostBaker ( $this->botUsername, $this->BOT_TOKEN, $this->sign );
$postbaker->sendToApproveGroup ( $repliedTomessage, $this->message ['text'].var_export($repliedTomessage) );
} else if(! empty ( $repliedTomessage ['text'] ) && empty($repliedTomessage['entities'][1]['url'])) {
$reply = explode ( "@", $repliedTomessage ['text'] );
$this->apiRequestWebhook ( "sendMessage", array (
'chat_id' => $reply [0],
"text" => $text
) );
} else {
$newsMaker = new PostFromInternet ( $this->botUsername, $text );
if ($newsMaker->hasTopic) {
$news = $newsMaker->getPost ();
$counterNews = 5;
for($i = 0; $i < $counterNews; $i ++) {
// $newsText = "*".$news [$i]['title']."*";
$newsText = "<b>" . $news [$i] ['title'] . "</b>";
$newsText = str_replace("*** BILDplus Inhalt ***", "", $newsText);
$newsText .= "<a href='" . $news [$i] ['media2'] . "'> ";
// $newsText .= $news [$i]['alt'];
$newsText .= "</a>";
// $newsText .= "[ ](".$news [$i]['media2'].")";
$newsText .=PHP_EOL.PHP_EOL. $news [$i] ['alt'];
// $newsText .= "</br>";
$news [$i] ['desc'] = str_replace ( "<br", "", $news [$i] ['desc'] );
$news [$i] ['desc'] = str_replace ( "/>", "", $news [$i] ['desc'] );
// $newsText .= "<pre> ".$news [$i]['desc']."</pre>";
$newsText .= PHP_EOL.PHP_EOL.$news [$i] ['desc'];
$newsText .= PHP_EOL."<pre> " . $news [$i] ['pubdate'] . "</pre>";
$newsId = $this->databseFriend->addNewsToDb($news [$i]);
// $newsId = 0;
// $link = $news [$i];
// $link = $news [$i] ['link'];
// $newsText.=$link;
$keyboard = [
'inline_keyboard' => [[['text' => "اطلاعات بیشتر خبر", 'callback_data' => "task:more:$newsId"],['text' => "کمک در ترجمه", 'callback_data' => "task:translate:$newsId"]]],
];
// $keyboard = [
// 'inline_keyboard' => [[['text' => "این خبر را reply کنید و ترجمه را وارد کنید", 'callback_data' => "task:translate:$newsId"]],
// [['text' => "اطلاعات بیشتر خبر", 'callback_data' => "task:more:$newsId"]]],
// ];
$markup = json_encode($keyboard, true);
$this->apiRequest ( "sendMessage", array (
'chat_id' => $this->chat_id,
"text" => $newsText,
'reply_markup' => $markup,
"parse_mode" => "html"
) );
}
// die("stopped");
$this->apiRequest ( "sendMessage", array (
'chat_id' => $this->chat_id,
// "text" => $text." hi". $newsMaker->hasTopic
"text" => "در صورت ترجمه خبر آن را در @khabaralman خواهید دید. آیا مایلید خبری را ترجمه کنید؟"
) );
}
$this->apiRequest ( "sendMessage", array (
'chat_id' => 44903244,
"text" => $this->message['from']['id']."@@@".$this->message['from']['username']."--".$this->message['from']['first_name']." ".$this->message['from']['last_name'].$this->message ['text']
) );
}
// $this->apiRequest ( "sendMessage", array (
// 'chat_id' => $this->chat_id,
// // "text" => $text." hi". $newsMaker->hasTopic
// "text" => "در حال ساخت"
// ) );
}
}