最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python 3.x - How to send a .ovpn file via Telegram bot so that it opens on the android? - Stack Overflow

programmeradmin2浏览0评论

I am writing a telegram bot that generates .ovpn files and sends them to users. I have the following code:

async def generate_certificate(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
    """Генерация сертификата и отправка файла пользователю."""
    username = update.message.text.strip()

    # Команда для генерации сертификата
    command = f"/root/VPN-TIME/src/scripts/ovpn-gen.sh /root/easy-rsa-master/easyrsa3 /root/OVPNS /root/user-conf.ovpn {username}"

    try:
        # Выполнение команды bash
        result = subprocess.run(command, shell=True, capture_output=True, text=True, check=True)

        # Получаем путь к сгенерированному файлу
        generated_file_path = result.stdout.strip()

        if generated_file_path:
            # Отправляем файл пользователю
            with open(generated_file_path, 'rb') as file:
                await context.bot.send_document(chat_id=update.effective_chat.id, document=file)

            await update.message.reply_text(f"Сертификат для пользователя {username} успешно сгенерирован и отправлен.")
        else:
            await update.message.reply_text("Ошибка при генерации сертификата.")
    except subprocess.CalledProcessError as e:
        await update.message.reply_text(f"Произошла ошибка при запуске скрипта: {e}")
    except FileNotFoundError:
        await update.message.reply_text(f"Файл не найден: {generated_file_path}")
    except Exception as e:
        await update.message.reply_text(f"Ошибка: {e}")

    return ConversationHandler.END

Generation is successful, I receive a correct .ovpn file. Both its contents and its name are correct. I can even successfully connect to OpenVPN using it but there is one problem:

When I send this file to myself (manually), go to Telegram from my phone and click on the file, it immediately opens in openvpn connect. However, if I received this file through a bot, then when I click on the file, it does not open in openvpn connect (see screenshot)

ChatGPT suggested that I solve the problem by specifying the mime type:

mime_type="application/x-openvpn-profile"

However, I get an error:

ExtBot.send_document() got an unexpected keyword argument 'mime_type'

ChatGPT suggested that I check the .ovpn file for validity (and it is valid, I checked it) and forcefully add the ".onvpn" extension to the file before sending, and it is already there

发布评论

评论列表(0)

  1. 暂无评论