This commit is contained in:
2025-08-14 21:51:09 +03:00
parent d522cbef83
commit 79c82c5109
20 changed files with 365 additions and 56 deletions

View File

@@ -1,5 +1,17 @@
<?php
use Bastion\ApplicationFactory;
$sysLibPath = rtrim(DIR_SYSTEM, '/') . '/library/oc_telegram_shop';
$basePath = rtrim(DIR_APPLICATION, '/') . '/..';
if (is_readable($sysLibPath . '/oc_telegram_shop.phar')) {
require_once "phar://{$sysLibPath}/oc_telegram_shop.phar/vendor/autoload.php";
} elseif (is_dir("$basePath/oc_telegram_shop")) {
require_once "$basePath/oc_telegram_shop/vendor/autoload.php";
} else {
throw new RuntimeException('Unable to locate application directory.');
}
/**
* @property Document $document
* @property Loader $load
@@ -163,6 +175,25 @@ class ControllerExtensionModuleTgshop extends Controller
$this->response->setOutput($this->load->view('extension/module/tgshop_init', $data));
}
public function handle(): void
{
$app = ApplicationFactory::create([
'base_url' => HTTPS_SERVER,
'public_url' => HTTPS_CATALOG,
'telegram' => [
'bot_token' => $this->config->get('module_tgshop_bot_token'),
'chat_id' => $this->config->get('module_tgshop_chat_id'),
'owner_notification_template' => $this->config->get('module_tgshop_owner_notification_template'),
'customer_notification_template' => $this->config->get('module_tgshop_customer_notification_template'),
],
'logs' => [
'path' => DIR_LOGS,
],
]);
$app->bootAndHandleRequest();
}
protected function validate(): bool
{
if (! $this->user->hasPermission('modify', 'extension/module/tgshop')) {

View File

@@ -167,35 +167,23 @@
return;
}
fetch(`https://api.telegram.org/bot${telegramToken}/getUpdates`)
.then(res => res.json())
.then(data => {
if (!data.ok || !data.result.length) {
alert('Не удалось получить обновления от бота. Убедитесь, что вы написали боту нужное кодовое слово.');
return;
fetch('/admin/index.php?route=extension/module/tgshop/handle&api_action=getChatId&user_token={{ user_token }}')
.then(async (res) => {
const data = await res.json().catch(() => null);
if (!res.ok) {
throw new Error(`Ошибка ${res.status}: ${data.message || res.statusText}`);
}
// Ищем последнее сообщение с chat_id
const lastMessage = data.result.reverse().find(update => update.message && update.message.chat);
if (!lastMessage) {
alert('Не найдено сообщений с chat_id.');
return;
}
if (lastMessage.message.text !== 'opencart_get_chatid') {
alert('Ошибка. Последнее сообщение у бота не содержит правильного кодового слова.');
return;
}
const chatId = lastMessage.message.chat.id;
$('#{{ settingKey }}').val(chatId); // подставляем в поле
alert('ChatID успешно получен и подставлен в поле.')
$('#{{ settingKey }}').val(data.data.chat_id);
alert('ChatID успешно получен и подставлен в поле.');
})
.catch(err => {
console.error(err);
alert('Ошибка при получении chat_id. Проверьте токен и соединение.');
alert(err);
});
});
</script>
</div>