Notifications

class terminusgps.aws.notifications.AsyncNotificationManager(group_id: str | None = None, region_name: str = 'us-east-1')[source]

Public Data Attributes:

region_name

An AWS region name.

group_id

A message group id.

Public Methods:

__init__([group_id, region_name])

Sets group_id and region_name if provided.

__aenter__()

Creates an asyncronous session and client.

__aexit__(exc_type, exc_val, exc_tb)

Destroys the asyncronous session.

send_push_batch(target_arns, message)

Sends a push notification to each AWS resource by ARN in target_arns.

send_sms_batch(to_numbers, message)

Sends an sms message to each phone number in to_numbers.

send_sms(to_number, message, message_id)

Sends an sms to to_number.

send_push(target_arn, message, message_id)

Sends a push notification to target_arn.


async __aenter__() AsyncNotificationManager[source]

Creates an asyncronous session and client.

Returns:

The notification manager.

Return type:

AsyncNotificationManager

async __aexit__(exc_type, exc_val, exc_tb) None[source]

Destroys the asyncronous session.

Parameters:
  • exc_type – Exception type.

  • exc_val – Exception value.

  • exc_tb – Exception traceback.

Returns:

Nothing.

Return type:

None

__init__(group_id: str | None = None, region_name: str = 'us-east-1') None[source]

Sets group_id and region_name if provided.

Returns:

Nothing.

Return type:

None

__weakref__

list of weak references to the object

property group_id: str

A message group id.

Type:

str

property region_name: str

An AWS region name.

Type:

str

async send_push(target_arn: str, message: str, message_id: str) None[source]

Sends a push notification to target_arn.

Parameters:
  • target_arn (str) – An AWS resource ARN.

  • message (str) – A message body.

  • message_id (str) – A message deduplication id.

Returns:

Nothing.

Return type:

None

async send_push_batch(target_arns: Sequence[str], message: str) None[source]

Sends a push notification to each AWS resource by ARN in target_arns.

Parameters:
  • target_arns (Sequence) – A sequence of AWS resource ARNs.

  • message (str) – A message body.

Returns:

Nothing.

Return type:

None

async send_sms(to_number: str, message: str, message_id: str) None[source]

Sends an sms to to_number.

Parameters:
  • to_number (str) – A destination phone number.

  • message (str) – A message body.

  • message_id (str) – A message deduplication id.

Raises:

ValidationError – If to_number wasn’t a valid E.164 formatted phone number.

Returns:

Nothing.

Return type:

None

async send_sms_batch(to_numbers: Sequence[str], message: str) None[source]

Sends an sms message to each phone number in to_numbers.

Parameters:
  • to_numbers (Sequence) – A sequence of phone numbers.

  • message (str) – A message body.

Raises:

ValidationError – If a to_number wasn’t a valid E.164 formatted phone number.

Returns:

Nothing.

Return type:

None

Usage

import asyncio
import uuid

from terminusgps.aws.notifications import AsyncNotificationManager

async def main() -> None:
    # Manager requires a group_id
    group_id: str = str(uuid.uuid4())
    message: str = "We know where ours are... do you?"
    async with AsyncNotificationManager(group_id) as manager:
        # Some methods require a message id
        message_id: str = str(uuid.uuid4())

        # Send an sms to one phone number
        # Phone numbers must be in E.164 format
        to_number: str = "+17135555555"
        await manager.send_sms(
            to_number=to_number, message=message, message_id=message_id
        )

        # Send an sms to multiple phone numbers (message ids are automatically generated)
        to_numbers: list[str] = ["+17135555555", "+12815555555", "+18325555555"]
        await manager.send_sms_batch(to_numbers=to_numbers, message=message)

        # Send a push notification to an AWS resource
        target_arn: str = "arn:aws:sns:us-east-1:555555555555:MyAWSResource"
        await manager.send_push(
            target_arn=target_arn, message=message, message_id=message_id
        )

if __name__ == "__main__":
    asyncio.run(main())