-->

Create and get new channel incoming webhook in sla

2020-07-24 04:27发布

问题:

I just created a channel via Slack Api using channels.create method. How do I add incoming webhook and get the URL programmatically? I have other tools that will use it further.

回答1:

You can not create new incoming webhooks programmatically, but you don't have to. Just override the channel property on an existing incoming webhook for your current Slack team to use the new channel.

Example:

{
  "text": "This is a line of text.\nAnd this is another one.", 
  "channel": "channel-name"
}

Note that this will only work for incoming webhooks defined via custom integrations, but not for those defined as part of a Slack app.



回答2:

data = {
    "attachments": [
        {
            "author_name": "[Alert] - A Jenkins Job is Already Running!",
            "color": "#36a64f",
            "title": "Android Jenkins Job",
            "title_link": "http://xx.xxx.xxx.xxx/job/Mobile_Regression/",
            "footer": "Android Build Attempted",
            "ts": time.time()
        }
    ],
    "channel": "#channel"
}
json_params_encoded = json.dumps(data)
slack_response = requests.post(url=hook_url, data=json_params_encoded, headers={"Content-type": "application/json"})