コード例 #1
0
 def handle(self) -> MessageResponse:
     message = gettext('Unrecognized option "%(user_input)s". ',
                       user_input=self.user_input)
     if self.client.is_enabled_for_alerts:
         message += gettext(
             "Reply with M for the menu or U to stop this alert.")
     else:
         message += gettext("Reply with M for the menu.")
     return MessageResponse(body=message)
コード例 #2
0
ファイル: feedback.py プロジェクト: airq-dev/hazebot
    def handle(self) -> MessageResponse:
        selected_choice = self._get_selected_choice()
        if selected_choice == "E":
            return MessageResponse(body=gettext("Please enter your feedback below:"))

        feedback = self.user_input
        if selected_choice:
            feedback = dict(self.feedback_choices()).get(selected_choice, feedback)

        send_email(
            ["*****@*****.**"],
            "User {} gave feedback{}".format(
                self.client.identifier, " on unsubscribe" if self.is_unsubscribe else ""
            ),
            f'User feedback: "{feedback}"',
        )
        self.client.log_event(EventType.FEEDBACK_RECEIVED, feedback=feedback)
        return MessageResponse(body=gettext("Thank you for your feedback!"))
コード例 #3
0
    def handle(self) -> MessageResponse:
        if self.client.zipcode is None:
            return self._get_missing_zipcode_message()

        if self.client.is_enabled_for_alerts:
            return MessageResponse(
                body=gettext(
                    "Looks like you're already watching %(zipcode)s.",
                    zipcode=self.client.zipcode.zipcode,
                )
            )

        self.client.enable_alerts()

        return MessageResponse(
            body=gettext(
                "Got it! We'll send you timely alerts when air quality in %(zipcode)s changes category.",
                zipcode=self.client.zipcode.zipcode,
            )
        )
コード例 #4
0
ファイル: prefs.py プロジェクト: airq-dev/hazebot
 def handle(self) -> MessageResponse:
     response = MessageResponse()
     response.write(gettext("Which preference do you want to set?"))
     for letter, pref in ClientPreferencesRegistry.iter_with_letters():
         response.write(f"{letter} - {pref.display_name}: {pref.description}")
     self.client.log_event(EventType.LIST_PREFS)
     return response
コード例 #5
0
    def _get_message(self, _zipcode: Zipcode) -> MessageResponse:
        response = (
            MessageResponse()
            .write(self.client.get_current_pm25_level().description)
            .write("")
        )

        num_desired = 3
        recommended_zipcodes = self.client.get_recommendations(num_desired)
        if recommended_zipcodes:
            response.write(
                gettext("Here are the closest places with better air quality:")
            )
            conversion_factor = self.client.conversion_factor
            for recommendation in recommended_zipcodes:
                response.write(
                    gettext(
                        " - %(city)s %(zipcode)s: %(pm25_level)s (%(distance)s mi)",
                        city=recommendation.city.name,
                        zipcode=recommendation.zipcode,
                        pm25_level=recommendation.get_pm25_level(
                            conversion_factor
                        ).display.upper(),
                        distance=round(
                            kilometers_to_miles(
                                recommendation.distance(self.client.zipcode)
                            ),
                            ndigits=1,
                        ),  # TODO: Make this based on locale
                    )
                )
            response.newline()

        response.write(
            ngettext(
                "Average PM2.5 from %(num)d sensor near %(zipcode)s is %(pm25)s ug/m^3.",
                "Average PM2.5 from %(num)d sensors near %(zipcode)s is %(pm25)s ug/m^3.",
                self.client.zipcode.num_sensors,
                zipcode=self.client.zipcode.zipcode,
                pm25=self.client.get_current_pm25(),
            )
        )

        self.client.log_event(
            EventType.DETAILS,
            zipcode=self.client.zipcode.zipcode,
            recommendations=[r.zipcode for r in recommended_zipcodes],
            pm25=self.client.get_current_pm25(),
            num_sensors=self.client.zipcode.num_sensors,
        )

        return response
コード例 #6
0
    def handle(self) -> MessageResponse:
        if self.client.zipcode is None:
            return self._get_missing_zipcode_message()

        if self.client.alerts_disabled_at:
            return MessageResponse(body=gettext(
                "Looks like you already stopped watching %(zipcode)s.",
                zipcode=self.client.zipcode.zipcode,
            ))

        self.client.disable_alerts()

        message = [
            gettext(
                "Got it! You will not receive air quality updates until you text a new zipcode."
            ),
            "",
            gettext(
                "Tell us why you're leaving so we can improve our service:"),
        ]
        for key, choice in ReceiveFeedback.feedback_choices():
            message.append(f"{key}. {choice}")
        return MessageResponse.from_strings(message)
コード例 #7
0
 def handle(self) -> MessageResponse:
     self.client.log_event(EventType.MENU)
     return MessageResponse.from_strings([
         gettext("Reply"),
         gettext("1. Air recommendations"),
         gettext("2. Current AQI"),
         gettext("3. Set preferences"),
         gettext("4. About us"),
         gettext("5. Give feedback"),
         gettext("6. Stop alerts"),
         gettext("7. Donate"),
         "",
         gettext("Or, enter a new zipcode."),
     ])
コード例 #8
0
ファイル: prefs.py プロジェクト: airq-dev/hazebot
    def handle(self) -> MessageResponse:
        event = self.client.get_last_client_event()
        if not event or event.type_code != EventType.SET_PREF_REQUEST:
            return MessageResponse(
                body=gettext("Hmm, looks like something went wrong. Try again?")
            )

        pref_name = event.validate()["pref_name"]
        pref = ClientPreferencesRegistry.get_by_name(pref_name)

        try:
            value = pref.set_from_user_input(self.client, self.user_input)
        except InvalidPrefValue as e:
            return MessageResponse(body=str(e))

        self.client.log_event(EventType.SET_PREF, pref_name=pref.name, pref_value=value)

        return MessageResponse(
            body=gettext(
                "Your %(pref)s is now %(value)s",
                pref=pref.display_name,
                value=pref.format_value(value),
            )
        )
