示例#1
0
def delete_message(message: telegram.Message):
    try:
        message.delete()
    except TelegramError:
        try:
            message.reply_text('删除消息失败,请检查一下 bot 的权限设置')
        except TelegramError:
            pass
示例#2
0
def delete_message(message: telegram.Message):
    _ = partial(get_by_user, user=message.from_user)

    try:
        message.delete()
    except TelegramError:
        try:
            message.reply_text(_(Text.DELETE_FAIL))
        except TelegramError:
            pass
示例#3
0
def _delete_after(delay: int, message: Message):
    """Delete message after specified seconds.

    Args:
        delay (int): Delay in secs
        message (Message): Message to delete
    """
    time.sleep(delay)
    message.delete()
    logger.debug(f'Message {message.message_id} in {message.chat.type} chat {message.chat.id} deleted:\n{message.text}')
    return
示例#4
0
def handle_edit(bot, chat, job_queue, message: telegram.Message, text: str):
    target = message.reply_to_message
    if not isinstance(target, telegram.Message):
        return error_message(message, job_queue, '回复需要编辑的记录')

    assert isinstance(message.from_user, telegram.User)
    user_id = message.from_user.id
    log = Log.objects.filter(message_id=target.message_id).first()
    if log is None:
        error_message(message, job_queue, '这条记录不存在于数据库')
    elif log.user_id == user_id:
        handle_say(bot, chat, job_queue, message, log.character_name, text, edit_log=log)
        message.delete()
    else:
        error_message(message, job_queue, '你没有编辑这条消息的权限')
示例#5
0
def try_delete_message(message: Message):
    try:
        return message.delete()
    except telegram.error.BadRequest:
        # deletion failed most likely due to lack of permissions in the chat
        # we can safely ignore it
        return False
示例#6
0
def handle_delete(message: telegram.Message, job_queue):
    target = message.reply_to_message
    if isinstance(target, telegram.Message):
        user_id = message.from_user.id
        log = Log.objects.filter(message_id=target.message_id).first()
        if log is None:
            error_message(message, job_queue, '这条记录不存在于数据库')
        elif log.user_id == user_id or is_gm(message.chat_id, user_id):
            log.deleted = True
            log.save()
            target.delete()
            message.delete()
        else:
            error_message(message, job_queue, '你没有删掉这条记录的权限')
    else:
        error_message(message, job_queue, '回复需要删除的记录')
示例#7
0
文件: mode.py 项目: vldc-hq/vldc-bot
def _remove_message_after(message: Message, job_queue: JobQueue, seconds: int):
    logger.debug(
        "Scheduling cleanup of message %s \
                   in %d seconds",
        message,
        seconds,
    )
    job_queue.run_once(lambda _: message.delete(), seconds, context=message.chat_id)
示例#8
0
文件: mode.py 项目: Grawl/vldc-bot
def _remove_message_after(message: Message, context: CallbackContext,
                          seconds: int):
    logger.debug(
        f"Scheduling cleanup of message {message.message_id} in {seconds} seconds"
    )
    context.job_queue.run_once(lambda _: message.delete(),
                               seconds,
                               context=message.chat_id)
示例#9
0
def command_handler(message: telegram.Message):
    _message = message.text.split(' ')
    command = _message[0].replace('/', '')
    subcommand = _message[1:]

    if command == 'start':
        command_start(message, subcommand)
    elif command == 'status':
        command_status(message, subcommand)
    elif command == 'subscribe':
        command_subscribe(message, subcommand)
    elif command == 'unsubscribe':
        command_unsubscribe(message, subcommand)
    elif command == 'products':
        command_products(message, subcommand)
    elif command == 'price':
        command_price(message, subcommand)

    message.delete()
示例#10
0
    def __delete_message_and_reply(message: Message = None):

        if message is not None:
            if message.reply_to_message is not None:
                message.reply_to_message.delete()
            message.delete()
示例#11
0
def try_to_delete(message: Message) -> bool:
    try:
        return message.delete()
    except (BadRequest, Unauthorized):
        return False
def _delete_notification_if_full_name_too_long(members,
                                               message: telegram.Message):
    for member in members:
        if len(member.full_name) > FULL_NAME_TOO_LONG:
            message.delete()
示例#13
0
def handle_say(bot: telegram.Bot, chat, job_queue, message: telegram.Message,
               name: str, text: str, edit_log=None, with_photo=None):
    user_id = message.from_user.id
    gm = is_gm(message.chat_id, user_id)

    # process input text
    def name_resolve(match):
        username = match.group(1)
        name_result = get_name_by_username(message.chat_id, username)
        if not name_result:
            return '@{}'.format(username)
        return '<b>{}</b>'.format(name_result)

    text = USERNAME_REGEX.sub(name_resolve, text)
    kind = LogKind.NORMAL.value

    if is_empty_message(text):
        error_message(message, job_queue, '不能有空消息')
        return
    elif ME_REGEX.search(text):
        send_text = ME_REGEX.sub('<b>{}</b>'.format(name), text)
        content = send_text
        kind = LogKind.ME.value
    else:
        send_text = '<b>{}</b>: {}'.format(name, text)
        content = text
    symbol = get_symbol(message.chat_id, user_id)
    send_text = symbol + send_text
    # on edit
    if edit_log:
        assert isinstance(edit_log, Log)
        edit_log.content = content
        edit_log.kind = kind
        edit_log.save()
        bot.edit_message_text(send_text, message.chat_id, edit_log.message_id, parse_mode='HTML')
        message.delete()
        return

    # send message or photo
    reply_to_message_id = None
    reply_log = None
    target = message.reply_to_message
    if isinstance(target, telegram.Message):
        reply_to_message_id = target.message_id
        reply_log = Log.objects.filter(chat=chat, message_id=reply_to_message_id).first()
    if isinstance(with_photo, telegram.PhotoSize):
        sent = message.chat.send_photo(
            photo=with_photo,
            caption=send_text,
            reply_to_message_id=reply_to_message_id,
            parse_mode='HTML',
        )
    else:
        sent = message.chat.send_message(
            send_text,
            reply_to_message_id=reply_to_message_id,
            parse_mode='HTML',
        )

    if chat.recording:
        # record log
        created_log = Log.objects.create(
            message_id=sent.message_id,
            chat=chat,
            user_id=user_id,
            user_fullname=message.from_user.full_name,
            kind=kind,
            reply=reply_log,
            character_name=name,
            content=content,
            gm=gm,
            created=message.date,
        )
        # download and write photo file
        if isinstance(with_photo, telegram.PhotoSize):
            created_log.media.save('{}.jpeg'.format(uuid.uuid4()), io.BytesIO(b''))
            media = created_log.media.open('rb+')
            with_photo.get_file().download(out=media)
            media.close()
    message.delete()
示例#14
0
def del_msg(msg: Message) -> None:
    try:
        msg.delete()
    except TelegramError:
        pass