Skip to content

Commit 7a16a8b

Browse files
Add files via upload
Slackbot using slackapi and bolt framework
1 parent 39559e3 commit 7a16a8b

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

0.1 SlackBot/bolt.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
3+
import os
4+
from slack_bolt import App
5+
from slack_bolt.adapter.socket_mode import SocketModeHandler
6+
7+
8+
SLACK_APP_TOKEN = "xapp-1-A03KSSHPL65-3654924430838-d4c7cca46549ea8e53b9217d8cd4eaaf866972893e3dcf8d4e3547cd0484982e" #basic info -app level token - add scope
9+
SLACK_BOT_TOKEN = 'xoxb-3657324970245-3662649764932-KCBEPXF1GwOrbVxjui3V2gdi' #oauth bot token
10+
# Initializes your app with your bot token and socket mode handler
11+
app = App(token=SLACK_BOT_TOKEN)
12+
13+
# 1. Listens to incoming messages that contain "hello"
14+
15+
# @app.message("hello")
16+
# def message_hello(message, say):
17+
# # say() sends a message to the channel where the event was triggered
18+
# say(f"Hey there <@{message['user']}>!")
19+
20+
21+
#2 - Sending and responding to actions
22+
23+
@app.message('hello')
24+
def message_hello(message, say):
25+
# say() sends a message to the channel where the event was triggered
26+
say(
27+
blocks=[
28+
{
29+
"type": "section",
30+
"text": {"type": "mrkdwn", "text": f"Hey there <@{message['user']}>!"},
31+
"accessory": {
32+
"type": "button",
33+
"text": {"type": "plain_text", "text": "Click Me"},
34+
"action_id": "button_click"
35+
}
36+
}
37+
],
38+
text=f"Hey there <@{message['user']}>!"
39+
)
40+
41+
42+
@app.action("button_click")
43+
def action_button_click(body, ack, say):
44+
# Acknowledge the action
45+
ack()
46+
say(f"<@{body['user']['id']}> clicked the button")
47+
48+
49+
@app.message(":wave:")
50+
def say_hello(message, say):
51+
user = message['user']
52+
say(f"Hi there, <@{user}>!")
53+
54+
#Schedule a message - calling chat_schedulemssage api
55+
@app.message("wake me up")
56+
def say_hello(client, message):
57+
# Unix Epoch time for September 30, 2020 11:59:59 PM
58+
when_september_ends = 1655150400 #use convo_info to print out the timestamp,use timestamp to see live demo
59+
channel_id = message["channel"]
60+
client.chat_scheduleMessage(
61+
channel=channel_id,
62+
post_at=when_september_ends,
63+
text="Summer has come and passed"
64+
)
65+
66+
67+
#adding users to the channel by user id - using conversations_invite
68+
@app.message('invite')
69+
def invite(client,message):
70+
channel_id = message["channel"]
71+
client.conversations_invite(
72+
channel=channel_id,
73+
users = ['W1234567890','U2345678901','U3456789012']
74+
)
75+
76+
#conversation_info
77+
@app.message('convo_info')
78+
def conversation_info(client,message):
79+
channel_id = message['channel']
80+
info = client.conversations_info ( channel=channel_id)
81+
# print (info)
82+
print(client)
83+
print(message)
84+
85+
86+
# Start your app
87+
if __name__ == "__main__":
88+
SocketModeHandler(app,SLACK_APP_TOKEN).start()

0 commit comments

Comments
 (0)