Skip to content

twilio

Push Notifications via Twilio

TwilioNotifications #

Bases: BaseNotifications

Push Notifications via Twilio

Source code in camply/notifications/twilio.py
class TwilioNotifications(BaseNotifications):
    """
    Push Notifications via Twilio
    """

    def __init__(self):
        super().__init__()
        try:
            from twilio.rest import Client
        except ImportError as ie:
            raise RuntimeError(
                "Looks like `twilio` isn't installed. Install it with `pip install camply[twilio]`"
            ) from ie

        if any(
            [
                TwilioConfig.ACCOUNT_SID is None,
                TwilioConfig.ACCOUNT_SID == "",
                TwilioConfig.AUTH_TOKEN is None,
                TwilioConfig.AUTH_TOKEN == "",
            ]
        ):
            warning_message = (
                "Twilio is not configured properly. To send Twilio messages "
                "make sure to run `camply configure` or set the "
                "proper environment variable: `TWILIO_ACCOUNT_SID`, `TWILIO_AUTH_TOKEN`."
            )
            logger.error(warning_message)
            raise EnvironmentError(warning_message)
        self.client = Client(TwilioConfig.ACCOUNT_SID, TwilioConfig.AUTH_TOKEN)
        self.phone_nums = TwilioConfig.DEST_NUMBERS.split(",")
        logger.info(
            "Twilio: will notify these phone numbers: " + ", ".join(self.phone_nums)
        )

    def send_message(self, message: str, **kwargs):
        """
        Send a message via Twilio - if environment variables are configured

        Parameters
        ----------
        message: str
        """
        for phone_num in self.phone_nums:
            self.client.messages.create(
                to=phone_num, from_=TwilioConfig.SOURCE_NUMBER, body=message
            )

    def send_campsites(self, campsites: List[AvailableCampsite], **kwargs):
        """
        Send a message with a campsite object

        Parameters
        ----------
        campsites: AvailableCampsite
        """
        for campsite in campsites:
            message_title, formatted_dict = self.format_standard_campsites(
                campsite=campsite,
            )
            fields = [f"πŸ•{message_title}", ""]
            for key, value in formatted_dict.items():
                fields.append(f"{key}: {value}")
            fields.append("")
            fields.append("camply, the campsite finder ⛺️")
            composed_message = "\n".join(fields)
            self.send_message(message=composed_message)

send_campsites(campsites, **kwargs) #

Send a message with a campsite object

Parameters:

Name Type Description Default
campsites List[AvailableCampsite]
required
Source code in camply/notifications/twilio.py
def send_campsites(self, campsites: List[AvailableCampsite], **kwargs):
    """
    Send a message with a campsite object

    Parameters
    ----------
    campsites: AvailableCampsite
    """
    for campsite in campsites:
        message_title, formatted_dict = self.format_standard_campsites(
            campsite=campsite,
        )
        fields = [f"πŸ•{message_title}", ""]
        for key, value in formatted_dict.items():
            fields.append(f"{key}: {value}")
        fields.append("")
        fields.append("camply, the campsite finder ⛺️")
        composed_message = "\n".join(fields)
        self.send_message(message=composed_message)

send_message(message, **kwargs) #

Send a message via Twilio - if environment variables are configured

Parameters:

Name Type Description Default
message str
required
Source code in camply/notifications/twilio.py
def send_message(self, message: str, **kwargs):
    """
    Send a message via Twilio - if environment variables are configured

    Parameters
    ----------
    message: str
    """
    for phone_num in self.phone_nums:
        self.client.messages.create(
            to=phone_num, from_=TwilioConfig.SOURCE_NUMBER, body=message
        )