<?php
class ButtonService extends BaseSerevice
{
public $keyboard;
private $buttonsData;
public $buttons;
function init()
{
parent::init();
$this->buttonsData = [];
$this->initButtons();
$this->keyboard['inline_keyboard'] = [];
}
private function initButtons()
{
foreach ($this->buttonsData as $buttonObj) {
// "text": "\u062e\u0648\u0628",
// "callback_data": "likeWord:c7"
$callBackData = explode(":", $buttonObj[0]['callback_data']);
$button = new Button();
$button->setText($buttonObj[0]['text']);
$button->setKey($callBackData[0]);
$button->setData($callBackData[1]);
$this->addButton($button);
}
}
public function getImageButtons()
{
if($this->getTG()->isAdmin() || $this->getTG()->chat_id == "5591984913"){
$this->addKeyboardMarkup("💬 استخراج متن ", ExtractTextCallbackHandler::$key . ":" . "null");
}
$baseUrlName = $this->createImageUrl();
if(strlen($baseUrlName)>0){
// $this->getTG()->sendMessageLog($imageURl);
$this->keyboard['inline_keyboard'][] = [
[
'text' => "💬 استخراج متن وب ",
'web_app' => ['url' => 'https://app.bayadic.com/ocr-reader?url='.urlencode($baseUrlName)]
]
];
}
// }
$markup = json_encode($this->getKeyboard(), true);
return $markup;
}
public function getVoiceButton()
{
$markup = json_encode($this->getKeyboard(), true);
return $markup;
}
public function createMarkUpButtons()
{
$verbConjId = 0;
if ($this->getTG()->translated && !$this->getTG()->isPersianLanguage($this->getTG()->word) && $this->getTG()->lookForSound) {
try {
$soundFile = "something";
} catch (Exception $ex) {
$this->getTG()->sendMessageLog(var_export($ex, true));
}
}
if ($this->getTG()->translated && !$this->getTG()->isPersianLanguage($this->getTG()->word)) {
if ($verbId = $this->getTG()->getDB()->isVerb($this->getTG()->word))
$verbConjId = $verbId;
}
if (!isset($soundFile)) {
$soundFile = 0;
}
$markup = $this->buildKeyboardMarkup($soundFile, $this->getTG()->word, $verbConjId);
return $markup;
}
public function buildKeyboardMarkup($soundFile, $word, $verbConjId = 0)
{
if(!$this->getTG()->getDB()->translated){
$this->translateInWebApp($word);
}
$this->addSoundKeyboard($word, $soundFile);
$this->addConjunctionkeyboard($word, $verbConjId);
$this->addTranslatorsKeys($word);
$this->addAddToLeitner($word);
$this->addTableSearchWordGame($word);
if($this->getTG()->isAdmin()){
$this->addWebAppItems($word);
}
$markup = json_encode($this->getKeyboard(), true);
return $markup;
}
function addTableSearchWordGame($word){
}
function clearKeyboard()
{
$this->keyboard['inline_keyboard'] = [];
}
function addTranslatorsKeys($word)
{
$isUserTranslator = $this->getTG()->getUser()->hasRole(EnumRole::TRANSLATOR);
}
public function getButtons()
{
return $this->buttons;
}
public function addButton(Button $button)
{
$this->buttons[] = $button;
}
function addSoundKeyboard($word, $soundFile)
{
// if (!empty($soundFile)) {
// $this->addKeyboardMarkup("📣 تلفظ کلمه $word", EnumSpeech::SPEECH_GET_SOUND . ":" . $word);
// }
$this->addWebAppPronounce($word);
}
function getADButton($messageID)
{
$this->addKeyboardMarkup(" تبلیغ 50", CreateAds::$key . ":" . $messageID."-50");
$this->addKeyboardMarkup(" تبلیغ 100", CreateAds::$key . ":" . $messageID."-100");
$this->addKeyboardMarkup(" تبلیغ 200", CreateAds::$key . ":" . $messageID."-200");
$this->addKeyboardMarkup(" تبلیغ 500", CreateAds::$key . ":" . $messageID."-500");
$markup = json_encode($this->getKeyboard(), true);
return $markup;
}
function addExamplesWebapp($word)
{
}
function addConjugationsWebapp($word)
{
}
function addWebAppPronounce($word)
{
}
function addWebAppItems($word)
{
$this->keyboard['inline_keyboard'][] = [
[
'text' => "📣 جدول لغات",
'web_app' => ['url' => 'https://app.bayadic.com/games/wordsearch/'.$word]
]
];
}
function translateInWebApp($word)
{
}
function addAddToLeitner($word)
{
}
function addConjunctionkeyboard($word, $verbConjId)
{
if ($verbConjId > 0) {
// $this->addKeyboardMarkup("🖐 صرف فعل $word", EnumConjuction::GET_CONJUCTION . ":" . $verbConjId);
$this->addConjugationsWebapp($word);
}
}
function addKeyboardMarkup($text, $callbackData)
{
$this->keyboard['inline_keyboard'][] = [['text' => $text, 'callback_data' => $callbackData]];
return $this;
}
function createImageUrl() {
$size = sizeof($this->getTG()->message['photo']);
$fileId = $this->getTG()->message['photo'][$size - 1]['file_id'];
$getFile = "https://api.telegram.org/bot" . $this->getTG()->getBotToken() .
"/getFile?file_id=" . $fileId;
// Get file metadata from Telegram API
$resultFileData = json_decode(file_get_contents($getFile), true);
if (isset($resultFileData['result']['file_path'])) {
$imgUrl = "https://api.telegram.org/file/bot" . $this->getTG()->getBotToken() . "/" . $resultFileData['result']['file_path'];
// Get the image data
$data = file_get_contents($imgUrl);
// Define the folder and filename
$folderPath = '../../../images-web-server/';
if (!file_exists($folderPath)) {
mkdir($folderPath, 0777, true); // Create the folder if it doesn't exist
}
$filePath = $folderPath . basename($resultFileData['result']['file_path']); // Extract the original filename
// Save the image locally
file_put_contents($filePath, $data);
echo "Image saved to: " . $filePath;
return "https://images-bot.bayadic.com/".basename($resultFileData['result']['file_path']);
} else {
echo "Failed to retrieve file path from Telegram API.";
return "";
}
}
function getKeyboard()
{
return $this->keyboard;
}
function getKeyboardMarkedup()
{
return json_encode(
$this->getKeyboard(),
true
);
}
/**
* Set the value of buttonsData
*
* @return self
*/
public function setButtonsData($buttonsData)
{
$this->buttonsData = $buttonsData;
$this->initButtons();
}
public function findButton($key): Button
{
$buttons = &$this->getButtons();
foreach ($buttons as &$button) {
if ($button->getKey() == $key) {
return $button;
}
}
$button = new Button();
$button->setKey($key);
$button->setText("not found");
$button->setUsed(false);
return $button;
}
public function deleteButton($key)
{
$buttons = &$this->getButtons();
foreach ($buttons as $arrayKey => $button) {
if ($button->getKey() == $key) {
unset($this->buttons[$arrayKey]);
}
}
}
public function removeAllButtons()
{
$this->buttons = [];
$this->clearKeyboard();
}
public function rebuildKeyboard()
{
$buttons = $this->getButtons();
$this->clearKeyboard();
foreach ($buttons as $button) {
$this->addKeyboardMarkup($button->getText(), $button->getKey() . ':' . $button->getData());
}
}
}