Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration with AWS SNS #140

Open
leetcoder314 opened this issue May 7, 2024 · 1 comment
Open

Integration with AWS SNS #140

leetcoder314 opened this issue May 7, 2024 · 1 comment

Comments

@leetcoder314
Copy link

Is there a way to integrate this with AWS instead of Twilio? Willl love to contribute

@GrbavaCigla
Copy link

You could just replace callback function. Here is an example:

def send_sms_with_callback_token(user, mobile_token, **kwargs):
    """
    Sends a SMS to user.mobile via Amazon SNS.

    Passes silently without sending in test environment.
    """
    if (
        settings.PASSWORDLESS_AUTH.get(
            "PASSWORDLESS_TEST_SUPPRESSION",
            passwordless_settings.PASSWORDLESS_TEST_SUPPRESSION,
        )
        is True
    ):
        return True

    base_string = kwargs.get(
        "mobile_message",
        settings.PASSWORDLESS_AUTH.get(
            "PASSWORDLESS_MOBILE_MESSAGE",
            passwordless_settings.PASSWORDLESS_MOBILE_MESSAGE,
        ),
    )

    try:
        to_number = getattr(
            user,
            settings.PASSWORDLESS_AUTH.get(
                "PASSWORDLESS_USER_MOBILE_FIELD_NAME",
                passwordless_settings.PASSWORDLESS_USER_MOBILE_FIELD_NAME,
            ),
        )
        if to_number.__class__.__name__ == "PhoneNumber":
            to_number = to_number.__str__()

        client = boto3.client(
            "sns",
            region_name=settings.AWS_SNS_REGION,
            aws_access_key_id=settings.AWS_SNS_ACCESS_KEY_ID,
            aws_secret_access_key=settings.AWS_SNS_SECRET_ACCESS_KEY,
        )

        client.publish(
            Message=base_string % mobile_token.key,
            PhoneNumber=to_number,
        )
        return True
    except KeyError:
        logger.debug("Couldn't send SMS. Did you set your AWS tokens?")
        return False
    except Exception as e:
        number = getattr(
            user,
            settings.PASSWORDLESS_AUTH.get(
                "PASSWORDLESS_USER_MOBILE_FIELD_NAME",
                passwordless_settings.PASSWORDLESS_USER_MOBILE_FIELD_NAME,
            ),
        )

        logger.debug(
            f"Failed to send token SMS to user: {user.id} with number {number}.\n{e}"
        )
        return False

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants