Python SDK for subgram.io
Project description
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"
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file subgram-0.0.4.tar.gz.
File metadata
- Download URL: subgram-0.0.4.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8811bfca5713ce05ff7d9803c8b8b26899077e6d1c6a5587e677980286e9a059
|
|
| MD5 |
e2010594296bfe85d5d82c34039add13
|
|
| BLAKE2b-256 |
48ea54a91f62917e2edb23a28eb7976ee3810a0a4786fff8d756f93fa46c2c25
|
File details
Details for the file subgram-0.0.4-py3-none-any.whl.
File metadata
- Download URL: subgram-0.0.4-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e53c9cbb25fbfd9e84c5f4b15a35f0004e582867be983f6260f902344c8e5624
|
|
| MD5 |
89a584acc7159e1be1e974a6f1292b20
|
|
| BLAKE2b-256 |
7eec97752c2687999f8b9f215788c8ff3d514b1199077ec8b6dfcd6e6023e0a0
|