Skip to main content

NativeAds SDK for Telegram bots — inject ads into LLM replies (aiogram / python-telegram-bot / raw).

Project description

nativeads (Python)

Python SDK for NativeAds — monetize your Telegram bot by injecting ads into LLM replies. Works with aiogram 3.x, python-telegram-bot v20+, or raw bot code.

pip install nativeads              # core (httpx only)
pip install nativeads[aiogram]     # + aiogram 3.x
pip install nativeads[ptb]         # + python-telegram-bot v20+

Quick start (inject)

from nativeads import NativeAds

ads = NativeAds(api_key="sk_live_xxx", platform_id="plt_xxx")

# inside an async message handler, after generating the LLM answer:
result = await ads.inject(
    user_id=message.from_user.id,
    message=llm_answer,
    language_code=message.from_user.language_code,
    is_premium=message.from_user.is_premium,
    keyboard=my_keyboard,        # optional — aiogram / PTB / raw dict
    # ad_button_position="top",  # default "bottom"
)
# link_preview_options hides the ad link's preview card (clean native footnote).
await message.answer(
    result.message,
    reply_markup=result.keyboard,
    link_preview_options=result.link_preview_options,
)

result.link_preview_options is {"is_disabled": True} by default, so the ad reads as a native footnote, not a banner. It's None when no ad was injected — always safe to pass. Want the rich preview card? Build the client with link_preview=True. On older aiogram/PTB without link_preview_options, use disable_web_page_preview=True instead.

aiogram middleware (DI)

from nativeads import NativeAds
from nativeads.middleware import NativeAdsMiddleware

mw = NativeAdsMiddleware(api_key="sk_live_xxx", platform_id="plt_xxx")
dp.message.middleware(mw)
dp.shutdown.register(mw.aclose)

async def handler(message: Message, nativeads: NativeAds):
    result = await nativeads.inject(user_id=message.from_user.id, message=answer)
    await message.answer(
        result.message,
        reply_markup=result.keyboard,
        link_preview_options=result.link_preview_options,
    )

Privacy-friendly mode (fetch)

res = await ads.fetch(user_id=uid, language_code="ru")
if res.has_ad:
    text = f"{llm_answer}\n\n{res.ad.ad_text}"
    # render res.ad.button_text / res.ad.button_url yourself

Guarantees

  • Never raises. On any error (timeout, network, 5xx) inject()/fetch() return your original message and keyboard unchanged. Default timeout 3s.
  • Your buttons are preserved — the ad button is a separate row (top/bottom), respecting Telegram's 4096-char and 13-row limits.
  • Click tracking is automaticbutton_url is a tracking redirect.

API

  • NativeAds(api_key, platform_id=None, *, base_url=..., timeout=3.0, platform="telegram", show_every=1, skip_first=0, link_preview=False)
    • show_every — ad on every Nth message (5 = optimal, 0 = off); N>1 keeps the first message ad-free.
    • skip_first — never show an ad on a user's first N messages.
    • link_preview — show Telegram's preview card for the ad link (False keeps it a clean footnote).
    • Frequency is client-side (in-memory per-user counter); skipped turns return your message without a server call.
    • NativeAdsMiddleware(...) accepts the same show_every / skip_first / link_preview.
  • await ads.inject(*, user_id, message, language_code=None, is_premium=None, keyboard=None, ad_button_position="bottom", parse_mode=None) -> InjectResult
  • await ads.fetch(*, user_id, language_code=None, is_premium=None, parse_mode=None) -> FetchResult
  • await ads.aclose()

InjectResult(message, has_ad, impression_id, ad, keyboard, link_preview_options) · FetchResult(has_ad, impression_id, ad) · Ad(ad_text, button_text, button_url, ad_text_formatted)

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

nativeads-0.3.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nativeads-0.3.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file nativeads-0.3.0.tar.gz.

File metadata

  • Download URL: nativeads-0.3.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for nativeads-0.3.0.tar.gz
Algorithm Hash digest
SHA256 cb66a2dd86d262fdc685f16f2a6e0f89a9f65768e008a0a6c835a9cf6d996622
MD5 d7e914e7ca70cd424ad6ac9656f80459
BLAKE2b-256 ea72a3a31a69253aec41b1b888f00430ee7ce39dc498b7541cfc06ed6d2cf2f9

See more details on using hashes here.

File details

Details for the file nativeads-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: nativeads-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for nativeads-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 24ffc6d03f15c68652e8e6dd8be39ba75b98159fc45edaa25cfbda0fcf612d72
MD5 554e8a2d273e1f644866877eed316d6f
BLAKE2b-256 0ac1ca50abab8d9728356366f68f330731a6cb60b47b620c0285571232ad0f4b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page