The requests to be handled are as follows.
-chat.postMessage (Official Document) -users.profile.set (Official Document) -users.setPresence (Official Document) -users.getPresence (Official Document) -users.list (Official Document)
You can also refer to other requests in the Official Documents.
** Two types of OAuth tokens ** slackapi->YourApp -> Install app When you open, you will find two tokens.
The tokens required differ depending on the type of request, so I wanted to be able to simplify that as well.
The location to define is arbitrary, but this time I stored it in ".env".
.env
SLACK_OAUTH_TOKEN='xoxp-{YOUR_TOKEN}'
SLACK_USER_TOKEN='xoxb-{YOUR_TOKEN}'
You need to import ** dotenv ** when using it in a python file.
slackapi_functions.py
from dotenv import load_dotenv
import os
load_dotenv()
slack_oauth_token = os.environ['SLACK_OAUTH_TOKEN']
slack_user_token = os.environ['SLACK_USER_TOKEN']
Also import to use slackapi!
slackapi_functions.py
import requests #To send a request
import json #Receive the response as Json.
from slack import WebClient #Slackapi Web Client
slackapi_functions.py
class SlackFunctions:
def __init__(self):
self._headers = {'Content-Type': 'application/json'}
chat.postMessage
slackapi_functions.py
def post_message(self, channel, message):
params = {"token": slack_user_token,
"channel": channel, "text": message}
r = requests.post('https://slack.com/api/chat.postMessage',
headers=self._headers, params=params)
This is a request to send a message.
Send to \ # general → post_message (" # general "," hello! ")` ` Send to user by DM →
post_message ("{USERS_ID} "," Wake UP! ")` ``
users.profile.set
slackapi_functions.py
def profile_set(self, text, emoji):
params = {
"token": slack_oauth_token,
"profile": json.dumps({
"status_text": text,
"status_emoji": emoji
})
}
r = requests.post('https://slack.com/api/users.profile.set',
params=params)
A request to set custom status text and emoji. Since there are status_text and status_emoji in the variable called profile, you can use it as it is when setting only those two.
users.setPresence
slackapi_functions.py
def set_presence(self, status): # statue: 'away' OR 'auto'
params = {
"token": slack_user_token,
"presence": status
}
r = requests.post('https://slack.com/api/users.setPresence',
params=params)
This is a request as to whether it is actually active or away.
You can force it to be away``, but you cannot force it to be` `active
.
So the only argument to presence is `" away "``` or
`" auto "```.
users.getPresence
slackapi_functions.py
def get_presence(self, user):
params = {"token": slack_user_token, "user":user}
r = requests.post('https://slack.com/api/users.getPresence',
headers=self._headers, params=params)
return r.json()
A request that returns the user's status (active or away). I wanted to use the status that came back, so I gave it a return price.
users.list
slackapi_functions.py
def users_list(self):
params = {"token": slack_user_token}
r = requests.post(' https://slack.com/api/users.list',
headers=self._headers, params=params)
return r.json()
It is a request to return information of all users (including BOT and plug-in application) in workspace. Since there is a lot of information, I made it a function that returns after processing the response.
def users_list(self):
params = {"token": slack_user_token}
r = requests.post(' https://slack.com/api/users.list',
headers=self._headers, params=params)
rjson = r.json()
id_list = []
name_list = []
for i in range(0, len(rjson["members"])):
id_list.append(rjson["members"][i]["id"])
name_list.append(rjson["members"][i]["profile"]["display_name"])
return id_list, name_list
First, import `` `slackapi_functions.py```
your_slackapp.py
import slackapi_functions
SF = slackapi_functions.SlackFunctions()
After that, you can use it with the Class name and Function name!
your_slackapp.py
SF.post_message("#international-team", "Hello, this is your app!")
SF.change_status_message("I am focused!", :computer:)
SF.set_presence('away')
user_status = SF.get_presence(YOUR_ID)
accounts_info = SF.users_list()
Recommended Posts