The explanation of how to register Slack API and how to use Slack API is omitted. If you haven't done so yet, please register from the URL below. https://api.slack.com/
https://qiita.com/kou_pg_0131/items/56dd81f2f4716ca292ef
Use the Slack API to remove messages from a particular channel, a particular user, that have been posted for more than 3 days.
Python3.8.5
(1) Use the Slack API to get the message of a specific channel and get the time stamp of the message in it. (2) Use the Slack API to delete messages that are older than 3 days from the message time stamps.
-Getting the time stamp of a message on a specific channel is conversations.history
https://api.slack.com/methods/conversations.history
-Delete the message is chat.delete
https://api.slack.com/methods/chat.delete
#Import required libraries
import requests
from datetime import datetime
import re
#Get a message by specifying a channel
#Web API URL
url = "https://slack.com/api/conversations.history"
#Workspace token
token = "Workspace token"
#ID of the target channel
channel = "ID of the target channel"
#A list for storing the time stamp of each message
timestamplist = []
#Make information dictionary type
payload = {"token": token, "channel": channel}
#Get channel information
response = requests.get(url, params=payload)
#Convert to json format
message_json = response.json()
#Store the message time stamp in the list
for m in message_json["messages"]:
try:
#Create a list of user list(There is duplication)
timestamplist.append(m["ts"])
#If there is no ts key, a KeyError will occur, so exception handling
except KeyError:
pass
print(timestamplist)
#Get Timestamp for Today's Date
current_ts = int(datetime.now().strftime('%s'))
#Delete a message for a specific user on a channel
for ts in timestamplist:
#Web API URL
url = "https://slack.com/api/chat.delete"
#Workspace token
token = "Workspace token"
#ID of the target channel
channel = "ID of the target channel"
#3 days ago
term = 60 * 60 * 24 * 3
#Time stamp
ts = ts
#User specification
as_user = "User ID"
#Delete message if time stamp is more than 3 days old
if current_ts - int(re.sub(r'\.\d+$', '', ts)) > term:
payload = {"token": token, "channel": channel,
"ts": ts, "as_user": as_user}
response = requests.post(url, params=payload)
json = response.json()
as_user In the above source code, by passing the ID of a specific user to the variable as_user, I am trying to delete the message of a specific user within the target period. If no user is specified, the messages of all users within the target period will be deleted.
In Python, you can represent a time interval by using "seconds * minutes * hours * days". For example, one week is "60 * 60 * 24 * 7" and 12 hours 30 minutes 40 seconds is "40 * 30 * 12". By changing this number, you can change the range of messages to be deleted.
A standard library for processing regular expressions in Python. You can extract, replace, and divide character strings using regular expression patterns.
You can replace the matched part with another character string.
The usage is as follows.
sub (r'character string to be replaced','character string after replacement', target object name)
In the case of re.sub (r'\. \ D + $','', ts)
this time
It means "in ts, if there is a half-width number from the dot to the end, it is converted to empty (synonymous with deleting)."
ยท Python documentation https://docs.python.org/ja/3/library/re.html -How to use Python's regular expression module re (match, search, sub, etc.) https://note.nkmk.me/python-re-match-search-findall-etc/
https://michimani.net/post/programming-delete-old-slack-messages/
Recommended Posts