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.0.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.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nativeads-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 a24fa02178e675f1fa243938ddb48d08dbeedd76543661497fe30780399a509c
MD5 5cbc7cb02ace11fb33e82179dab015b7
BLAKE2b-256 f5e06d72e4cf4303d4689f838427f3b95149dee0771a94b15b1496e222f95c7d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nativeads-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 856c991ce030379c7225b773a94e0b019c368c4c3b0dda1d2c8a58423d269515
MD5 dcb1dc96cc071c5bec09d94674a84281
BLAKE2b-256 86ef1ce9dcc5669ef70c11cdae547b3128324ff644ffe186219b574262b8f328

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