async def right_time_for_suggestions(self):
     is_ok = await s.get_conf_value(s.CAMPAIN_ID, 'DEFAULT_SUGGESTIONS')
     if is_ok is None:
         # todo_cr : cette excepiton instervient au tout premier lancement du programme quand la BDD n'est pas encore initialisée. Faire attention à ibintialise la BDD au tout début
         logging.error("On checke si on peut envoyer des suggestions sauf que la variable DEFAULT_SUGGESTIONS est absente de la configuration")
         is_ok = False
     return is_ok
Exemple #2
0
 def _execute(self, queries, except_func=None):
     conn = sqlite3.connect(self.db_file)
     c = conn.cursor()
     res = None
     for q, args in queries:
         try:
             res = c.execute(q, args)
         except sqlite3.OperationalError as e:
             if "no such table" in str(e):
                 self.create_schema()
                 res = c.execute(q, args)
             else:
                 if except_func is not None:
                     except_func(e)
                 else:
                     logging.error(e)
         except Exception as e:
             if except_func is not None:
                 except_func(e)
             else:
                 logging.error(e)
     if res is None:
         result = None
     else:
         result = [e for e in res]
     conn.commit()
     conn.close()
     return result
Exemple #3
0
    async def iter_participants(self, tg_channel, first_time=False, **kwargs):
        if tg_channel is None:
            logging.error(
                "On essaie d'obtenir les participant·e·s d'un channel qui n'a pas été trouvé"
            )
            return iter([])

        try:
            tg_participants = self.client.iter_participants(
                tg_channel, **kwargs)
        except Exception as e:
            logging.exception(e)
            return iter([])

        now = dt.datetime.now(s.TIMEZONE)
        # todo_es : !!!! lors d'une suppression de bdd postgres (ou nouveau campain_id), cette ligne plante au tout premier lancement, pas au 2nd
        default_sug_freq = await s.get_conf_value(
            s.CAMPAIN_ID, 'DEFAULT_SUGGESTIONS_FREQUENCY')

        res = [
            await
            TelegramParticipant.from_telethon_object(p, now, default_sug_freq,
                                                     first_time)
            async for p in tg_participants
        ]
        # todo_chk peut on utiliser yield ici?

        return iter(res)
Exemple #4
0
 def _manage_flood_wait_error(exception):
     """
     Méthode appéles dans le cas où le code a été saisi trop de fois de manière erronée :
     Télégram exige alors un temps d'attente avant de pouvoir retenter sa chance.
     """
     logging.test(20079)
     # waiting_time = TelegramConnetion._find_waiting_time(exception)
     logging.error(
         s.STR_TG_TOO_MANY_INVALID.format(exception.seconds, ts.__name__))
     sys.exit()
Exemple #5
0
    def send_message(self, tg_channel, message, **kwargs):
        if tg_channel is None:
            logging.error(
                "On essaie d'envoyer un message vers un channel qui n'a pas été trouvé\nMessage=\n"
                + str(message))
            return None

        try:
            res = self.client.send_message(tg_channel, message, **kwargs)
        except Exception as e:
            logging.exception(e)
            return None

        return res
Exemple #6
0
    async def get_messages(self, tg_channel, nb_msgs, **kwargs):
        if tg_channel is None:
            logging.error(
                "On essaie d'obtenir les messages d'un channel qui n'a pas été trouvé"
            )
            return []

        try:
            res = await self.client.get_messages(tg_channel, nb_msgs, **kwargs)
        except Exception as e:
            logging.exception(e)
            return []

        res = [TelegramMessage.from_telethon_object(m) for m in res]
        return res
    async def send(self, participant, channel, msg, force=False, debug=False):
        """

        @rtype: None
        """

        if not self.is_bot(participant) and await self._is_reachable(participant):
            # todo_f : vérifier dans la conf que ME n'est pas le bot
            if participant.is_ok or force:
                if await s.get_conf_value(s.CAMPAIN_ID, 'SEND_ONLY_TO_ME'):
                    if self.me is None or self.my_channel is None:
                        logging.error("Il semble que self.me ou self.my_channel n'aient pas été initialisés.")
                    msg = s.SEND_ONLY_TO_ME_INTRO.format(participant.display_name,participant.get_normalised_id(), msg)
                    participant = self.me
                    channel = self.my_channel
                await self._send_and_record_message(participant, channel, msg)