Skip to main content

Python SDK for the Sidekick ad-injection API

Project description

sidekick-ads

Python SDK for the Sidekick ad-injection API.

Installation

# Core (httpx only)
pip install sidekick-ads

# With aiogram support
pip install sidekick-ads[aiogram]

# With python-telegram-bot support
pip install sidekick-ads[ptb]

Quick start

aiogram 3.x with middleware

from aiogram import Bot, Dispatcher, Router
from aiogram.types import Message
from sidekick_ads import Sidekick
from sidekick_ads.middleware import SidekickMiddleware

bot = Bot(token="BOT_TOKEN")
dp = Dispatcher()
router = Router()
dp.include_router(router)

# Register the middleware — it injects a Sidekick instance into every handler
dp.message.middleware(
    SidekickMiddleware(api_key="sk_xxx", platform_id="plt_xxx")
)


@router.message()
async def handle(message: Message, sidekick: Sidekick):
    llm_response = "Here is your answer..."

    user = message.from_user
    user_id = user.id if user else 0
    language_code = user.language_code if user else None
    is_premium = user.is_premium if user else None

    result = await sidekick.inject(
        user_id=user_id,
        message=llm_response,
        language_code=language_code,
        is_premium=is_premium,
        keyboard=None,  # or an existing InlineKeyboardMarkup
    )

    await message.answer(
        result.message,
        reply_markup=result.keyboard,
    )

aiogram 3.x without middleware

from aiogram import Bot, Dispatcher, Router
from aiogram.types import Message
from sidekick_ads import Sidekick

bot = Bot(token="BOT_TOKEN")
dp = Dispatcher()
router = Router()
dp.include_router(router)

sidekick = Sidekick(api_key="sk_xxx", platform_id="plt_xxx")


@router.message()
async def handle(message: Message):
    llm_response = "Here is your answer..."

    user = message.from_user
    user_id = user.id if user else 0
    language_code = user.language_code if user else None
    is_premium = user.is_premium if user else None

    result = await sidekick.inject(
        user_id=user_id,
        message=llm_response,
        language_code=language_code,
        is_premium=is_premium,
        keyboard=None,  # or an existing InlineKeyboardMarkup
    )

    await message.answer(
        result.message,
        reply_markup=result.keyboard,
    )

python-telegram-bot v20+

from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
from sidekick_ads import Sidekick

sidekick = Sidekick(api_key="sk_xxx", platform_id="plt_xxx")


async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    llm_response = "Here is your answer..."

    user = update.effective_user
    user_id = user.id if user else 0
    language_code = user.language_code if user else None
    is_premium = user.is_premium if user else None

    # Example: existing keyboard with one button
    existing_kb = InlineKeyboardMarkup(
        [[InlineKeyboardButton(text="My button", url="https://example.com")]]
    )

    result = await sidekick.inject(
        user_id=user_id,
        message=llm_response,
        language_code=language_code,
        is_premium=is_premium,
        keyboard=existing_kb,
    )

    await update.message.reply_text(
        result.message,
        reply_markup=result.keyboard,
    )


app = ApplicationBuilder().token("BOT_TOKEN").build()
app.add_handler(CommandHandler("start", start))
app.run_polling()

Raw usage (no framework)

import asyncio
from sidekick_ads import Sidekick


async def main():
    sidekick = Sidekick(api_key="sk_xxx", platform_id="plt_xxx")

    result = await sidekick.inject(
        user_id=123456789,
        message="Here is your answer...",
        language_code="en",
        is_premium=False,
    )

    print(result.message)
    print(result.has_ad)
    print(result.impression_id)
    print(result.ad)
    print(result.keyboard)

    await sidekick.close()


asyncio.run(main())

fetch() — privacy-friendly alternative

If your LLM reply contains user data (names, query fragments) that you'd prefer not to send to Sidekick, use fetch() instead of inject(). It returns the ad as separate fields, and you render them into your reply yourself.

import asyncio
from sidekick_ads import Sidekick


async def main():
    sidekick = Sidekick(api_key="sk_live_xxx", platform_id="plt_xxx")

    result = await sidekick.fetch(
        user_id=123456789,
        language_code="en",
        parse_mode="HTML",  # optional — adds `ad_text_formatted`
    )

    llm_reply = "Here is your answer..."

    if result.has_ad and result.ad is not None:
        text = f"{llm_reply}\n\n{result.ad.ad_text_formatted}"
        keyboard = {"inline_keyboard": [[
            {"text": result.ad.button_text, "url": result.ad.button_url}
        ]]}
        # send `text` with `keyboard` via your bot framework
    else:
        # send `llm_reply` unchanged
        pass

    await sidekick.close()


asyncio.run(main())

await sidekick.fetch(...) -> FetchResult

Parameter Type Default
user_id int required
language_code str required
is_premium bool | None None
parse_mode str | None None

FetchResult and FetchAdData

@dataclass
class FetchAdData:
    ad_text: str
    ad_url: str
    button_text: str
    button_url: str
    ad_text_formatted: Optional[str] = None  # only when parse_mode is sent


@dataclass
class FetchResult:
    has_ad: bool
    impression_id: Optional[str]
    ad: Optional[FetchAdData]

Like inject(), fetch() never raises — on any error (timeout, network failure, 5xx) it returns FetchResult(has_ad=False, impression_id=None, ad=None).

See the /api/v1/ad/fetch HTTP endpoint for the full wire format.

API reference

Sidekick(api_key, platform_id, *, base_url, timeout, platform)

Parameter Type Default
api_key str required
platform_id str required
base_url str https://sidekick-ads.com
timeout float 1.0
platform str "telegram"

await sidekick.inject(...) -> InjectResult

Parameter Type Default
user_id int required
message str required
language_code str | None None
is_premium bool | None None
keyboard Any | None None
ad_button_position str "bottom"

InjectResult

Field Type
message str
has_ad bool
impression_id str | None
ad dict | None
keyboard Any | None

await sidekick.fetch(...) -> FetchResult

Parameter Type Default
user_id int required
language_code str required
is_premium bool | None None
parse_mode str | None None

FetchResult

Field Type
has_ad bool
impression_id str | None
ad FetchAdData | None

FetchAdData

Field Type
ad_text str
ad_url str
button_text str
button_url str
ad_text_formatted str | None

await sidekick.close()

Closes the underlying HTTP connection pool. The client is automatically recreated on the next inject() call if needed.

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

sidekick_ads-0.3.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

sidekick_ads-0.3.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sidekick_ads-0.3.0.tar.gz
Algorithm Hash digest
SHA256 ed06e1147d17d0e4ef267f45aae6aeb113c5e71bb7118be62e7df095ff3f7181
MD5 8d88c9f58ea1213664ccda527c3aa799
BLAKE2b-256 890c639b7b73526a3b87b836c5f1b78e512a0a1e78f31211c450d32dcba4234b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sidekick_ads-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d2f3219515e6f67eae7ef5ab29d079bef4b004a65726f70a3e6e2fcce48c3ae1
MD5 1a1ad8af9b2e18ae825653adfe889e0c
BLAKE2b-256 2da04ecbe39a4e50d13d8c04667230c0363c99980942fac8d6bc82ee2eae5b08

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