def get_response_opt_out(self, event, action):
     manager = OptOutManager(self.state)
     user_id = event.message.from_.id
     had_user_opted_out = manager.has_user_opted_out(user_id)
     if action == "add-me":
         if had_user_opted_out:
             response = FormattedText().bold("❌ You had already opted-out.")
         else:
             manager.add_user(user_id)
             response = FormattedText().bold(
                 "✅ You have been added to the opt-out list of this feature."
             )
     elif action == "remove-me":
         if not had_user_opted_out:
             response = FormattedText().bold(
                 "❌ You are not currently on the list.")
         else:
             manager.remove_user(user_id)
             response = FormattedText().bold(
                 "✅ You have been removed from the opt-out list of this feature."
             )
     else:
         if had_user_opted_out:
             response = FormattedText().bold(
                 "🙃 You are in the opt-out list.")
         else:
             response = FormattedText().bold(
                 "🙂 You are NOT in the opt-out list.")
     if manager.is_override_enabled_on_chat(event):
         response.newline().newline()\
             .bold("⚠️ Opt-out override is currently enabled on this chat ⚠️").newline()\
             .normal("Opt-out list is not in effect here while override is enabled.")
     return response.build_message()
 def process(self, event):
     response = FormattedText().newline().join(ConfigStatus(self.config, self.state).get_config_status())
     response.newline().newline()\
         .italic("These are the current values read from storage.").newline()\
         .italic("But please, note that most of them are being cached at startup and "
                 "changing them will not modify bot behavior until it is restarted.")
     self.api.send_message(response.build_message().to_chat_replying(event.message))
Beispiel #3
0
 def __get_current_value(settings, key):
     text = FormattedText()
     value = settings.get(key)
     if value is None:
         text.italic("No value")
     else:
         text.code_block(value)
     if not settings.is_set(key):
         text.newline().italic("(default)")
     return text
 def process(self, event):
     api = self.api.no_async
     error = NotARealError("simulated error")
     response = FormattedText().bold("Simulating bot error...")
     args = event.command_args.split()
     if "fatal" in args:
         error = NotARealFatalError("simulated fatal error")
         response.newline().normal(" - ").bold("FATAL")
     if "async" in args:
         api = self.api. async
         response.newline().normal(" - ").bold("async")
Beispiel #5
0
 def get_current_value(self, settings, key):
     value = self.__get_current_value(settings, key)
     response = FormattedText().bold("Setting").normal(":").newline().code_block(key).newline().newline()\
         .bold("Value").normal(":").newline().concat(value)
     is_supported = settings.is_supported(key)
     response.newline().newline()\
         .bold("Supported?").newline().code_block("yes" if is_supported else "no")
     if is_supported and settings.is_set(key):
         response.newline().newline()\
             .bold("Default value").normal(":").newline().code_block(settings.get_default_value(key))
     return response.build_message()
Beispiel #6
0
 def get_response_list(event, action_params, handler):
     text = FormattedText().normal("List of pole timezones:").newline()
     for alias in handler.get_timezones():
         state = handler.get_timezone_state(alias)
         name = state.get_value("timezone", DEFAULT_TIMEZONE)
         text.bold(alias).normal(" → ").bold(name)
         offset_seconds = state.offset_seconds
         if offset_seconds is not None:
             text.normal(" (with ").bold(offset_seconds).normal(
                 " seconds offset)")
         text.newline()
     return text.build_message()
 def _full_content(self,
                   content_field="text",
                   prepend_newlines_if_content=False):
     text = FormattedText()
     content = getattr(self.message, content_field)
     if content is not None:
         if prepend_newlines_if_content:
             text.newline().newline()
         text.normal(self.start_content).bold(
             content_field.capitalize()).bold(":").newline()
         text.normal(content)
     text.concat(self._full_edits_content(content_field))
     return text
