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"
)
await message.answer(result.message, reply_markup=result.keyboard)

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)

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)
    • 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.
    • 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.
  • 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) · 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.1.1.tar.gz (10.8 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.1.1-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for nativeads-0.1.1.tar.gz
Algorithm Hash digest
SHA256 485ee8a0e633eb0b978ad82fbf8c0add435f739c6c776dead5137d517cb663d9
MD5 041af455dc39718e342d058872ec54af
BLAKE2b-256 40c65135afe0b56cf631e02497991c95a38cac318c188b00bca36572b2f7fa66

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nativeads-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fc1a08ce9cd7743bb2cdbabde3217997a45939421589c774a0edee9f2b498efc
MD5 776aca3046040535b40e284779a21ed3
BLAKE2b-256 8338115988191ba74802eb331f3d764efcff3fcbaf553a4a02b8c64b6df4afd9

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