-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32_slack_message.py
More file actions
38 lines (30 loc) · 1.02 KB
/
32_slack_message.py
File metadata and controls
38 lines (30 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import requests
import os
import dotenv
dotenv.load_dotenv()
dotenv.load_dotenv(".env.local", override=True)
SLACK_USER_OAUTH_TOKEN = os.getenv("SLACK_USER_OAUTH_TOKEN")
def send_message_to_self(text):
# Get your user ID using auth.test
auth_resp = requests.post(
"https://slack.com/api/auth.test",
headers={"Authorization": f"Bearer {SLACK_USER_OAUTH_TOKEN}"},
timeout=10,
).json()
user_id = auth_resp["user_id"]
# Open a DM with yourself
dm = requests.post(
"https://slack.com/api/conversations.open",
headers={"Authorization": f"Bearer {SLACK_USER_OAUTH_TOKEN}"},
json={"users": user_id},
timeout=10,
).json()
channel_id = dm["channel"]["id"]
# Send the message
requests.post(
"https://slack.com/api/chat.postMessage",
headers={"Authorization": f"Bearer {SLACK_USER_OAUTH_TOKEN}"},
json={"channel": channel_id, "text": text},
timeout=10,
)
send_message_to_self("✅ Slack test message from my script!")