Home / Blog & Tutorials / Frameworks

Pyrogram Bot Hosting: Deploy Async Telegram Bots with Auto-Restart & NVMe Speed

Pyrogram is one of the most powerful, asynchronous MTProto libraries available for Python. It empowers developers to build lightning-fast Telegram userbots, channel managers, and group security bots. However, because Pyrogram creates local .session SQLite files, hosting it on ephemeral servers (like standard Heroku or serverless Lambda) leads to session loss.

Why Pyrogram Needs Persistent NVMe Storage

Every time Pyrogram connects to Telegram MTProto datacenters, it writes cryptographic auth keys into a session database. If your host deletes this file on server restart, your bot logs out and prompts for phone authentication again. BotifyHost provides persistent NVMe disk volumes so your sessions are permanently preserved.

Basic Pyrogram Bot Template

# pyrogram_bot.py
import os
from pyrogram import Client, filters
from pyrogram.types import Message

API_ID = int(os.environ.get("API_ID", 1234567))
API_HASH = os.environ.get("API_HASH", "your_api_hash_here")
BOT_TOKEN = os.environ.get("BOT_TOKEN", "your_bot_token_here")

app = Client(
    "botify_session",
    api_id=API_ID,
    api_hash=API_HASH,
    bot_token=BOT_TOKEN
)

@app.on_message(filters.command("ping"))
async def ping_handler(client: Client, message: Message):
    await message.reply_text("🏓 Pong! Bot is running with sub-15ms response times on BotifyHost.")

if __name__ == "__main__":
    print("Starting Pyrogram bot...")
    app.run()

Handling Session Strings Securely

If you are deploying a userbot, use in-memory session_string to avoid storing raw phone credentials:

SESSION_STRING = os.environ.get("SESSION_STRING")
app = Client("userbot", session_string=SESSION_STRING, api_id=API_ID, api_hash=API_HASH)

Deploying on BotifyHost

  1. Create a requirements.txt containing:
    pyrogram>=2.0.106
    tgcrypto>=1.2.5
    (Tip: Installing tgcrypto speeds up Pyrogram MTProto encryption by up to 16x!)
  2. Upload your files to server3.botifyhost.app.
  3. Click Deploy & Run.
BotifyHost

Written by Aashiq Dev

Specialist at BotifyHost Cloud Infrastructure. Passionate about Telegram MTProto protocol, Python asynchronous concurrency, and building high-performance hosting clusters for developers worldwide.

Host Your Telegram Bot 24/7 on BotifyHost

Say goodbye to laptops running overnight and sleeping web dynos. Deploy your bot in 30 seconds with 99.99% uptime, built-in code editor, and live logs.

Need Help? Chat on Telegram