Skip to main content

Subgram python sdk

This SDK will help you to integrate subscriptions into your Telegram bot.

Init the module.

Get your SUBGRAM_TOKEN: https://t.me/subgram_merchant_bot

from subgram import Subgram

subgram = Subgram(SUBGRAM_TOKEN)

Check if a user has paid

Get your PRODUCT_ID: https://t.me/subgram_merchant_bot

async def show_paid_functionallity(update, context):
    if await subgram.has_access(
        user_id=update.effective_user.id,
        product_id=SUBGRAM_PRODUCT_ID,
    ):
        await update.effective_user.send_message("You paid!")

Create a checkout page

If you want to send to your user a checkout page link or if your user wants to manage a subscription (same link):

async def manage_subscription(update, context):
    checkout_page = await subgram.create_checkout_page(
        product_id=SUBGRAM_PRODUCT_ID,
        user_id=update.effective_user.id,
        name=update.effective_user.name,  # for invoices
        language_code=update.effective_user.language_code,  # for localization
    )

    await update.effective_user.send_message(
        "You can manage your subscription by clicking this button:",
        reply_markup=InlineKeyboardMarkup([[
            InlineKeyboardButton("Manage Subscription", 
            web_app=WebAppInfo(url=checkout_page.checkout_url))
        ]]),
    )

Listen to payment events

Instead of forcing your developers to rent a server, purchase a domain name and setting up SSL certificates for classic webhook updates, we replicated a long polling approach which is widely used among Telegram bot developers.

# define a function which will listen 
# to Subgram updates in the background
async def post_init(application: Application) -> None:
    asyncio.create_task(handle_subgram_events())

# best place to run it -- while building the Application:
def main() -> None:
    application = (
        Application
        .builder()
        .post_init(post_init)
        .token(settings.TELEGRAM_TOKEN)
        .build()
    )
    ...

In handle_subgram_events you should write your own handlers for each received event type. The function can look like this:

async def handle_subgram_events():
    bot = Bot(TELEGRAM_TOKEN)  # bot instance to send messages

    # Async Generator 
    async for event in subgram.event_listener():
        if event.type == EventType.SUBSCRIPTION_STARTED:
            await bot.send_message(
                chat_id=event.object.customer.telegram_id,
                text=f"Thank you for subscribing! You have access until: {event.object.status.ending_at}.",
            )

All event types are defined in src/subgram/constants.py:

# from subgram.constants import EventType

class EventType(str, Enum):
    SUBSCRIPTION_STARTED = "subscription.started"
    SUBSCRIPTION_RENEWED = "subscription.renewed"
    SUBSCRIPTION_RENEW_FAILED = "subscription.renew_failed"
    SUBSCRIPTION_CANCELLED = "subscription.cancelled"
    SUBSCRIPTION_UPGRADED = "subscription.upgraded"

Release files for subgram 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for subgram 0.0.4
File Size Uploaded
subgram-0.0.4.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for subgram 0.0.4
File Interpreter ABI Platform
subgram-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 12.0 kB

Release files / subgram-0.0.4.tar.gz

Download URL subgram-0.0.4.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8811bfca5713ce05ff7d9803c8b8b26899077e6d1c6a5587e677980286e9a059
BLAKE2b-256 checksum
How to use checksums
48ea54a91f62917e2edb23a28eb7976ee3810a0a4786fff8d756f93fa46c2c25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.13

Release files / subgram-0.0.4-py3-none-any.whl

Download URL subgram-0.0.4-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e53c9cbb25fbfd9e84c5f4b15a35f0004e582867be983f6260f902344c8e5624
BLAKE2b-256 checksum
How to use checksums
7eec97752c2687999f8b9f215788c8ff3d514b1199077ec8b6dfcd6e6023e0a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.13

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page