async def handler(event):
    found = False
    for i, ent in enumerate(event.entities or []):
        if isinstance(ent, types.MessageEntityBotCommand):
            found = True
            event.entities[i] = types.MessageEntityTextUrl(
                ent.offset + 1, ent.length,
                'tg://need_update_for_some_feature')

    if found:
        await asyncio.sleep(1)  # inter-dc issues lol
        try:
            await event.client(
                functions.messages.EditMessageRequest(
                    await event.get_input_chat(),
                    id=event.id,
                    no_webpage=not event.web_preview,
                    message="." + event.raw_text,
                    entities=event.entities))
        except:
            pass
Example #2
0
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
Example #3
0
    def _process_text(self, params):
        if not self.source.text:
            return False

        append_from = self.fwd == FWD_APPEND
        fwd = None
        for att in reversed(self.attachments):
            if append_from and isinstance(att, Fwd):
                fwd = _type_in_list(reversed(self.attachments), Fwd)

                # Fwd.url is already resolved here
                self.source.entities.append(
                    types.MessageEntityUrl(
                        len(self.source.raw_text) + 2,
                        len(fwd.url) - 2))
                self.source.text += '\n\n' + fwd.url
                append_from = False
                continue

            if not isinstance(att, Url):
                continue

            if self.source.text == str(att.url):
                if att.title:
                    params['message'] = att.title
                return True

        if fwd:
            self.attachments.remove(fwd)

        text_urls = []
        for e, inner_text in self.source.get_entities_text():
            # NOTE no MessageEntityMentionName usage examples/documentation available
            # so assume it is same as MessageEntityMention
            if isinstance(
                    e,
                (types.MessageEntityMention, types.MessageEntityMentionName)):
                text_urls.append(
                    types.MessageEntityTextUrl(
                        e.offset, e.length, 'https://t.me/' + inner_text[1:]))
                continue
            if isinstance(e, types.MessageEntityTextUrl):
                text_urls.append(e)

        geo = _type_in_list(self.attachments, Geo)
        if geo:
            self.attachments.remove(geo)
            params['lat'] = geo.lat
            params['long'] = geo.long

        # if this is a rich text
        rich_page = _type_in_list(self.attachments, Page)
        if rich_page:
            params['message'] = rich_page.title
            return False

        if text_urls:
            # add_surrogate/del_surrogate are used by Telethon internally in
            # get_entities_text -> get_inner_text to get correct offsets in unicode
            raw_text = add_surrogate(self.source.raw_text)

            msg = []
            prev = 0
            for tu in text_urls:
                title = del_surrogate(raw_text[prev:(tu.offset + tu.length)])
                # link titles to telegraph photos look like \u200b\u200b
                if _ZERO_CHARS.match(title):
                    continue
                msg.append(title)
                msg.append(' (' + tu.url + ') ')
                prev = tu.offset + tu.length
            msg.append(del_surrogate(raw_text[prev:]))
            del raw_text

            params['message'] = ''.join(msg)
        else:
            params['message'] = self.source.raw_text

        return True