API reference¶
Core public surface for building Signal bots. See Getting started for setup and Advanced usage for patterns.
Quick usage¶
from signal_client import SignalClient, command
@command("!ping")
async def ping(ctx):
await ctx.reply_text("pong")
client = SignalClient()
client.register(ping)
await client.start()
sequenceDiagram
participant Bot
participant API as signal-cli-rest-api
Bot->>API: subscribe websocket
API-->>Bot: message event
Bot-->>Bot: handler via @command
Bot->>API: REST reply Runtime¶
The main class for interacting with the Signal API and processing messages.
This class orchestrates the various components of the signal-client, including API clients, message listeners, and worker pools.
Source code in signal_client/app/bot.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
queue property ¶
Get the message queue.
Raises:
| Type | Description |
|---|---|
RuntimeError | If the application is not initialized. |
Returns:
| Type | Description |
|---|---|
Queue[QueuedMessage] | The asyncio.Queue for messages. |
worker_pool property ¶
Get the worker pool.
Raises:
| Type | Description |
|---|---|
RuntimeError | If the application is not initialized. |
Returns:
| Type | Description |
|---|---|
WorkerPool | The WorkerPool instance. |
api_clients property ¶
Get the API clients.
Raises:
| Type | Description |
|---|---|
RuntimeError | If the application is not initialized. |
Returns:
| Type | Description |
|---|---|
APIClients | The APIClients instance. |
websocket_client property ¶
Get the WebSocket client.
Raises:
| Type | Description |
|---|---|
RuntimeError | If the application is not initialized. |
Returns:
| Type | Description |
|---|---|
WebSocketClient | The WebSocketClient instance. |
message_service property ¶
Get the message service.
Raises:
| Type | Description |
|---|---|
RuntimeError | If the application is not initialized. |
Returns:
| Type | Description |
|---|---|
MessageService | The MessageService instance. |
__init__ ¶
__init__(
config: dict | None = None,
app: Application | None = None,
header_provider: HeaderProvider | None = None,
) -> None
Initialize the SignalClient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config | dict | None | A dictionary of configuration overrides. | None |
app | Application | None | An optional pre-initialized Application instance. | None |
header_provider | HeaderProvider | None | An optional callable or object that provides additional HTTP headers for API requests. | None |
Source code in signal_client/app/bot.py
register ¶
Register a new command with the client.
Registered commands will be executed when matching incoming messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command | Command | The Command instance to register. | required |
Source code in signal_client/app/bot.py
use ¶
use(
middleware: Callable[
[Context, Callable[[Context], Awaitable[None]]],
Awaitable[None],
],
) -> None
Register middleware to wrap command execution.
Middleware functions are called before command execution and can modify the context or prevent command execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
middleware | Callable[[Context, Callable[[Context], Awaitable[None]]], Awaitable[None]] | The middleware callable to register. | required |
Source code in signal_client/app/bot.py
__aenter__ async ¶
__aexit__ async ¶
__aexit__(
exc_type: type[BaseException] | None,
exc: BaseException | None,
tb: TracebackType | None,
) -> None
Asynchronous context manager exit.
Ensures proper shutdown of the client.
Source code in signal_client/app/bot.py
start async ¶
Start the SignalClient, including message listening and worker processing.
This method will block indefinitely until the client is shut down.
Source code in signal_client/app/bot.py
shutdown async ¶
Shut down the SignalClient gracefully.
This involves closing the websocket, waiting for queues to empty, stopping workers, and closing the aiohttp session.
Source code in signal_client/app/bot.py
set_websocket_client async ¶
Override the websocket client (primarily for testing purposes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
websocket_client | WebSocketClient | The new WebSocketClient instance to use. | required |
Source code in signal_client/app/bot.py
Explicit wiring of Signal client runtime components.
This class is responsible for initializing and managing the lifecycle of all components within the Signal client, including API clients, storage, message queues, and worker pools.
Source code in signal_client/app/application.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
__init__ ¶
Initialize the Application instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings | Settings | The application settings. | required |
header_provider | HeaderProvider | None | An optional callable or object that provides additional HTTP headers for API requests. | None |
Source code in signal_client/app/application.py
initialize async ¶
Initialize all components of the application.
This method sets up the AIOHTTP client session, API clients, message queues, storage, and worker pools. It must be called before the application can start processing messages.
Source code in signal_client/app/application.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
shutdown async ¶
Shut down the application gracefully.
Source code in signal_client/app/application.py
Commands and context¶
signal_client.core.command.command ¶
command(
*triggers: str | Pattern,
whitelisted: Sequence[str] | None = None,
case_sensitive: bool = False,
name: str | None = None,
description: str | None = None,
usage: str | None = None
) -> Callable[
[Callable[[Context], Awaitable[None]]], Command
]
Define a new command via decorator.
This decorator simplifies the creation of Command instances by allowing you to define triggers, whitelisted senders, and other metadata directly on the handler function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*triggers | str | Pattern | One or more strings or regular expressions that will trigger this command. | () |
whitelisted | Sequence[str] | None | An optional list of sender IDs that are allowed to execute this command. | None |
case_sensitive | bool | If True, string triggers will be matched case-sensitively. | False |
name | str | None | An optional name for the command. | None |
description | str | None | An optional description for the command. | None |
usage | str | None | Optional usage instructions for the command. | None |
Returns:
| Type | Description |
|---|---|
Callable[[Callable[[Context], Awaitable[None]]], Command] | A decorator that transforms an asynchronous function into a Command object. |
Raises:
| Type | Description |
|---|---|
ValueError | If no triggers are provided. |
Source code in signal_client/core/command.py
Provide helpers for command handlers interacting with the Signal API.
Instances of this class are passed to command handler functions, encapsulating the incoming message and all necessary API clients and utilities.
Source code in signal_client/core/context.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 | |
reply_text async ¶
reply_text(
text: str,
*,
recipients: Sequence[str] | None = None,
mentions: list[MessageMention] | None = None,
quote_mentions: list[MessageMention] | None = None,
base64_attachments: list[str] | None = None,
link_preview: LinkPreview | None = None,
text_mode: Literal["normal", "styled"] | None = None,
notify_self: bool | None = None,
edit_timestamp: int | None = None,
sticker: str | None = None,
view_once: bool = False
) -> SendMessageResponse | None
Reply to the incoming message with plain text, quoting it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text | str | The text content of the reply message. | required |
recipients | Sequence[str] | None | Optional list of recipient IDs (phone numbers or group IDs). Defaults to the sender of the incoming message. | None |
mentions | list[MessageMention] | None | Optional list of MessageMention objects for @mentions. | None |
quote_mentions | list[MessageMention] | None | Optional list of MessageMention objects for mentions within a quote. | None |
base64_attachments | list[str] | None | Optional list of base64 encoded attachments. | None |
link_preview | LinkPreview | None | Optional LinkPreview object for a URL preview. | None |
text_mode | Literal['normal', 'styled'] | None | 'normal' for plain text, 'styled' for markdown. | None |
notify_self | bool | None | Whether to send a notification to self. | None |
edit_timestamp | int | None | Timestamp of the message to edit. | None |
sticker | str | None | ID of a sticker to send. | None |
view_once | bool | If True, the message/attachments can only be viewed once. | False |
Returns:
| Type | Description |
|---|---|
SendMessageResponse | None | A SendMessageResponse object if successful, otherwise None. |
Source code in signal_client/core/context.py
send_markdown async ¶
send_markdown(
text: str,
*,
recipients: Sequence[str] | None = None,
mentions: list[MessageMention] | None = None
) -> SendMessageResponse | None
Send a message with markdown formatting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text | str | The markdown formatted text content of the message. | required |
recipients | Sequence[str] | None | Optional list of recipient IDs. | None |
mentions | list[MessageMention] | None | Optional list of MessageMention objects for @mentions. | None |
Returns:
| Type | Description |
|---|---|
SendMessageResponse | None | A SendMessageResponse object if successful, otherwise None. |
Source code in signal_client/core/context.py
send_with_preview async ¶
send_with_preview(
url: str,
*,
message: str | None = None,
title: str | None = None,
description: str | None = None,
recipients: Sequence[str] | None = None,
text_mode: Literal["normal", "styled"] | None = None
) -> SendMessageResponse | None
Send a message with a link preview.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url | str | The URL for which to generate a preview. | required |
message | str | None | Optional text message to accompany the link preview. | None |
title | str | None | Optional title for the link preview. | None |
description | str | None | Optional description for the link preview. | None |
recipients | Sequence[str] | None | Optional list of recipient IDs. | None |
text_mode | Literal['normal', 'styled'] | None | 'normal' for plain text, 'styled' for markdown. | None |
Returns:
| Type | Description |
|---|---|
SendMessageResponse | None | A SendMessageResponse object if successful, otherwise None. |
Source code in signal_client/core/context.py
react async ¶
Add a reaction (emoji) to the incoming message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
emoji | str | The emoji string to react with (e.g., "👍"). | required |
Source code in signal_client/core/context.py
send_receipt async ¶
send_receipt(
target_timestamp: int,
*,
receipt_type: Literal["read", "viewed"] = "read",
recipient: str | None = None
) -> None
Send a read or viewed receipt for a message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_timestamp | int | The timestamp of the message for which to send the receipt. | required |
receipt_type | Literal['read', 'viewed'] | The type of receipt to send ('read' or 'viewed'). Defaults to 'read'. | 'read' |
recipient | str | None | Optional recipient ID. Defaults to the sender of the incoming message. | None |
Source code in signal_client/core/context.py
download_attachments async ¶
download_attachments(
attachments: Sequence[AttachmentPointer] | None = None,
*,
max_total_bytes: int = DEFAULT_MAX_TOTAL_BYTES,
dest_dir: str | Path | None = None
) -> AsyncGenerator[list[Path], None]
Download attachments associated with the current message or a provided list.
This is an asynchronous context manager that yields a list of Path objects pointing to the downloaded files. The files are cleaned up automatically upon exiting the context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attachments | Sequence[AttachmentPointer] | None | An optional sequence of | None |
max_total_bytes | int | The maximum total size (in bytes) of attachments to download. Defaults to | DEFAULT_MAX_TOTAL_BYTES |
dest_dir | str | Path | None | Optional directory to save the attachments. If not provided, a temporary directory is used. | None |
Yields:
| Type | Description |
|---|---|
AsyncGenerator[list[Path], None] | A list of |
Source code in signal_client/core/context.py
lock async ¶
Acquire a distributed lock for a specific resource.
This is an asynchronous context manager that ensures exclusive access to a resource across multiple instances of the application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_id | str | A unique identifier for the resource to lock. | required |
Yields:
| Type | Description |
|---|---|
AsyncGenerator[None, None] | None, while the lock is held. |
Source code in signal_client/core/context.py
show_typing async ¶
Send a typing indicator to the sender of the incoming message.
Source code in signal_client/core/context.py
hide_typing async ¶
Hide the typing indicator from the sender of the incoming message.
Source code in signal_client/core/context.py
Represents a single command that the bot can respond to.
A command is defined by its triggers (patterns that match incoming messages), an optional whitelist of allowed senders, and a handler function that executes the command's logic.
Source code in signal_client/core/command.py
__init__ ¶
__init__(
triggers: list[str | Pattern],
whitelisted: list[str] | None = None,
*,
case_sensitive: bool = False,
metadata: CommandMetadata | None = None
) -> None
Initialize a Command instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triggers | list[str | Pattern] | A list of strings or regular expressions that will trigger this command. | required |
whitelisted | list[str] | None | An optional list of sender IDs (phone numbers or group IDs) that are allowed to execute this command. If empty or None, all senders are allowed. | None |
case_sensitive | bool | If True, string triggers will be matched case-sensitively. Defaults to False. | False |
metadata | CommandMetadata | None | Optional CommandMetadata to provide name, description, and usage. | None |
Source code in signal_client/core/command.py
with_handler ¶
Assign a handler function to this command.
If name or description are not already set, they will be inferred from the handler function's name and docstring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handler | Callable[[Context], Awaitable[None]] | The asynchronous function that will be executed when this command is triggered. It must accept a Context object. | required |
Returns:
| Type | Description |
|---|---|
Command | The Command instance with the handler assigned. |
Source code in signal_client/core/command.py
__call__ async ¶
Execute the command's handler function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context | Context | The Context object containing message details and API clients. | required |
Raises:
| Type | Description |
|---|---|
CommandError | If no handler has been assigned to the command. |
Source code in signal_client/core/command.py
Settings¶
Bases: BaseSettings
Single, explicit configuration surface for the Signal client.
Settings are loaded from environment variables and an optional .env file. All settings can be overridden via constructor arguments.
Source code in signal_client/core/config.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |
validate_storage ¶
Validate storage-related settings based on the chosen storage_type.
Ensures that required fields for 'redis' and 'sqlite' storage are provided and that numeric fields have valid positive values.
Raises:
| Type | Description |
|---|---|
ValueError | If storage configuration is invalid. |
Returns:
| Type | Description |
|---|---|
Self | The validated Settings instance. |
Source code in signal_client/core/config.py
from_sources classmethod ¶
Load settings from environment variables and an optional dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config | dict[str, Any] | None | An optional dictionary to override environment-loaded settings. | None |
Raises:
| Type | Description |
|---|---|
ConfigurationError | If any required settings are missing or invalid. |
Returns:
| Type | Description |
|---|---|
Self | A validated Settings instance. |
Source code in signal_client/core/config.py
Observability¶
signal_client.observability.health_server.start_health_server async ¶
start_health_server(
application: Application,
*,
host: str = "127.0.0.1",
port: int = 8081
) -> HealthServer
Create and start a HealthServer for the given application.
Source code in signal_client/observability/health_server.py
signal_client.observability.metrics_server.start_metrics_server ¶
start_metrics_server(
port: int = 8000,
addr: str = "127.0.0.1",
*,
registry: CollectorRegistry | None = None
) -> object
Start an HTTP server that exposes Prometheus metrics at /.
Returns the server object so callers can stop it if desired.
Source code in signal_client/observability/metrics.py
signal_client.observability.logging.ensure_structlog_configured ¶
Idempotently configure structlog unless already configured externally.
Source code in signal_client/observability/logging.py
Troubleshooting¶
- Missing members in the reference? Run
poetry run mkdocs build --strictto surface mkdocstrings import errors (often missing deps). - Import path errors: prefer
signal_client.<module>paths shown above; avoid private modules not exported in__all__.
Next steps¶
- Use the reference alongside Examples to map API calls to runnable scripts.
- See Advanced usage for middleware and resiliency tuning using these APIs.
Exceptions¶
signal_client.core.exceptions ¶
Custom exceptions for the Signal client.
AuthenticationError ¶
Bases: SignalAPIError
Raised for authentication failures (HTTP status code 401 Unauthorized).
Source code in signal_client/core/exceptions.py
__init__ ¶
ConfigurationError ¶
GroupNotFoundError ¶
Bases: SignalAPIError
Raised when a requested group is not found (HTTP status 404.
For group-related operations).
Source code in signal_client/core/exceptions.py
__init__ ¶
InvalidRecipientError ¶
Bases: SignalAPIError
Raised when a message cannot be sent due to an invalid recipient.
Applies to HTTP status 404 on send operations.
Source code in signal_client/core/exceptions.py
__init__ ¶
RateLimitError ¶
Bases: SignalAPIError
Raised when the API rate limit is exceeded (HTTP status codes 413 or 429).
Source code in signal_client/core/exceptions.py
__init__ ¶
ServerError ¶
Bases: SignalAPIError
Raised for server-side errors (HTTP status codes 5xx).
Source code in signal_client/core/exceptions.py
__init__ ¶
SignalAPIError ¶
Bases: Exception
Base exception for all API-related errors.
This exception is raised for general API errors that do not fall into more specific categories (e.g., unexpected status codes).
Source code in signal_client/core/exceptions.py
__init__ ¶
Store an API error message and optional status code.