slackbot.py 文件源码

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

项目:docpub 作者: mcclatchy 项目源码 文件源码
def slackbot(text):
    """
    Posts messages to Slack channel based on environment
    """

    slack = Slacker(SLACK_TOKEN)
    username = "DocPubBot"
    icon_url = "http://autobinaryrobots.com/wp-content/uploads/2016/11/robot.png"

    ## map environment to related Slack channel
    env_channels = {
        "local": "#docpublocal",
        "test": "#docpubtest",
        "prod": "#docpubprod"
    }

    ## set channel based on the environment
    channel = env_channels[DOCPUBENV]
    # channel = os.environ["MCCELECTIONSENV"]

    ## uses try statement in order to avoid requests package error:
        # "Max retries exceeded with url"
    try:
        slack.chat.post_message(channel, text, username=username, link_names=True, icon_url=icon_url)

        # return "Messaged posted: %s" % (text)

    except:
        print("WARNING: An error occured when trying to send the text to Slack.")

    ## outputs to command line so you can follow along/log there, especially when working locally
    print(text)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号