当前位置: 首页>>代码示例>>Python>>正文


Python types.MessageEntityMentionName方法代码示例

本文整理汇总了Python中telethon.tl.types.MessageEntityMentionName方法的典型用法代码示例。如果您正苦于以下问题:Python types.MessageEntityMentionName方法的具体用法?Python types.MessageEntityMentionName怎么用?Python types.MessageEntityMentionName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在telethon.tl.types的用法示例。


在下文中一共展示了types.MessageEntityMentionName方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: append_entity

# 需要导入模块: from telethon.tl import types [as 别名]
# 或者: from telethon.tl.types import MessageEntityMentionName [as 别名]
def append_entity(self, id, entities, message):
        fail = True
        try:
            entity = await self._client.get_input_entity(id)
        except ValueError:
            pass
        else:
            if isinstance(entity, InputPeerSelf):
                entity = await self._client.get_me(True)
            if isinstance(entity, InputPeerUser):
                fail = False
                entities.append(MessageEntityMentionName(len(message),
                                len(str(entity.user_id)), entity.user_id))
                message += str(entity.user_id)
        if fail:
            if not isinstance(id, int):
                id = utils.get_entity_id(id)
            entities.append(MessageEntityCode(len(message), len(str(id))))
            message += str(id)
        return message 
开发者ID:friendly-telegram,项目名称:friendly-telegram,代码行数:22,代码来源:log_group.py

示例2: get_target

# 需要导入模块: from telethon.tl import types [as 别名]
# 或者: from telethon.tl.types import MessageEntityMentionName [as 别名]
def get_target(message, arg_no=0):
    if any(isinstance(ent, MessageEntityMentionName) for ent in (message.entities or [])):
        e = sorted(filter(lambda x: isinstance(x, MessageEntityMentionName),
                          message.entities), key=lambda x: x.offset)[0]
        return e.user_id
    elif len(get_args(message)) > arg_no:
        user = get_args(message)[arg_no]
    elif message.is_reply:
        return (await message.get_reply_message()).from_id
    elif hasattr(message.to_id, "user_id"):
        user = message.to_id.user_id
    else:
        return None
    try:
        ent = await message.client.get_entity(user)
    except ValueError:
        return None
    else:
        if isinstance(ent, User):
            return ent.id 
开发者ID:friendly-telegram,项目名称:friendly-telegram,代码行数:22,代码来源:utils.py

示例3: encode_msg_entities

# 需要导入模块: from telethon.tl import types [as 别名]
# 或者: from telethon.tl.types import MessageEntityMentionName [as 别名]
def encode_msg_entities(entities):
    """
    Encodes a list of MessageEntity into a string so it
    can easily be dumped into e.g. Dumper's database.
    """
    if not entities:
        return None
    parsed = []
    for entity in entities:
        if entity.__class__ in ENTITY_TO_TEXT:
            if isinstance(entity, types.MessageEntityTextUrl):
                extra = ',{}'.format(
                    entity.url.replace(',', '%2c').replace(';', '%3b')
                )
            elif isinstance(entity, types.MessageEntityMentionName):
                extra = ',{}'.format(entity.user_id)
            else:
                extra = ''
            parsed.append('{},{},{}{}'.format(
                ENTITY_TO_TEXT[type(entity)],
                entity.offset, entity.length, extra
            ))
    return ';'.join(parsed) 
开发者ID:expectocode,项目名称:telegram-export,代码行数:25,代码来源:utils.py

示例4: decode_msg_entities

# 需要导入模块: from telethon.tl import types [as 别名]
# 或者: from telethon.tl.types import MessageEntityMentionName [as 别名]
def decode_msg_entities(string):
    """
    Reverses the transformation made by ``utils.encode_msg_entities``.
    """
    if not string:
        return None
    parsed = []
    for part in string.split(';'):
        split = part.split(',')
        kind, offset, length = split[0], int(split[1]), int(split[2])
        if kind in TEXT_TO_ENTITY:
            if kind == 'texturl':
                parsed.append(types.MessageEntityTextUrl(
                    offset, length, split[-1]
                ))
            elif kind == 'mentionname':
                parsed.append(types.MessageEntityMentionName(
                    offset, length, int(split[-1])
                ))
            else:
                parsed.append(TEXT_TO_ENTITY[kind](offset, length))
    return parsed 
开发者ID:expectocode,项目名称:telegram-export,代码行数:24,代码来源:utils.py

示例5: get_user_from_msg

# 需要导入模块: from telethon.tl import types [as 别名]
# 或者: from telethon.tl.types import MessageEntityMentionName [as 别名]
def get_user_from_msg(event: NewMessage.Event) -> Union[int, str, None]:
    """Get a user's ID or username from the event's regex pattern match"""
    user = None
    match = event.matches[0].group(1)

    if match == "this":
        match = str(event.chat.id)

    if event.entities:
        for entity in event.entities:
            if isinstance(entity, types.MessageEntityMentionName):
                return entity.user_id
            elif isinstance(entity, types.MessageEntityMention):
                offset = entity.offset
                length = entity.length
                maxlen = offset + length
                return event.text[offset:maxlen]

    if match:
        if isinstance(match, str) and match.isdigit():
            user = int(match.strip())
        else:
            user = match.strip()

    return user 
开发者ID:TG-UserBot,项目名称:TG-UserBot,代码行数:27,代码来源:ids.py

示例6: test_dump_msg_entities

