digester.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:HashDigestBot 作者: wagnerluis1982 项目源码 文件源码
def feed(self, message: telegram.Message) -> bool:
        """Give a telegram message to search for a tag

        The message will be added to the digest if has a tag or is a reply to a
        previous message with tag.

        Returns:
            bool: Indicate if the message was added to the digest
        """
        # Verify if message is allowed to digest
        if not self.db.exists(ConfigChat, chat_id=message.chat_id):
            return False

        # Extract tag from the message
        text_tag = extract_hashtag(message.text)

        # Check early if the message has a tag or can be a reply to a tagged message
        if not (text_tag or message.reply_to_message):
            return False

        # Get the user who sent the message.
        hashuser = self.db.get(HashUser, id=message.from_user.id)
        if not hashuser:
            hashuser = HashUser(
                id=message.from_user.id,
                friendly_name=self.make_friendly_name(message.from_user),
                username=message.from_user.username,
            )

        # A tag was found in the message?
        reply_id = None
        if text_tag:
            # Add a tag entry if necessary
            tag_id = self.db.generate_tag_id(text_tag)
            if not self.db.exists(HashTag, id=tag_id):
                tag = HashTag(id=tag_id, shapes={text_tag})
            # Or fetch tag and add a possible new shape
            else:
                tag = self.db.get(HashTag, id=tag_id)
                tag.shapes.add(text_tag)
        # Otherwise, the message may be a reply to a previous tagged message.
        else:
            reply_id = message.reply_to_message.message_id
            tag = self.db.get_message_tag(reply_id)
            if not tag:
                return False

        # Create a HashMessage from the telegram message
        hashmessage = HashMessage(
            id=message.message_id,
            date=message.date,
            text=message.text,
            chat_id=message.chat_id,
            reply_to=reply_id,
        )
        hashmessage.tag = tag
        hashmessage.user = hashuser

        # Add the hashmessage to the database
        self.db.insert(hashmessage)
        return True
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号