import slackweb

slack = slackweb.Slack(
    url="??????"
)
# 方法1
attachments = []
attachment = {
    "pretext": "Email Validation failed",
    "title": "Email address:",
    "text": "apsvvfb",
}
attachments.append(attachment)
# 方法2
attachments = [
    {
        "title": "Slack API Documentation",
        "text": "Optional text that appears within the attachment",
        "fields": [
            {
                "title": "Priority",
                "value": "High",
                "short": False
            }
        ]
    },
    {
        "title": "二つ目",
        "text": "second one",
    },
    {
        "title": "三つ目",
        "text": "third one",
    }
]
# 方法3
attachments = [
    {
        "title": "Danny Torrence left a 1 star review for your property.",
        "fields": [
            {
                "value": "abc"
            },
            {
                "value": "bdd"
            },
        ],
    }
]
# 方法4
invalid_emails = []
for user_email in user_emails:
	# email validation failed.
	if not check(user_email):
		print("Email validation failed.")

invalid_emails.append(dict(value=user_email))
attachments = [
        {
            "pretext": "Email Validation failed",
            "title": "Email address:",
            "fields": invalid_emails,
        }
]
#发送消息给slack
slack.notify(
        channel=config.slack_channel,
        username=config.slack_username,
        icon_emoji=":ghost",
        attachments=attachments,
)

检查邮件地址是否规范

def check(email):
    """Python program to validate an Email

    References:
        - https://www.geeksforgeeks.org/check-if-email-address-valid-or-not-in-python/
    """
    # Make a regular expression for validating an Email
    regex = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"

    if re.fullmatch(regex, email):
        print("Valid Email")
        return True
    else:
        print("Invalid Email")
        return False

Logo

GitCode AI社区是一款由 GitCode 团队打造的智能助手,AI大模型社区、提供国内外头部大模型及数据集服务。

更多推荐