# 需要导入模块: from telethon.tl import types [as 别名]
# 或者: from telethon.tl.types import MessageEntityMentionName [as 别名]
def test_dump_msg_entities(self):
        """Show that entities are correctly parsed and stored"""
        message = types.Message(
            id=1,
            to_id=types.PeerUser(321),
            date=datetime.now(),
            message='No entities'
        )
        dumper = Dumper(self.dumper_config)
        fmt = BaseFormatter(dumper.conn)

        # Test with no entities
        dumper.dump_message(message, 123, None, None)
        dumper.commit()
        assert not next(fmt.get_messages_from_context(123, order='DESC')).formatting

        # Test with many entities
        text, entities = markdown.parse(
            'Testing message with __italic__, **bold**, inline '
            '[links](https://example.com) and [mentions](@hi), '
            'as well as `code` and ``pre`` blocks.'
        )
        entities[3] = types.MessageEntityMentionName(
            entities[3].offset, entities[3].length, 123
        )
        message.id = 2
        message.date -= timedelta(days=1)
        message.message = text
        message.entities = entities
        dumper.dump_message(message, 123, None, None)
        dumper.commit()
        msg = next(fmt.get_messages_from_context(123, order='ASC'))
        assert utils.decode_msg_entities(msg.formatting) == message.entities 
开发者ID:expectocode,项目名称:telegram-export,代码行数:35,代码来源:tests.py

示例7: get_user

# 需要导入模块: from telethon.tl import types [as 别名]
# 或者: from telethon.tl.types import MessageEntityMentionName [as 别名]
def get_user(event):
    if event.reply_to_msg_id:
        previous_message = await event.get_reply_message()
        replied_user = await event.client(GetFullUserRequest(previous_message.from_id))
    else:
        user = event.pattern_match.group(1)

        if user.isnumeric():
            user = int(user)

        if not user:
            self_user = await event.client.get_me()
            user = self_user.id

        if event.message.entities is not None:
            probable_user_mention_entity = event.message.entities[0]

            if isinstance(probable_user_mention_entity, MessageEntityMentionName):
                user_id = probable_user_mention_entity.user_id
                replied_user = await event.client(GetFullUserRequest(user_id))
                return replied_user
        try:
            user_object = await event.client.get_entity(user)
            replied_user = await event.client(GetFullUserRequest(user_object.id))

        except (TypeError, ValueError):
            await event.edit("`I don't slap strangers !!`")
            return None

    return replied_user 
开发者ID:mkaraniya,项目名称:BotHub,代码行数:32,代码来源:slap.py

示例8: get_user

# 需要导入模块: from telethon.tl import types [as 别名]
# 或者: from telethon.tl.types import MessageEntityMentionName [as 别名]
def get_user(event):
    """ Get the user from argument or replied message. """
    if event.reply_to_msg_id:
        previous_message = await event.get_reply_message()
        replied_user = await event.client(GetFullUserRequest(previous_message.from_id))
    else:
        user = event.pattern_match.group(1)

        if user.isnumeric():
            user = int(user)

        if not user:
            self_user = await event.client.get_me()
            user = self_user.id

        if event.message.entities is not None:
            probable_user_mention_entity = event.message.entities[0]

            if isinstance(probable_user_mention_entity, MessageEntityMentionName):
                user_id = probable_user_mention_entity.user_id
                replied_user = await event.client(GetFullUserRequest(user_id))
                return replied_user
        try:
            user_object = await event.client.get_entity(user)
            replied_user = await event.client(GetFullUserRequest(user_object.id))

        except (TypeError, ValueError):
            await event.edit("`I don't slap aliens, they ugly AF !!`")
            return None

    return replied_user 
开发者ID:Dark-Princ3,项目名称:X-tra-Telegram,代码行数:33,代码来源:memes.py

示例9: get_full_user

# 需要导入模块: from telethon.tl import types [as 别名]
# 或者: from telethon.tl.types import MessageEntityMentionName [as 别名]
def get_full_user(event):
    if event.reply_to_msg_id:
        previous_message = await event.get_reply_message()
        if previous_message.forward:
            replied_user = await event.client(
                GetFullUserRequest(
                    previous_message.forward.from_id or previous_message.forward.channel_id
                )
            )
            return replied_user, None
        else:
            replied_user = await event.client(
                GetFullUserRequest(
                    previous_message.from_id
                )
            )
            return replied_user, None
    else:
        input_str = None
        try:
            input_str = event.pattern_match.group(1)
        except IndexError as e:
            return None, e
        if event.message.entities is not None:
            mention_entity = event.message.entities
            probable_user_mention_entity = mention_entity[0]
            if isinstance(probable_user_mention_entity, MessageEntityMentionName):
                user_id = probable_user_mention_entity.user_id
                replied_user = await event.client(GetFullUserRequest(user_id))
                return replied_user, None
            else:
                try:
                    user_object = await event.client.get_entity(input_str)
                    user_id = user_object.id
                    replied_user = await event.client(GetFullUserRequest(user_id))
                    return replied_user, None
                except Exception as e:
                    return None, e
        elif event.is_private:
            try:
                user_id = event.chat_id
                replied_user = await event.client(GetFullUserRequest(user_id))
                return replied_user, None
            except Exception as e:
                return None, e
        else:
            try:
                user_object = await event.client.get_entity(int(input_str))
                user_id = user_object.id
                replied_user = await event.client(GetFullUserRequest(user_id))
                return replied_user, None
            except Exception as e:
                return None, e 
开发者ID:mkaraniya,项目名称:BotHub,代码行数:55,代码来源:whois.py


注:本文中的telethon.tl.types.MessageEntityMentionName方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。