feat: add bot_token validation

This commit is contained in:
2025-08-16 17:17:28 +03:00
parent 9f35acf399
commit d7df5a4b5c
2 changed files with 75 additions and 2 deletions

View File

@@ -339,7 +339,7 @@ HTML,
],
'module_tgshop_bot_token' => [
'type' => 'text',
'type' => 'bot_token',
'placeholder' => 'Введите токен от телеграм бота',
'help' => <<<TEXT
Токен, полученный при создании бота через @BotFather.
@@ -434,7 +434,7 @@ HTML,
return $map;
}
private function updateConfigFromDefaults()
private function updateConfigFromDefaults(): void
{
$defaults = $this->getDefaultConfig();
$settings = $this->model_setting_setting->getSetting('module_tgshop');

View File

@@ -306,6 +306,79 @@
class="form-control"
onfocus="this.select()"
/>
{# BOT TOKEN #}
{% elseif item['type'] == 'bot_token' %}
<div class="input-group">
<span class="input-group-btn">
<button id="{{ settingKey }}-btn" class="btn btn-primary" type="button" onclick="validateBotToken()">
<i class="fa fa-refresh"></i> Проверить Bot Token
</button>
</span>
<input type="text"
name="{{ settingKey }}"
value="{{ attribute(_context, settingKey) }}"
placeholder="{{ item['placeholder'] }}"
id="{{ settingKey }}"
class="form-control"
onfocusout="validateBotToken()"
/>
</div>
<div id="{{ settingKey }}-result-label"></div>
<button class="btn btn-link btn-xs" type="button" data-toggle="collapse" data-target="#{{ settingKey }}-collapse" aria-expanded="false" aria-controls="collapseExample">
Инструкция как создать Bot Token
</button>
<div class="collapse" id="{{ settingKey }}-collapse">
<div class="well">
<p>Подробная инструкция доступна в <a href="https://nikitakiselev.github.io/telecart-docs/#telegram" target="_blank">документации <i class="fa fa-external-link"></i></a>.</p>
</div>
</div>
<script>
function validateBotToken() {
const $input = $('#{{ settingKey }}');
const $resultLabel = $('#{{ settingKey }}-result-label');
const botToken = $input.val();
if (botToken.trim().length === 0) {
$resultLabel
.text(`❌ Введите Bot Token!`)
.css('color', 'red');
return;
}
$input.attr('disabled', true);
$resultLabel.text('Проверяю...');
$.ajax({
url: `https://api.telegram.org/bot${botToken}/getMe`,
method: 'GET',
dataType: 'json',
success: function (resp) {
if (resp.ok) {
const user = resp.result;
$resultLabel
.text(`✅ Бот: @${user.username} (id: ${user.id})`)
.css('color', 'green');
} else {
$resultLabel
.text(`❌ Ошибка: ${resp.description || 'неверный токен'}`)
.css('color', 'red');
}
},
error: function (xhr) {
$resultLabel
.text(`❌ Ошибка соединения (${xhr.status})`)
.css('color', 'red');
},
complete: function () {
$input.attr('disabled', false);
}
});
}
</script>
{% else %}
Unsupported {{ item|json_encode }}
{% endif %}