Ejemplo n.º 1
0
        def wrapper(update, context):
            session = get_session()
            try:
                user = get_user(session, update)
                if not is_allowed(user, update):
                    return

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

                session.commit()
            # Raise all telegram errors and let the generic error_callback handle it


#            except TelegramError as e:
#                raise e
# Handle all not telegram relatated exceptions
            except:
                traceback.print_exc()
                sentry.captureException()
                if hasattr(update, 'callback_query'
                           ) and update.callback_query is not None:
                    locale = 'English'
                    if user is not None:
                        locale = user.locale
                    update.callback_query.answer(
                        i18n.t('callback.error', locale=locale))
            finally:
                session.close()
Ejemplo n.º 2
0
        def wrapper(update, context):
            session = get_session()
            try:
                user = get_user(session, update)
                if not is_allowed(user, update):
                    return

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

                session.commit()
            # Handle all not telegram relatated exceptions
            except Exception as e:
                if not ignore_exception(e):
                    if config['logging']['debug']:
                        traceback.print_exc()
                    sentry.captureException()

                if hasattr(update, 'callback_query'
                           ) and update.callback_query is not None:
                    locale = 'English'
                    if user is not None:
                        locale = user.locale
                    update.callback_query.answer(
                        i18n.t('callback.error', locale=locale))
            finally:
                session.close()
Ejemplo n.º 3
0
def error_callback(update, context):
    """Handle generic errors from telegram."""
    try:
        raise context.error
    except BadRequest as e:
        # It took to long to send the inline query response.
        # Probably due to slow network on client side.
        if str(e) == 'Query_id_invalid':  # noqa
            return

        traceback.print_exc()
        sentry.captureException()
    # A user banned the bot Just ignore this.
    # This probably happens due to sending a message during maintenance work
    except Unauthorized:
        pass
    except (TimedOut, NetworkError):
        pass

    except:  # noqa
        if hasattr(update,
                   'callback_query') and update.callback_query is not None:
            update.callback_query.answer(i18n.t('callback.error'))
        traceback.print_exc()
        sentry.captureException()
Ejemplo n.º 4
0
        def wrapper(context):
            session = get_session()
            try:
                func(context, session)

                session.commit()
            except:  # noqa
                # Capture all exceptions from jobs. We need to handle those inside the jobs
                traceback.print_exc()
                sentry.captureException()
            finally:
                session.close()
Ejemplo n.º 5
0
    def wrapper(context):
        session = get_session()
        try:
            func(context, session)

            session.commit()
        except Exception as e:
            # Capture all exceptions from jobs. We need to handle those inside the jobs
            if not ignore_job_exception(e):
                if config["logging"]["debug"]:
                    traceback.print_exc()
                sentry.captureException()
        finally:
            session.close()
Ejemplo n.º 6
0
        def wrapper(update, context):
            session = get_session()
            try:
                user = get_user(session, update)
                if not is_allowed(user, update, private=private):
                    return

                if hasattr(update, 'message') and update.message:
                    message = update.message
                elif hasattr(update,
                             'edited_message') and update.edited_message:
                    message = update.edited_message

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

                response = func(context.bot, update, session, 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:
                pass

            # Raise all telegram errors and let the generic error_callback handle it


#            except TelegramError as e:
#                raise e

# Handle all not telegram relatated exceptions
            except:
                traceback.print_exc()
                sentry.captureException()
                if send_message:
                    locale = 'english'
                    if user is not None:
                        locale = user.locale
                    session.close()
                    message.chat.send_message(
                        i18n.t('misc.error', locale=locale),
                        parse_mode='markdown',
                        disable_web_page_preview=True,
                    )

            finally:
                session.close()
Ejemplo n.º 7
0
        def wrapper(update, context):
            user = None
            session = get_session()
            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("Got an update without a message")

                user, _ = get_user(session, message.from_user)
                if user.banned:
                    return

                if private and message.chat.type != "private":
                    message.chat.send_message(
                        "Please do this in a direct conversation with me.")
                    return

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

                session.commit()

                # Respond to user
                if response is not None:
                    message.chat.send_message(response)

            except Exception as e:
                if not ignore_exception(e):
                    if config["logging"]["debug"]:
                        traceback.print_exc()
                    sentry.captureException()

                    locale = "English"
                    if user is not None:
                        locale = user.locale

                    message.chat.send_message(
                        i18n.t("misc.error", locale=locale),
                        parse_mode="markdown",
                        disable_web_page_preview=True,
                    )

                session.close()

            finally:
                session.close()
Ejemplo n.º 8
0
    def wrapper(update, context):
        session = get_session()
        try:
            user, _ = get_user(session, update.chosen_inline_result.from_user)
            if user.banned:
                return

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

            session.commit()
        except Exception as e:
            if not ignore_exception(e):
                if config["logging"]["debug"]:
                    traceback.print_exc()
                    sentry.captureException()

        finally:
            session.close()
Ejemplo n.º 9
0
    def wrapper(update, context):
        user = None
        if context.user_data.get("ban"):
            return

        temp_ban_time = context.user_data.get("temporary-ban-time")
        if temp_ban_time is not None and temp_ban_time >= date.today(
        ) - timedelta(days=1):
            update.callback_query.answer(i18n.t("callback.spam"))
            return

        session = get_session()
        try:
            user, statistic = get_user(session,
                                       update.callback_query.from_user)
            # Cache ban value, so we don't have to lookup the value in our database
            if user.banned:
                context.user_data["ban"] = True
                return

            # Cache temporary-ban time, so we don't have to create a connection to our database
            if statistic.votes > config["telegram"]["max_user_votes_per_day"]:
                update.callback_query.answer(
                    i18n.t("callback.spam", locale=user.locale))
                context.user_data["temporary-ban-time"] = date.today()
                return

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

            session.commit()
        except Exception as e:
            if not ignore_exception(e):
                if config["logging"]["debug"]:
                    traceback.print_exc()
                sentry.captureException()

                locale = "English"
                if user is not None:
                    locale = user.locale
                update.callback_query.answer(
                    i18n.t("callback.error", locale=locale))

        finally:
            session.close()
Ejemplo n.º 10
0
        def wrapper(update, context):
            session = get_session()
            try:
                user = get_user(session, update)
                if not is_allowed(user, update, private=private):
                    return

                if hasattr(update, 'message') and update.message:
                    message = update.message
                elif hasattr(update,
                             'edited_message') and update.edited_message:
                    message = update.edited_message

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

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

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

            # Handle all not telegram relatated exceptions
            except Exception as e:
                if not ignore_exception(e):
                    if config['logging']['debug']:
                        traceback.print_exc()
                    sentry.captureException()

                if send_message:
                    locale = 'English'
                    if user is not None:
                        locale = user.locale
                    session.close()
                    message.chat.send_message(
                        i18n.t('misc.error', locale=locale),
                        parse_mode='markdown',
                        disable_web_page_preview=True,
                    )

            finally:
                session.close()