예제 #1
0
def db_read(update, context) -> 'telegram.Message':
    u = User.get_user(update, context)
    user_id = extract_user_data_from_update(update)['user_id']

    if not u.is_admin:
        return update.message.reply_text(static_text.no_access)

    text = update.message.text.replace(f'/read_translation', '').strip()
    if text == "":
        text = static_text.read_translation_help
    else:
        if text.isdigit():
            tr = Translation.objects.filter(id=int(text)).first()
        else:
            tr = Translation.objects.filter(native_text=text).first()
        if tr is None:
            text = static_text.nothing_found
        else:
            text = f"#{tr.id} : \n" \
                   f"{tr.native_text} - {tr.translated_text}"

    return context.bot.send_message(
        text=text,
        chat_id=user_id,
    )
예제 #2
0
파일: admin.py 프로젝트: pznkv/pythonlabs
def broadcast_command_with_message(update, context) -> 'telegram.Message':
    """ Type /broadcast <some_text>. Then check your message in Markdown format and broadcast to users."""
    u = User.get_user(update, context)
    user_id = extract_user_data_from_update(update)['user_id']

    if not u.is_admin:
        return update.message.reply_text(static_text.no_access)

    text = f"{update.message.text.replace(f'/broadcast', '', 1).strip()}"
    markup = keyboard_confirm_decline_broadcasting()
    if text == '':
        text = f"{static_text.empty_message}\n{static_text.broadcast_help}"
        markup = None

    try:
        return context.bot.send_message(text=text,
                                        chat_id=user_id,
                                        parse_mode=telegram.ParseMode.MARKDOWN,
                                        reply_markup=markup)
    except telegram.error.BadRequest as e:
        place_where_mistake_begins = re.findall(r"offset (\d{1,})$", str(e))
        text_error = static_text.error_with_markdown
        if len(place_where_mistake_begins):
            text_error += f"{static_text.specify_word_with_error}'{text[int(place_where_mistake_begins[0]):].split(' ')[0]}'"
        return context.bot.send_message(text=text_error, chat_id=user_id)
예제 #3
0
 def handler(update, context, *args, **kwargs):
     user_id = extract_user_data_from_update(update)['user_id']
     action = f"{func.__module__}.{func.__name__}" if not action_name else action_name
     UserActionLog.objects.create(user_id=user_id,
                                  action=action,
                                  created_at=timezone.now())
     return func(update, context, *args, **kwargs)
예제 #4
0
 def handler(update, context, *args, **kwargs):
     user_id = extract_user_data_from_update(update)['user_id']
     action = f"{func.__module__}.{func.__name__}" if not action_name else action_name
     try:
         UserActionLog.objects.create(user_id=user_id,
                                      action=action,
                                      created_at=timezone.now())
     except django.db.utils.IntegrityError:
         pass
     return func(update, context, *args, **kwargs)
예제 #5
0
def secret_level(update, context): #callback_data: SECRET_LEVEL_BUTTON variable from manage_data.py
    """ Pressed 'secret_level_button_text' after /start command"""
    user_id = extract_user_data_from_update(update)['user_id']
    text = unlock_secret_room.format(
        user_count=User.objects.count(),
        active_24=User.objects.filter(updated_at__gte=timezone.now() - datetime.timedelta(hours=24)).count()
    )

    context.bot.edit_message_text(
        text=text,
        chat_id=user_id,
        message_id=update.callback_query.message.message_id,
        parse_mode=telegram.ParseMode.MARKDOWN
    )
예제 #6
0
    def get_user_and_created(cls, update, context):
        """ python-telegram-bot's Update, Context --> User instance """
        data = utils.extract_user_data_from_update(update)
        u, created = cls.objects.update_or_create(user_id=data["user_id"],
                                                  defaults=data)

        if created:
            if context is not None and context.args is not None and len(
                    context.args) > 0:
                payload = context.args[0]
                if str(payload).strip() != str(
                        data["user_id"]).strip():  # you can't invite yourself
                    u.deep_link = payload
                    u.save()

        return u, created
예제 #7
0
def db_create(update, context) -> 'telegram.Message':
    u = User.get_user(update, context)
    user_id = extract_user_data_from_update(update)['user_id']

    if not u.is_admin:
        return update.message.reply_text(static_text.no_access)

    text = update.message.text.replace(f'/create_translation', '').strip()

    words = [w.strip() for w in text.split(':') if w.strip() != ""]
    if len(words) != 2:
        text = static_text.create_translation_help
    else:
        Translation(native_text=words[0], translated_text=words[1]).save()
        text = f"{words[0]} - {words[1]}"

    return context.bot.send_message(
        text=text,
        chat_id=user_id,
    )
예제 #8
0
def db_update(update, context) -> 'telegram.Message':
    u = User.get_user(update, context)
    user_id = extract_user_data_from_update(update)['user_id']

    if not u.is_admin:
        return update.message.reply_text(static_text.no_access)

    text = update.message.text.replace(f'/update_translation', '').strip()

    words = [w.strip() for w in text.split(':') if w.strip() != ""]
    if len(words) != 3 or not words[0].isdigit():
        text = static_text.update_translation_help
    else:
        tr = Translation.objects.filter(id=int(words[0])).first()
        tr.native_text = words[1]
        tr.translated_text = words[2]
        tr.save()
        text = str(tr)

    return context.bot.send_message(
        text=text,
        chat_id=user_id,
    )