コード例 #9
0
    def handle(self) -> MessageResponse:
        if self.params.get("zipcode"):
            zipcode = Zipcode.query.get_by_zipcode(self.params["zipcode"])
            if zipcode is None:
                return MessageResponse(
                    body=gettext(
                        "Hmm. Are you sure %(zipcode)s is a valid US zipcode?",
                        zipcode=self.params["zipcode"],
                    )
                )
        else:
            if self.client.zipcode is None:
                return self._get_missing_zipcode_message()
            zipcode = self.client.zipcode

        if not zipcode.pm25 or zipcode.is_pm25_stale:
            return MessageResponse(
                body=gettext(
                    'Oops! We couldn\'t determine the air quality for "%(zipcode)s". Please try a different zip code.',
                    zipcode=zipcode.zipcode,
                )
            )

        return self._get_message(zipcode)
コード例 #10
0
ファイル: __init__.py プロジェクト: airq-dev/hazebot
def handle_command(
    user_input: str, identifier: str, identifier_type: ClientIdentifierType, locale: str
) -> MessageResponse:
    if not app.config["HAZEBOT_ENABLED"]:
        return MessageResponse(
            "Hazebot is sleeping until fire season. "
            "We'll be back in June 2022. "
            "Until then, you can email [email protected] with questions or feedback."
        )
    else:
        client, was_created = Client.query.get_or_create(
            identifier, identifier_type, locale
        )
        if not was_created:
            client.mark_seen(locale)
        return _parse_command(client, user_input).handle()
コード例 #11
0
ファイル: prefs.py プロジェクト: airq-dev/hazebot
    def handle(self) -> MessageResponse:
        letter = self.user_input.strip()
        pref = ClientPreferencesRegistry.get_by_letter(letter)
        if pref is None:
            return MessageResponse(
                body=gettext(
                    "Hmm, %(input)s doesn't seem to be a valid choice. Please try again.",
                    input=self.user_input[:20],
                )
            )

        self.client.log_event(EventType.SET_PREF_REQUEST, pref_name=pref.name)

        response = MessageResponse()
        response.write(pref.get_prompt())
        formatted_value = pref.format_value(getattr(self.client, pref.name))
        response.write(gettext("Current: %(value)s", value=formatted_value))

        return response
コード例 #12
0
ファイル: feedback.py プロジェクト: airq-dev/hazebot
 def handle(self) -> MessageResponse:
     self.client.log_event(EventType.FEEDBACK_BEGIN)
     # TODO: Consider adding cancel option
     return MessageResponse(body=gettext("Please enter your feedback below:"))
コード例 #13
0
    def _get_message(self, zipcode: Zipcode) -> MessageResponse:
        is_first_message = self.client.zipcode_id is None
        was_updated = self.client.update_subscription(zipcode)

        aqi_display = gettext(" (AQI %(aqi)s)", aqi=self.client.get_current_aqi())

        if self.client.is_enabled_for_alerts and is_first_message and was_updated:
            response = (
                MessageResponse()
                .write(
                    gettext(
                        "Welcome to Hazebot! We'll send you alerts when air quality in %(city)s %(zipcode)s changes category. Air quality is now %(pm25_level)s%(aqi_display)s.",
                        city=zipcode.city.name,
                        zipcode=zipcode.zipcode,
                        pm25_level=self.client.get_current_pm25_level().display,
                        aqi_display=aqi_display,
                    )
                )
                .newline()
                .write(
                    gettext(
                        'Save this contact and text us your zipcode whenever you\'d like an instant update. And you can always text "M" to see the whole menu.'
                    )
                )
                .media(
                    f"{config.SERVER_URL}/public/vcard/{self.client.locale}.vcf",
                )
            )
        else:
            response = (
                MessageResponse()
                .write(
                    gettext(
                        "%(city)s %(zipcode)s is %(pm25_level)s%(aqi_display)s.",
                        city=zipcode.city.name,
                        zipcode=zipcode.zipcode,
                        pm25_level=self.client.get_current_pm25_level().display,
                        aqi_display=aqi_display,
                    )
                )
                .newline()
            )
            if not self.client.is_enabled_for_alerts:
                response.write(
                    gettext(
                        'Alerting is disabled. Text "Y" to re-enable alerts when air quality changes.'
                    )
                )
            elif was_updated:
                response.write(
                    gettext(
                        "You are now receiving alerts for %(zipcode)s.",
                        zipcode=zipcode.zipcode,
                    )
                )
            else:
                response.write(gettext('Text "M" for Menu, "E" to end alerts.'))

        self.client.log_event(
            self.event_type,
            zipcode=zipcode.zipcode,
            pm25=self.client.get_current_pm25(),
        )

        return response
コード例 #14
0
 def handle(self) -> MessageResponse:
     self.client.log_event(EventType.ABOUT)
     return MessageResponse(body=gettext(
         "hazebot runs on PurpleAir sensor data and is a free service. Reach us at hazebot.org or [email protected]. Press 7 for information on how to support our work."
     ))
コード例 #15
0
 def handle(self) -> MessageResponse:
     self.client.log_event(EventType.DONATE)
     return MessageResponse(body=gettext(
         "Like this project? A few dollars allows hundreds of people to breathe easy with hazebot. Help us reach more by donating here: https://bit.ly/3bh0Cx9."
     ))