コード例 #1
0
    def _update_check(self):
        def _check_for_update(bot, job):
            user = Cfg.get('update', 'github_user')
            repo = Cfg.get('update', 'github_repo')
            gh = GitHub(github_user=user, github_repo=repo)

            try:
                # Get latest release
                response = gh.get_latest_release()
            except Exception as ex:
                logging.error(repr(ex))
                return

            if job.context:
                if job.context["tag"] == response["tag_name"]:
                    return
            else:
                job.context = dict()
                job.context["tag"] = response["tag_name"]

            release_notes = response["body"]

            try:
                response = gh.get_tags()
            except Exception as ex:
                logging.error(repr(ex))
                return

            new_hash = str()
            for t in response:
                if t["name"] == job.context["tag"]:
                    new_hash = t["commit"]["sha"]
                    break

            cfg_hash = Cfg.get("update", "update_hash")

            if cfg_hash != new_hash:
                for admin in Cfg.get("admin_id"):
                    update_cmd = utl.esc_md("/update")
                    tag = job.context['tag']

                    bot.send_message(
                        admin,
                        f"New release *{tag}* available\n\n"
                        f"*Release Notes*\n{release_notes}\n\n"
                        f"{update_cmd}",
                        parse_mode=ParseMode.MARKDOWN)

        if Cfg.get("update", "update_check") is not None:
            sec = utl.get_seconds(Cfg.get("update", "update_check"))

            if not sec:
                sec = con.DEF_UPDATE_CHECK
                msg = f"Update check time not valid. Using {sec} seconds"
                logging.warning(msg)

            try:
                self.job_queue.run_repeating(_check_for_update, sec, first=0)
            except Exception as e:
                logging.error(repr(e))
コード例 #2
0
    def _refresh_cache(self):
        if Cfg.get("refresh_cache") is not None:
            sec = utl.get_seconds(Cfg.get("refresh_cache"))

            if not sec:
                sec = con.DEF_CACHE_REFRESH
                msg = f"Refresh rate for caching not valid. Using {sec} seconds"
                logging.warning(msg)

            try:
                self.job_queue.run_repeating(APICache.refresh, sec, first=0)
            except Exception as e:
                logging.error(repr(e))
コード例 #3
0
    def get_action(self, bot, update, args):
        # Check if database is enabled
        if not Cfg.get("database", "use_db"):
            update.message.reply_text(
                text=f"{emo.ERROR} Plugin '{type(self).__name__}' "
                f"can't be used since database is disabled",
                parse_mode=ParseMode.MARKDOWN)
            return

        # Check if any arguments provided
        if not args:
            update.message.reply_text(text=f"Usage:\n{self.get_usage()}",
                                      parse_mode=ParseMode.MARKDOWN)
            return

        # 'list' argument - show all repeaters for a user
        if args[0].lower() == "list":
            chat_id = update.message.chat.id
            user_id = update.message.from_user.id

            repeaters = self.tgb.db.read_rep(user_id, chat_id)

            if repeaters:
                for rep in repeaters:
                    chat = self.tgb.db.read_chat(rep[2])
                    chat_name = chat[2] if chat else None

                    update.message.reply_text(
                        text=f"Command:\n`{rep[3]}`\n"
                        f"Chat:\n`{chat_name}`\n\n"
                        f"↺ {rep[4]} seconds\n\n"
                        f"(ID: {rep[0]})",
                        parse_mode=ParseMode.MARKDOWN,
                        reply_markup=self._keyboard_remove_rep())
                return
            else:
                update.message.reply_text(f"{emo.INFO} No repeaters active")
                return

        # Extract time interval
        interval = str()
        if args[0].startswith("i="):
            interval = args[0].replace("i=", "")
            args.pop(0)

        # Check if time interval is provided
        if not interval:
            update.message.reply_text(
                text=f"{emo.ERROR} Time interval has to be provided",
                parse_mode=ParseMode.MARKDOWN)
            return

        # In seconds
        interval = utl.get_seconds(interval)

        # Check if interval was successfully converted to seconds
        if not interval:
            update.message.reply_text(
                text=f"{emo.ERROR} Wrong format for time interval",
                parse_mode=ParseMode.MARKDOWN)
            return

        # Check for command to repeat
        if not args:
            update.message.reply_text(
                text=f"{emo.ERROR} Provide command to repeat",
                parse_mode=ParseMode.MARKDOWN)
            return

        # Check if command is repeater itself
        if args[0].replace("/", "") in self.get_cmds():
            update.message.reply_text(
                text=f"{emo.ERROR} Repeater can't repeat itself",
                parse_mode=ParseMode.MARKDOWN)
            return

        # Set command to repeat as current message text
        update.message.text = " ".join(args)

        try:
            self._run_repeater(update, interval)
            self.tgb.db.save_rep(update, interval)
        except IntegrityError as ie:
            err = "Repeater already saved"
            update.message.reply_text(f"{emo.ERROR} {err}")
            logging.warning(f"{err} {ie}")
            return
        except Exception as e:
            update.message.reply_text(f"{emo.ERROR} {e}")
            raise e

        update.message.reply_text(text=f"{emo.CHECK} Timer is active")