subgram


Namesubgram JSON
Version 0.0.4 PyPI version JSON
download
home_page
SummaryPython SDK for subgram.io
upload_time2024-03-07 11:24:01
maintainer
docs_urlNone
author
requires_python>=3.8
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

``` python
from subgram import Subgram

subgram = Subgram(SUBGRAM_TOKEN)
```


## Check if a user has paid

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

``` python
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):


``` python
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.


``` python
# 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:

``` python
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:

``` python
# 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"

```
            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "subgram",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "",
    "author": "",
    "author_email": "Karim Iskakov <kar.iskakov@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/48/ea/54a91f62917e2edb23a28eb7976ee3810a0a4786fff8d756f93fa46c2c25/subgram-0.0.4.tar.gz",
    "platform": null,
    "description": "# Subgram python sdk\n\nThis SDK will help you to integrate subscriptions into your Telegram bot.\n\n\n## Init the module.\n\nGet your `SUBGRAM_TOKEN`: https://t.me/subgram_merchant_bot\n\n``` python\nfrom subgram import Subgram\n\nsubgram = Subgram(SUBGRAM_TOKEN)\n```\n\n\n## Check if a user has paid\n\nGet your `PRODUCT_ID`: https://t.me/subgram_merchant_bot\n\n``` python\nasync def show_paid_functionallity(update, context):\n    if await subgram.has_access(\n        user_id=update.effective_user.id,\n        product_id=SUBGRAM_PRODUCT_ID,\n    ):\n        await update.effective_user.send_message(\"You paid!\")\n```\n\n\n## Create a checkout page\n\nIf you want to send to your user a checkout page link or if your user wants to manage a subscription (same link):\n\n\n``` python\nasync def manage_subscription(update, context):\n    checkout_page = await subgram.create_checkout_page(\n        product_id=SUBGRAM_PRODUCT_ID,\n        user_id=update.effective_user.id,\n        name=update.effective_user.name,  # for invoices\n        language_code=update.effective_user.language_code,  # for localization\n    )\n\n    await update.effective_user.send_message(\n        \"You can manage your subscription by clicking this button:\",\n        reply_markup=InlineKeyboardMarkup([[\n            InlineKeyboardButton(\"Manage Subscription\", \n            web_app=WebAppInfo(url=checkout_page.checkout_url))\n        ]]),\n    )\n```\n\n## Listen to payment events\n\nInstead 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.\n\n\n``` python\n# define a function which will listen \n# to Subgram updates in the background\nasync def post_init(application: Application) -> None:\n    asyncio.create_task(handle_subgram_events())\n\n# best place to run it -- while building the Application:\ndef main() -> None:\n    application = (\n        Application\n        .builder()\n        .post_init(post_init)\n        .token(settings.TELEGRAM_TOKEN)\n        .build()\n    )\n    ...\n```\n\nIn `handle_subgram_events` you should write your own handlers for each received event type. The function can look like this:\n\n``` python\nasync def handle_subgram_events():\n    bot = Bot(TELEGRAM_TOKEN)  # bot instance to send messages\n\n    # Async Generator \n    async for event in subgram.event_listener():\n        if event.type == EventType.SUBSCRIPTION_STARTED:\n            await bot.send_message(\n                chat_id=event.object.customer.telegram_id,\n                text=f\"Thank you for subscribing! You have access until: {event.object.status.ending_at}.\",\n            )\n```\n\nAll event types are defined in src/subgram/constants.py:\n\n``` python\n# from subgram.constants import EventType\n\nclass EventType(str, Enum):\n    SUBSCRIPTION_STARTED = \"subscription.started\"\n    SUBSCRIPTION_RENEWED = \"subscription.renewed\"\n    SUBSCRIPTION_RENEW_FAILED = \"subscription.renew_failed\"\n    SUBSCRIPTION_CANCELLED = \"subscription.cancelled\"\n    SUBSCRIPTION_UPGRADED = \"subscription.upgraded\"\n\n```",
    "bugtrack_url": null,
    "license": "",
    "summary": "Python SDK for subgram.io",
    "version": "0.0.4",
    "project_urls": {
        "Homepage": "https://github.com/subgram/subgram-python-sdk",
        "Issues": "https://github.com/subgram/subgram-python-sdk/issues"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7eec97752c2687999f8b9f215788c8ff3d514b1199077ec8b6dfcd6e6023e0a0",
                "md5": "89a584acc7159e1be1e974a6f1292b20",
                "sha256": "e53c9cbb25fbfd9e84c5f4b15a35f0004e582867be983f6260f902344c8e5624"
            },
            "downloads": -1,
            "filename": "subgram-0.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "89a584acc7159e1be1e974a6f1292b20",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 5263,
            "upload_time": "2024-03-07T11:23:59",
            "upload_time_iso_8601": "2024-03-07T11:23:59.734397Z",
            "url": "https://files.pythonhosted.org/packages/7e/ec/97752c2687999f8b9f215788c8ff3d514b1199077ec8b6dfcd6e6023e0a0/subgram-0.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "48ea54a91f62917e2edb23a28eb7976ee3810a0a4786fff8d756f93fa46c2c25",
                "md5": "e2010594296bfe85d5d82c34039add13",
                "sha256": "8811bfca5713ce05ff7d9803c8b8b26899077e6d1c6a5587e677980286e9a059"
            },
            "downloads": -1,
            "filename": "subgram-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "e2010594296bfe85d5d82c34039add13",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 6698,
            "upload_time": "2024-03-07T11:24:01",
            "upload_time_iso_8601": "2024-03-07T11:24:01.510347Z",
            "url": "https://files.pythonhosted.org/packages/48/ea/54a91f62917e2edb23a28eb7976ee3810a0a4786fff8d756f93fa46c2c25/subgram-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-07 11:24:01",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "subgram",
    "github_project": "subgram-python-sdk",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "subgram"
}
        
Elapsed time: 0.52894s