It's for people who are using Slack for free and have reached the integration limit and have become sick. You want to try various integrations even in the free frame! ?? It's a waste to use a valuable integration frame for Incoming WebHooks! It is a poor spirit w
https://api.slack.com/web
It will be the item of ʻAuthentication` at the bottom of the above page.
Click Create token
in the image to generate a token for each user.
Make a note of it as you will use it later.
Create a channel from Slack of the team that issued the token.
This time, suppose you created a Channel called sample
.
You can also POST to an existing channel, so you don't have to create a new one.
You're ready to go!
The following page has a list of API methods.
https://api.slack.com/methods
This time, we will use the chat.postMessage method in this list.
sample.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
#proxy settings, if any
PROXIES = {
"http": "http://hogehoge:port/",
"https": "https://fugafuga:port/",
}
class SlackWrapper:
#slack
__token = 'your_slack_token' #Write the token you got earlier here
__channel = '#sample' #Channel name you want to POST
__postSlackUrl = 'https://slack.com/api/chat.postMessage' #This is fixed
__icon_url = 'icon URL' #Specify the URL of the icon to post to Slack.
__username = 'sample' #Username to post to Slack
def __init__(self):
pass
def post(self, posttext):
params = {'token': self.__token,
'channel': self.__channel ,
'text':posttext,
'icon_url': self.__icon_url,
'username':self.__username,
'unfurl_links': 'false'
}
#POST to Slack
r = requests.post(self.__postSlackUrl, params=params, proxies=PROXIES)
#When there is no proxy
# r = requests.post(self.__postSlackUrl, params=params)
if __name__ == '__main__':
slack = SlackWrapper()
slack.post('Hello! Slack!')
Now you can POST to the specified channel without using Incoming WebHooks! You did it!
Recommended Posts