def wrapper(update, context):
        session = get_session()
        try:
            user = User.get_or_create(session, update.callback_query.from_user)

            if user.banned:
                return

            if config["mode"]["authorized_only"] and not user.authorized:
                return

            func(context.bot, update, session, user)

            session.commit()
        # Handle all not telegram relatated exceptions
        except Exception as e:
            if not ignore_exception(e):
                traceback.print_exc()

                if should_report_exception(context, e):
                    sentry.capture_exception(
                        tags={
                            "handler": "callback_query",
                        },
                        extra={
                            "query": update.callback_query,
                        },
                    )
        finally:
            session.close()
Пример #2
0
        def wrapper(context):
            session = get_session()
            try:
                func(context, session)

                session.commit()
            except:
                # Capture all exceptions from jobs.
                # We need to handle those inside the jobs
                traceback.print_exc()
                sentry.capture_exception(tags={"handler": "job"})
            finally:
                context.job.enabled = True
                session.close()
Пример #3
0
        def wrapper(update, context):
            session = get_session()
            try:
                user = User.get_or_create(session, update.inline_query.from_user)

                if user.banned:
                    return

                if config["mode"]["private_inline_query"] and not user.authorized:
                    return

                func(context, update, session, user)
                session.commit()

            # Handle all not telegram relatated exceptions
            except Exception as e:
                if not ignore_exception(e):
                    traceback.print_exc()
                    sentry.capture_exception(tags={"handler": "inline_query"})

            finally:
                session.close()
        def wrapper(update, context):
            session = get_session()
            chat = None
            message = None
            try:
                if hasattr(update, "message") and update.message:
                    message = update.message
                elif hasattr(update, "edited_message") and update.edited_message:
                    message = update.edited_message
                else:
                    raise Exception("Update didn't have a message.")

                user = User.get_or_create(session, message.from_user)

                if config["mode"]["authorized_only"] and not user.authorized:
                    if config["mode"]["private_inline_query"]:
                        text = i18n.t(
                            "text.misc.private_access_no_inline",
                            username=config["telegram"]["bot_name"],
                        )
                    else:
                        text = i18n.t(
                            "text.misc.private_access",
                            username=config["telegram"]["bot_name"],
                        )
                    message.chat.send_message(
                        text,
                        parse_mode="Markdown",
                        disable_web_page_preview=True,
                    )
                    session.commit()
                    return
                if not is_allowed(user, update, admin_only=admin_only):
                    return

                chat_id = message.chat_id
                chat_type = message.chat.type
                chat = Chat.get_or_create(session, chat_id, chat_type)

                if not is_allowed(user, update, chat=chat):
                    return

                response = func(context.bot, update, session, chat, user)

                session.commit()
                # Respond to user
                if hasattr(update, "message") and response is not None:
                    message.chat.send_message(response)

            # A user banned the bot
            except Unauthorized:
                if chat is not None:
                    session.delete(chat)

            # A group chat has been converted to a super group.
            except ChatMigrated:
                if chat is not None:
                    session.delete(chat)

            # Handle all not telegram relatated exceptions
            except Exception as e:
                if not ignore_exception(e):
                    traceback.print_exc()
                    if should_report_exception(context, e):
                        sentry.capture_exception(
                            tags={
                                "handler": "message",
                            },
                            extra={
                                "update": update.to_dict(),
                                "function": func.__name__,
                            },
                        )

                    error_message = i18n.t("text.misc.error")
                    try:
                        if "message" is not None:
                            message.chat.send_message(error_message)
                    except Exception as e:
                        if not ignore_exception(e):
                            raise e

            finally:
                session.close()