Skip to main content

Async webhook server for pararam.io bot events

Project description

pararamio-bot

Async webhook server for pararam.io bot events.

Installation

pip install pararamio-bot

Quick Start

import asyncio
import os

from pararamio_bot import WebhookMessage, WebhookServer


async def on_message(message: WebhookMessage) -> None:
    print(f"@{message.user_unique_name}: {message.text}")


async def main() -> None:
    async with WebhookServer(bot_secret=os.environ["BOT_SECRET"], port=8080) as server:
        server.on_message(on_message)
        await asyncio.Event().wait()


asyncio.run(main())

Configure your bot's webhook URL in pararam.io to http://<host>:8080/<url_key>, where <url_key> is the first 8 characters of your bot secret.

Security

Every incoming request is verified in two steps:

  1. URL key — the first 8 chars of the bot secret must be present in the request path. Requests with a wrong key get 404.
  2. X-Signature — HMAC-SHA256 signature of the request body, verified against the bot secret. Missing or invalid signature returns 401.

Both checks are always enabled and cannot be disabled.

API

WebhookServer

server = WebhookServer(
    bot_secret="...",  # Full bot secret from pararam.io
    host="0.0.0.0",    # Bind address (default)
    port=8080,          # Listen port (default)
)

Context manager:

async with WebhookServer(bot_secret="...") as server:
    server.on_message(handler)
    # server is running

Manual start/stop:

await server.start()
# ...
await server.stop()

Callbacks

Register async callbacks for incoming events:

server.on_message(callback)  # Called for each incoming message
server.on_action(callback)   # Called for bot:// link clicks

Multiple callbacks can be registered; they are called sequentially.

Models

WebhookMessage — incoming message from a user:

Field Type Description
user_id int Sender user ID
user_unique_name str Sender unique name
post_no int Post number in the chat
chat_id int Chat ID
text str Message text
organization_id int | None Organization ID (optional)
text_parsed list[dict] | None Parsed text structure
reply_no int | None Replied-to post number
reply_text str | None Replied-to post text
file_guid str | None Attached file GUID
file_name str | None Attached file name
attachments list[str] | None List of attachment identifiers

WebhookAction — user clicked a bot:// link:

Field Type Description
user_id int User who clicked
chat_id int Chat ID
post_no int Post number
organization_id int | None Organization ID (optional)
action str Action name from the link
params dict[str, str] Action parameters

Utility Functions

from pararamio_bot import extract_url_key, extract_api_key, make_signature, verify_signature

# Get the URL key (first 8 chars) from a bot secret
url_key = extract_url_key(bot_secret)

# Get the API key portion (chars 20+ for secrets > 50 chars)
api_key = extract_api_key(bot_secret)

# Compute / verify HMAC-SHA256 signatures
sig = make_signature(body, secret)
is_valid = verify_signature(body, sig, secret)

HTTP Endpoints

Route Description
POST /{url_key} Receive message webhooks
POST /{url_key}/act Receive action webhooks

Requirements

  • Python >= 3.11
  • aiohttp >= 3.9
  • pydantic >= 2.0

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

pararamio_bot-3.0.18.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

pararamio_bot-3.0.18-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file pararamio_bot-3.0.18.tar.gz.

File metadata

  • Download URL: pararamio_bot-3.0.18.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for pararamio_bot-3.0.18.tar.gz
Algorithm Hash digest
SHA256 e002861a555253e6f37e30f0f18dc303b26805451b0f2188cbfc17d5823d7251
MD5 e84c858ad0da426b998a56f221b40a55
BLAKE2b-256 2341a5cded7117dc40558ec20bf8129904c95ea03bd92848ffa456fdced5a406

See more details on using hashes here.

File details

Details for the file pararamio_bot-3.0.18-py3-none-any.whl.

File metadata

  • Download URL: pararamio_bot-3.0.18-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.11.14

File hashes

Hashes for pararamio_bot-3.0.18-py3-none-any.whl
Algorithm Hash digest
SHA256 b1c2ecb0be97ff7a492d39ae7e167f19d86078a904582c3a97594528c4ef38a2
MD5 319fc84f4087b53279ba1aad8b696805
BLAKE2b-256 452ebb3c9bba1e4e83382d9aa64184a344f7c25ab5070f494671e75550ecc384

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