import logging import requests from telegram import Update from telegram.ext import Application, CommandHandler, ContextTypes # Настройка логирования logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) # API для получения курсов валют API_KEY = "d6f66e833788bf56b7394f36" API_URL = f"https://v6.exchangerate-api.com/v6/{API_KEY}/latest/" async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await update.message.reply_text('Привет! Используй команду /convert сумма из_валюты в_валюту для конвертации. Например: /convert 100 USD EUR') async def convert(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: try: args = context.args if len(args) != 3: await update.message.reply_text('Используй команду так: /convert сумма из_валюты в_валюту. Например: /convert 100 USD EUR') return amount, from_currency, to_currency = args amount = float(amount) from_currency = from_currency.upper() to_currency = to_currency.upper() # Запрос к API с отключенной проверкой SSL response = requests.get(f"{API_URL}{from_currency}", verify=False) logger.info(f"Ответ API: {response.status_code} {response.text}") # Логирование ответа API response.raise_for_status() # Проверка на ошибки HTTP data = response.json() if data.get("result") == "error": error_message = data.get("error-type", "Неизвестная ошибка API") await update.message.reply_text(f'Ошибка API: {error_message}') return if to_currency not in data.get('conversion_rates', {}): await update.message.reply_text(f'Валюта {to_currency} не найдена.') return rate = data['conversion_rates'][to_currency] converted_amount = amount * rate await update.message.reply_text(f'{amount} {from_currency} = {converted_amount:.2f} {to_currency}') except ValueError: await update.message.reply_text('Пожалуйста, введите корректную сумму.') except requests.exceptions.RequestException as e: logger.error(f"Ошибка при запросе к API: {e}") await update.message.reply_text('Произошла ошибка при запросе к API. Попробуйте позже.') except Exception as e: logger.error(f"Ошибка: {e}") await update.message.reply_text('Произошла ошибка при конвертации. Проверь правильность ввода данных.') def main() -> None: # Ваш токен Telegram бота token = "7984868906:AAEogzeyGIQhs6UiJZ8uXW2cDxqFbjCqgEg" logger.info(f"Используемый токен: {token}") try: application = Application.builder().token(token).build() logger.info("Приложение успешно создано") application.add_handler(CommandHandler("start", start)) application.add_handler(CommandHandler("convert", convert)) logger.info("Обработчики команд добавлены") application.run_polling() logger.info("Бот запущен и ожидает сообщений") except Exception as e: logger.error(f"Ошибка при запуске бота: {e}") if __name__ == '__main__': main()