2

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.

Sergey
  • 978
  • 2
  • 11
  • 33

2 Answers2

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.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • Unfortunately, this will not work in my case. What I needed is to have exclusive access to a channel. Worked around my problem with a pool of channels that have everything set-up and simple locking mechanism. – Sergey May 09 '17 at 18:55
  • @Sergey Welll, that is an additional requirement. For that I would just create new private channels with [groups.create](https://api.slack.com/methods/groups.create). And then invite your user to the new private channel. You will have exclusive access. And you will be able to access it with a webhook with the trick mentioned above. – Erik Kalkoken May 09 '17 at 22:39
0
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"})
Shivam Bharadwaj
  • 1,864
  • 21
  • 23