def parse_oob_xml(self, oob: ET.Element): if oob is not None and oob.text is not None: self._location = oob.text return True else: YLogger.error(self, "Unvalid geomap oob command - missing location text!") return False
def poll_and_answer(self): running = True try: (last_direct_message_id, last_status_id) = self._get_last_message_ids() self._poll(last_direct_message_id, last_status_id) except KeyboardInterrupt: running = False except RateLimitError: if self._configuration.client_configuration.rate_limit_sleep != -1: rate_limit_sleep = self._configuration.client_configuration.rate_limit_sleep else: rate_limit_sleep = self.FIFTEEN_MINUTES YLogger.error(self, "Rate limit exceeded, sleeping for %d seconds", rate_limit_sleep) self.sleep(rate_limit_sleep) except Exception as excep: YLogger.exception(self, "Poll and answer error", excep) return running
def save_conversation(self, clientid): if self._conversation_storage is not None: if clientid in self._conversations: conversation = self._conversations[clientid] self._conversation_storage.save_conversation(conversation, clientid) else: YLogger.error(self, "Unknown conversation id type [%s] unable tonot persist!", clientid)
def create_viber_bot(self, viber_token): if viber_token is None: YLogger.error(self, "'viber_token' missing") return None name = self.configuration.client_configuration.name if name is None: YLogger.error(self, "'name' missing from Viber configuration") return None avatar = self.configuration.client_configuration.avatar if avatar is None: YLogger.error(self, "'avatar' missing from Viber configuration") return None webhook = self.configuration.client_configuration.webhook if webhook is None: YLogger.error(self, "'webhook' missing from Viber configuration") return None configuration = BotConfiguration( name=name, avatar=avatar, auth_token=viber_token ) bot = self.create_viber_api(configuration) if bot is not None: YLogger.error(self, "'Failed to create Viber api") bot.set_webhook(webhook) return bot
def parse_oob_xml(self, oob: ET.Element): if oob is not None and oob.text is not None: self._search = oob.text return True else: YLogger.error(self, "Unvalid search oob command - missing search query!") return False
def receive_message(self, request): api_key_response = self.check_api_key(request) if api_key_response is not None: return api_key_response question = self.get_question(request) if question is None: YLogger.error(self, "'question' missing from request") abort(400) userid = self.get_userid(request) userid_expire_date = self.get_userid_cookie_expirary_date(self.configuration.client_configuration.cookie_expires) client_context = self.create_client_context(userid) try: answer = self.get_answer(client_context, question) rendered = self._renderer.render(client_context, answer) response_data = self.create_success_response_data(question, rendered) except Exception as excep: YLogger.exception(self, "Failed receving message", excep) response_data = self.create_error_response_data(client_context, question, str(excep)) return self.create_response(response_data, userid, userid_expire_date)
def handle_message(self, message): try: if self.configuration.client_configuration.debug is True: self.dump_request(message) self.renderer.print_payload("Incoming", message) # Facebook Messenger ID for user so we know where to send response back to recipient_id = self.get_recipitent_id(message) if recipient_id: client_context = self.create_client_context(recipient_id) message_text = self.get_message_text(message) # We have been send a text message, we can respond if message_text is not None: response_text = self.handle_text_message(client_context, message_text) # else if user sends us a GIF, photo,video, or any other non-text item elif self.has_attachements(message): response_text = self.handle_attachement(client_context, message) # otherwise its a general error else: YLogger.error(client_context, "Facebook general error handling message") response_text = "Sorry, I do not understand you!" YLogger.debug(client_context, "Facebook message response: [%s]", response_text) self.render_response(client_context, response_text) except Exception as e: YLogger.exception(None, "Error handling facebook message", e)
def equals(self, client_context, words, word_no): word = words.word(word_no) if self.userid != '*': if self.userid != client_context.userid: return EqualsMatch(False, word_no) if self._pattern_template is not None: template = client_context.brain.regex_templates[self._pattern_template] if template is not None: result = template.match(word) if result is not None: YLogger.debug(client_context, "Match word [%s] regex", word) return EqualsMatch(True, word_no, word) else: YLogger.error(client_context, "No word [%s] matched regex", word) return EqualsMatch(False, word_no) else: return EqualsMatch(False, word_no) else: result = self.pattern.match(word) if result is not None: YLogger.debug(client_context, "Match word [%s] regex", word) return EqualsMatch(True, word_no, word) else: YLogger.error(client_context, "No word [%s] matched regex", word) return EqualsMatch(False, word_no)
def parse_oob_xml(self, oob: ET.Element): if oob is not None and oob.text is not None: self._url = oob.text return True else: YLogger.error(self, "Unvalid url oob command - missing url!") return False
def parse_oob_xml(self, oob): if oob is not None and oob.text is not None: self._command = oob.text return True else: YLogger.error(self, "Unvalid clear oob command - missing command") return False
def get_property_value(client_context, local, name): if local is True: value = None #TODO Why would you need this test, when is get_conversation(clientid) == None ? if client_context.bot.get_conversation(client_context) is not None: if client_context.bot.get_conversation(client_context).has_current_question(): value = client_context.bot.get_conversation(client_context).current_question().property(name) else: if name is not None and client_context.brain.dynamics.is_dynamic_var(name) is True: value = client_context.brain.dynamics.dynamic_var(client_context, name) else: value = client_context.bot.get_conversation(client_context).property(name) #if value is None: # value = bot.brain.properties.property(name) if value is None: YLogger.error(client_context, "No property for [%s]", name) value = TemplateGetNode.get_default_value(client_context.bot) return value
def load_license_key_file(self, license_key_filename): try: YLogger.info(self, "Loading license key file: [%s]", license_key_filename) with open(license_key_filename, "r", encoding="utf-8") as license_file: for line in license_file: self._process_license_key_line(line) except Exception: YLogger.error(self, "Invalid license key file [%s]", license_key_filename)
def load_config_section(self, configuration_file, configuration, bot_root): dynamic_config = configuration_file.get_section("dynamic", configuration) if dynamic_config is not None: self.load_dynamic_sets(configuration_file, dynamic_config) self.load_dynamic_maps(configuration_file, dynamic_config) self.load_dynamic_vars(configuration_file, dynamic_config) else: YLogger.error(self, "Config section [dynamic] missing from Brain, using defaults")
def get_headlines(self, source, max_articles=0, sort=False, reverse=False): if source in self.function_mapping: function = self.function_mapping[source] return function(self.api_key, max_articles, sort, reverse) else: YLogger.error(self, "No source available for %s", source) return []
def scheduled_event(name, userid, clientid, action, text): YLogger.debug(None, "Received Scheduled Event [%s] [%s] [%s] [%s] [%s]", name, userid, clientid, action, text) if name in ProgramyScheduler.schedulers: scheduler = ProgramyScheduler.schedulers[name] scheduler.scheduled(userid, clientid, action, text) else: YLogger.error(None, "Unknown scheduler [%s]", name)
def execute(self, context, data): source, max_num, sort, reverse = self.parse_data(context, data) if source is None: YLogger.error(context, "NewsAPIExtension no source passed in as data parameter!") return "" return self.get_news(context, source, max_num, sort, reverse)
def map_value(self, client_context, input_value): try: int_value = int(input_value) str_value = str(int_value + 1) YLogger.debug(client_context, "SuccessorMap converted %s to %s", input_value, str_value) return str_value except Exception: YLogger.error(client_context, "SuccessorMap could not convert %s to integer string", input_value) return ""
def unknown(self, telegram_bot, update): try: unknown_response = self.get_unknown_command(update.message.chat_id) if unknown_response: telegram_bot.send_message(chat_id=update.message.chat_id, text=unknown_response) YLogger.error(self, "No response to return in unknown()") except Exception as e: YLogger.exception(self, "Failed to handle unknown", e)
def resolve_to_string(self, client_context): sentence = client_context.bot.get_conversation(client_context).current_question().current_sentence() star = sentence.matched_context.star(1) if star is not None: resolved = client_context.bot.ask_question(client_context, star, srai=True) else: YLogger.error(client_context, "Sr node has no stars available") resolved = "" YLogger.debug(client_context, "[%s] resolved to [%s]", self.to_string(), resolved) return resolved
def load_file_contents(self, id, filename, userid="*"): YLogger.debug(self, "Loading map [%s]", filename) the_map = {} try: with open(filename, 'r', encoding='utf8') as my_file: for line in my_file: self.process_line(line, the_map) except Exception as excep: YLogger.error(self, "Failed to load map [%s] - %s", filename, excep) return the_map
def start(self, telegram_bot, update): try: initial_question = self.get_initial_question(update) if initial_question: telegram_bot.send_message(chat_id=update.message.chat_id, text=initial_question) else: YLogger.error(self, "Not initial question to return in start()") except Exception as e: YLogger.exception(self, "Failed to start", e)
def listener_event(event): try: message = ProgramyScheduler.get_event_str(event) if message is not None: YLogger.debug(None, message) else: YLogger.error(None, "Unknown APSchedulerEvent! %s", str(event)) except Exception as e: YLogger.exception(None, "APScheduler Listener Error", e)
def message(self, telegram_bot, update): try: response = self.ask_question(update.message.chat_id, update.message.text) if response: telegram_bot.send_message(chat_id=update.message.chat_id, text=response) else: YLogger.error(self, "Not response to return in message()") except Exception as e: YLogger.exception(self, "Failed to handle message", e)
def test_check_log_not_output(self): logging.getLogger().setLevel(level=logging.NOTSET) YLogger.set_default_level() YLogger.critical(None, "Critical Log") YLogger.fatal(None, "Fatal Log") YLogger.error(None, "Error Log") YLogger.exception(None, "Exception Log", Exception("test")) YLogger.warning(None, "Warning Log") YLogger.info(None, "Info Log") YLogger.debug(None, "Debug Log")
def load_file_contents(self, id, filename, userid="*"): YLogger.debug(self, "Loading set [%s]", filename) the_set = {} try: with open(filename, 'r', encoding='utf8') as my_file: for line in my_file: self.process_line(line, the_set) except Exception as excep: YLogger.error(self, "Failed to load set [%s] - %s", filename, excep) return self.sort_sets(the_set)
def shutdown_ping_service(self): self.unregister_with_healthchecker() try: url = "http://%s:%d%s" % (self.config.host, self.config.port, self.config.shutdown) requests.get(url) except Exception: YLogger.error(None, "Failed to shutdown ping service")
def load_config_section(self, configuration_file, configuration, bot_root): dynamic_config = configuration_file.get_section( "dynamic", configuration) if dynamic_config is not None: self.load_dynamic_sets(configuration_file, dynamic_config) self.load_dynamic_maps(configuration_file, dynamic_config) self.load_dynamic_vars(configuration_file, dynamic_config) else: YLogger.error( self, "Config section [dynamic] missing from Brain, using defaults")
def get_default_value(bot): value = bot.brain.properties.property("default-get") if value is None: YLogger.error(None, "No property defined for default-get, checking defaults") value = bot.brain.configuration.defaults.default_get if value is None: YLogger.error(None, "No value defined for default default-get, returning 'unknown'") value = "unknown" return value
def load_services(self, client): YLogger.debug(self, "Loading services") self._client = client if client.storage_factory.entity_storage_engine_available( StorageFactory.SERVICES) is True: storage_engine = client.storage_factory.entity_storage_engine( StorageFactory.SERVICES) services_store = storage_engine.services_store() services_store.load_all(self) else: YLogger.error(None, "No storage engine available for services!")
def verify_fb_token(self, token_sent, request): # take token sent by facebook and verify it matches the verify token you sent # if they match, allow the request, else return an error if token_sent is None: YLogger.error(self, "Verify Token is None!") if token_sent == self._verify_token: return self.get_hub_challenge(request) YLogger.error(self, "Facebook verify token failed received [%s]", token_sent) return 'Invalid verification token'
def load_single_file_contents(self, filename): just_filename = self.get_just_filename_from_filepath(filename) collection = {} try: collection[just_filename] = self.load_file_contents(filename) except Exception as excep: YLogger.exception(self, excep) YLogger.error(self, "Failed to load file contents for file [%s]", filename) return collection
def load_from_filename(self, filename, id=None): count = 0 try: with open(filename, "r", encoding="utf-8") as data_file: for line in data_file: if self.process_line(line, id): count += 1 except FileNotFoundError: YLogger.error(self, "File not found [%s]", filename) return count
def split_line_by_pattern(self, line): line = line.strip() if line is not None and line: pattern = self.get_split_pattern() match = pattern.search(line) if match is not None: lhs = match.group(1) rhs = match.group(2) return [lhs, rhs] YLogger.error(self, "Pattern is bad [%s]", line) return None
def load_from_filename(self, filename): count = 0 try: with open(filename, "r", encoding="utf-8") as data_file: for line in data_file: if self.process_line(line): count += 1 except FileNotFoundError: YLogger.error(self, "File not found [%s]", filename) return count
def load_file_contents(self, filename): YLogger.debug(self, "Loading map [%s]", filename) the_map = {} try: with open(filename, 'r', encoding='utf8') as my_file: for line in my_file: self.process_line(line, the_map) except Exception as excep: YLogger.error(self, "Failed to load map [%s] - %s", filename, excep) return the_map
def test_ylogger_error(self): client_context = ClientContext(TestClient(), "testid") YLogger.reset_snapshot() YLogger.error(client_context, "Test Message") snapshot = YLogger.snapshot() self.assertIsNotNone(snapshot) self.assertEqual( str(snapshot), "Critical(0) Fatal(0) Error(1) Exception(0) Warning(0) Info(0), Debug(0)" )
def execute(self, context, data): source, max_num, sort, reverse = self.parse_data(context, data) if source is None: YLogger.error( context, "NewsAPIExtension no source passed in as data parameter!") return "" return self.get_news(context, source, max_num, sort, reverse)
def scheduled(self, userid, clientid, action, text): YLogger.debug(None, "Processing Scheduled Event [%s] [%s] [%s] [%s] [%s]", self.name, userid, clientid, action, text) client_context = self._client.create_client_context(userid) if action == 'TEXT': self._client.render_response(client_context, text) elif action == 'SRAI': response = client_context.bot.ask_question(client_context, text) self._client.render_response(client_context, response) else: YLogger.error(client_context, "Unknown scheduler command [%s]", action)
def get_time(self, rest_request): time_param = self.get_parameter(rest_request, 'time') if time_param != 'None': try: iso8601.parse_date(time_param) except Exception: errMsg = "<time> datetime parser error: [%s]" % time_param YLogger.error(self, errMsg) self.server_abort(400, errMsg) return time_param
def save_conversation(self, clientid): if self._conversation_storage is not None: if clientid in self._conversations: conversation = self._conversations[clientid] self._conversation_storage.save_conversation( conversation, clientid) else: YLogger.error( self, "Unknown conversation id type [%s] unable tonot persist!", clientid)
def resolve_to_string(self, client_context): resolved = self.resolve_children_to_string(client_context) YLogger.debug(client_context, "[%s] resolved to [%s]", self.to_string(), resolved) if self._service is not None: bot_service = ServiceFactory.get_service(self._service) response = bot_service.ask_question(client_context, resolved) YLogger.debug(client_context, "SRAIX service [%s] return [%s]", self._service, response) return response else: YLogger.error(client_context, "Sorry SRAIX does not currently have an implementation for [%s]", self._service) return ""
def load_config_section(self, configuration_file, configuration, bot_root, subs: Substitutions = None): storage = configuration_file.get_section(self._section_name, configuration) if storage is not None: self._conversation_logger = configuration_file.get_option( storage, "conversation_logger", subs=subs) else: YLogger.error(None, "'config' section missing from storage config")
def load_config_section(self, configuration_file, configuration, bot_root, subs: Substitutions = None): storage = configuration_file.get_section(self._section_name, configuration) if storage is not None: self._host = configuration_file.get_option(storage, "host") self._port = configuration_file.get_option(storage, "port") self._password = configuration_file.get_option(storage, "password") self._db = configuration_file.get_option(storage, "db") self._prefix = configuration_file.get_option(storage, "prefix") self._drop_all_first = configuration_file.get_option(storage, "drop_all_first") self._expiretime = configuration_file.get_option(storage, "expiretime") else: YLogger.error(None, "'config' section missing from storage config")
def equals(self, client_context, words, word_no): word = words.word(word_no) if word is not None: word = word.upper() for set_word in self._words: if word == set_word: YLogger.debug(client_context, "Found word [%s] in iset", word) return EqualsMatch(True, word_no, word) YLogger.error(client_context, "No word [%s] found in iset", word) return EqualsMatch(False, word_no)
def check_api_key(self, request): if self.configuration.client_configuration.use_api_keys is True: api_key = self.api_keys.get_api_key(request) if api_key is None: YLogger.error(self, "Unauthorised access - api required but missing") return self.unauthorised_access_response() if self.api_keys.is_apikey_valid(api_key) is False: YLogger.error(self, "'Unauthorised access - invalid api key") return self.unauthorised_access_response() return None
def load_trigger_manager(config: TriggerConfiguration): if config.manager is not None: try: return ClassLoader.instantiate_class(config.manager)(config) except Exception as e: YLogger.exception(None, "Failed to load trigger manager [%s]", e, config.manager) else: YLogger.error(None, "No Trigger Manager defined in configuration") return None
def check_api_key(self, request): if self.configuration.client_configuration.use_api_keys is True: api_key = self.get_api_key(request) if api_key is None: YLogger.error(self, "Unauthorised access - api required but missing") return self.unauthorised_access_response() if self.is_apikey_valid(api_key) is False: YLogger.error(self, "'Unauthorised access - invalid api key") return self.unauthorised_access_response() return None
def __init__(self, argument_parser=None): self._viber_bot = None self._viber_token = None FlaskRestBotClient.__init__(self, "viber", argument_parser) YLogger.debug(self, "Viber Client is running....") if self._viber_token is not None: self._viber_bot = self.create_viber_bot(self._viber_token) else: YLogger.error(self, "Viber token missing, unable to create client")
def load_oob_processors(self, storage_factory): if storage_factory.entity_storage_engine_available( StorageFactory.OOBS) is True: storage_engine = storage_factory.entity_storage_engine( StorageFactory.OOBS) oobs_store = storage_engine.oobs_store() oobs_store.load(self) else: YLogger.error(None, "No storage engine available for oobs!") if self._oobs.get('default', None) is None: self._oobs['default'] = DefaultOutOfBandProcessor()
def authenticate(self, client_context): try: if client_context.userid in self.authorised: return True else: if self._auth_clientid(client_context) is True: return True return False except Exception as excep: YLogger.error(client_context, str(excep)) return False
def _load_file_contents(self, collection, filename): YLogger.debug(self, "Loading nodes from file [%s]", filename) count = 0 try: with open(filename, "r", encoding="utf-8") as file: for line in file: line = line.strip() if line: self.process_config_line(collection, line, filename) except FileNotFoundError: YLogger.error(self, "File not found [%s]", filename) return count
def map_value(self, client_context, input_value): try: int_value = int(input_value) str_value = str(int_value - 1) YLogger.debug(client_context, "PredecessorMap converted %s to %s", input_value, str_value) return str_value except Exception: YLogger.error( client_context, "PredecessorMap could not convert %s to integer string", input_value) return ""
def load_substitutions(self, substitutions): if substitutions is not None: try: with open(substitutions, "r") as subsfile: for line in subsfile: namevalue = line.strip().split(":") if len(namevalue) > 1: self.add_substitute(namevalue[0], ":".join(namevalue[1:])) except Exception as e: YLogger.error(None, "Failed to load substitutions file")