Beispiel #8
0
 def list_settings(settings):
     keys = settings.list()
     response = FormattedText().normal(
         "Settings status for this chat:").newline()
     for setting_name, value, default_value, is_set, is_supported in keys:
         response.newline()
         if not is_supported:
             response.code_inline(setting_name)
         elif is_set:
             response.bold(setting_name)
         else:
             response.normal(setting_name)
         response.normal(" → ").code_inline(value)
         if is_set and is_supported:
             response.normal(" (default: ").code_inline(
                 default_value).normal(")")
     return response.build_message()
 def _full_edits_content(self, edited_field="text"):
     text = FormattedText()
     total_number_of_edits = len(self.edited_messages)
     for index, edited_message in enumerate(self.edited_messages):
         formatted_date = DateFormatter.format_full(
             edited_message.edit_date)
         edited_content = getattr(edited_message, edited_field)
         text.newline().newline()
         text.normal(self.bullet).normal("Edit ").bold(index + 1).bold("/").bold(total_number_of_edits)\
             .normal(", done at ").bold(formatted_date).normal(".").newline()
         if edited_content is None:
             text.normal(self.bullet).bold(
                 edited_field.capitalize()).bold(" deleted")
         else:
             text.normal(self.start_content).bold("New ").bold(
                 edited_field).bold(":").newline()
             text.normal(edited_content)
     return text
Beispiel #10
0
 def __build_success_response_message(event,
                                      title,
                                      printable_poles,
                                      footer_text=None):
     # header
     text = FormattedText().normal(title).newline()
     # body
     if isinstance(printable_poles, FormattedText):
         text.concat(printable_poles)
     else:
         text.normal(printable_poles)
     # footer
     text.newline().newline()
     if footer_text is not None:
         text.concat(footer_text)
     else:
         text.normal(_("Write {0} to see more options."))\
             .start_format().bold(event.command + " help").end_format()
     return text.build_message()
class VersionAction(Action):
    def __init__(self, project_package_name: str, releases_url: str = None):
        super().__init__()
        version = self.get_version(project_package_name)
        self.text = FormattedText().normal("Version {version}")
        if releases_url:
            self.text.newline().newline().normal("Releases: {releases_url}")
        self.text.start_format().bold(version=version).normal(
            releases_url=releases_url).end_format()

    @staticmethod
    def get_version(project_package_name: str):
        try:
            return pkg_resources.get_distribution(project_package_name).version
        except:
            return "<unknown>"

    def process(self, event):
        self.api.send_message(self.text.build_message().to_chat_replying(
            event.message))
 def _full_content_header(self):
     text = FormattedText()\
         .normal(self.bullet)\
         .normal("Message ").bold(self.message_id)\
         .normal(" sent on ").bold(self.full_date)\
         .normal(" by ").bold(self.full_user)\
         .normal(".").newline()
     self.__add_reply_info_if_needed(text)
     self.__add_forwarded_info_if_needed(text)
     self.__add_edit_info_if_needed(text)
     self.__add_incomplete_info_if_needed(text)
     return text.newline()
 def __build_message(name: str, project_name: str, version: str,
                     authors: FormattedText, framework: FormattedText,
                     is_open_source: bool, license: FormattedText, url: str,
                     donation_addresses: FormattedText):
     text = FormattedText()\
         .normal("{project_name}, version {version}.")
     if framework:
         text.newline()\
             .normal("Based on {framework}.")
     if authors:
         text.newline().newline()\
             .bold("Authors").normal(":").newline()\
             .normal("{authors}")
     if is_open_source:
         text.newline().newline()\
             .normal("{project_name} is Open Source.").newline()\
             .normal("You can inspect its code, improve it and launch your own instance "
                     "(complying with the license).")
     if license:
         text.newline().newline()\
             .normal("{project_name} is licensed under the {license} license.")
     if url:
         text.newline().newline()\
             .normal("{project_name} home:").newline()\
             .normal("{url}")
     if donation_addresses:
         text.newline().newline()\
             .normal("If you find {name} useful and want to support its development, "
                     "please consider donating to any of the following addresses:").newline()\
             .normal("{donation_addresses}")
     return text.start_format()\
         .bold(name=name, project_name=project_name, version=version)\
         .normal(url=url)\
         .concat(framework=framework, authors=authors, license=license, donation_addresses=donation_addresses)\
         .end_format()\
         .build_message()