Skip to content

Commit 35eaa2b

Browse files
committed
Add old example under python2 dir & update the latest one
1 parent 055d905 commit 35eaa2b

File tree

6 files changed

+207
-16
lines changed

6 files changed

+207
-16
lines changed

example/README.rst

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,20 @@ messages using Slack's Web API via python-slackclient v2.
1010

1111
**Set up your Python environment:**
1212

13-
We're using virtualenv to keep the dependencies and environmental variables specific to this app. See `virtualenv`_ docs for more info.
13+
We're using venv (or virtualenv) to keep the dependencies and environmental variables specific to this app. See `venv`_ docs for more info.
1414

15-
.. _virtualenv: https://virtualenv.pypa.io
16-
17-
This example app works best in Python 2.7. If 2.7 is your default version, create a virtual environment by running:
18-
19-
.. code::
20-
21-
virtualenv env
22-
23-
Otherwise, if Python 3+ is your default, specify the path to your 2.7 instance:
15+
.. _venv: https://docs.python.org/3/library/venv.html
2416

2517
.. code::
2618
27-
virtualenv -p /your/path/to/python2 env
19+
python -m venv env
2820
2921
Then initialize the virtualenv:
3022

3123
.. code::
3224
3325
source env/bin/activate
3426
35-
3627
**Install the app's dependencies:**
3728

3829
.. code::
@@ -69,7 +60,7 @@ Copy your app's **Bot User OAuth Access Token** and add it to your python enviro
6960

7061
.. code::
7162
72-
export SLACK_BOT_TOKEN=xxxXXxxXXxXXxXXXXxxxX.xXxxxXxxxx
63+
export SLACK_BOT_TOKEN=xoxb-111-222-xxxXXxxXXxXXxXXXXxxxX
7364
7465
Next, go back to your app's **Basic Information** page
7566

example/example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from slackeventsapi import SlackEventAdapter
2-
import slack
2+
from slack import WebClient
33
import os
44

55
# Our app's Slack Event Adapter for receiving actions via the Events API
@@ -8,7 +8,7 @@
88

99
# Create a SlackClient for your bot to use for Web API requests
1010
slack_bot_token = os.environ["SLACK_BOT_TOKEN"]
11-
slack_client = slack.WebClient(slack_bot_token)
11+
slack_client = WebClient(slack_bot_token)
1212

1313
# Example responder to greetings
1414
@slack_events_adapter.on("message")

example/python2/README.rst

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
Example Slack events API bot
2+
=============================
3+
4+
This example app shows how easy it is to implement the Slack Events API Adapter
5+
to receive Slack Events and respond to
6+
messages using Slack's Web API via python-slackclient.
7+
8+
🤖 Setup and running the app
9+
------------------------------
10+
11+
**Set up your Python environment:**
12+
13+
We're using virtualenv to keep the dependencies and environmental variables specific to this app. See `virtualenv`_ docs for more info.
14+
15+
.. _virtualenv: https://virtualenv.pypa.io
16+
17+
This example app works best in Python 2.7. If 2.7 is your default version, create a virtual environment by running:
18+
19+
.. code::
20+
21+
virtualenv env
22+
23+
Otherwise, if Python 3+ is your default, specify the path to your 2.7 instance:
24+
25+
.. code::
26+
27+
virtualenv -p /your/path/to/python2 env
28+
29+
Then initialize the virtualenv:
30+
31+
.. code::
32+
33+
source env/bin/activate
34+
35+
36+
**Install the app's dependencies:**
37+
38+
.. code::
39+
40+
pip install -r requirements.txt
41+
42+
**🤖 Create a Slack app**
43+
44+
Create a Slack app on https://api.slack.com/apps/
45+
46+
.. image:: https://cloud.githubusercontent.com/assets/32463/24877733/32979776-1de5-11e7-87d4-b5dc9e3e7973.png
47+
48+
**🤖 Add a bot user to your app**
49+
50+
.. image:: https://cloud.githubusercontent.com/assets/32463/24877750/47a16034-1de5-11e7-989b-2a90b9d8e7e3.png
51+
52+
**🤖 Install your app on your team**
53+
54+
Visit your app's **Install App** page and click **Install App to Team**.
55+
56+
.. image:: https://cloud.githubusercontent.com/assets/32463/24877770/61804c36-1de5-11e7-91ef-5cf2e0845729.png
57+
58+
Authorize your app
59+
60+
.. image:: https://cloud.githubusercontent.com/assets/32463/24877792/774ed94c-1de5-11e7-8857-ac8d662c5b27.png
61+
62+
**🤖 Save your app's credentials**
63+
64+
Once you've authorized your app, you'll be presented with your app's tokens.
65+
66+
.. image:: https://cloud.githubusercontent.com/assets/32463/24877652/d8eebbb4-1de4-11e7-8f75-2cfb1e9d45ee.png
67+
68+
Copy your app's **Bot User OAuth Access Token** and add it to your python environmental variables
69+
70+
.. code::
71+
72+
export SLACK_BOT_TOKEN=xxxXXxxXXxXXxXXXXxxxX.xXxxxXxxxx
73+
74+
Next, go back to your app's **Basic Information** page
75+
76+
.. image:: https://user-images.githubusercontent.com/32463/43932347-63b21eca-9bf8-11e8-8b30-0a848c263bb1.png
77+
78+
Add your app's **Signing Secret** to your python environmental variables
79+
80+
.. code::
81+
82+
export SLACK_SIGNING_SECRET=xxxxxxxxXxxXxxXxXXXxxXxxx
83+
84+
85+
**🤖 Start ngrok**
86+
87+
In order for Slack to contact your local server, you'll need to run a tunnel. We
88+
recommend ngrok or localtunnel. We're going to use ngrok for this example.
89+
90+
If you don't have ngrok, `download it here`_.
91+
92+
.. _download it here: https://ngrok.com
93+
94+
95+
Here's a rudimentary diagream of how ngrok allows Slack to connect to your server
96+
97+
.. image:: https://cloud.githubusercontent.com/assets/32463/25376866/940435fa-299d-11e7-9ee3-08d9427417f6.png
98+
99+
100+
💡 Slack requires event requests be delivered over SSL, so you'll want to
101+
use the HTTPS URL provided by ngrok.
102+
103+
Run ngrok and copy the **HTTPS** URL
104+
105+
.. code::
106+
107+
ngrok http 3000
108+
109+
.. code::
110+
111+
ngrok by @inconshreveable (Ctrl+C to quit)
112+
113+
Session status online
114+
Version 2.1.18
115+
Region United States (us)
116+
Web Interface http://127.0.0.1:4040
117+
118+
Forwarding http://h7465j.ngrok.io -> localhost:9292
119+
Forwarding https://h7465j.ngrok.io -> localhost:9292
120+
121+
**🤖 Run the app:**
122+
123+
You'll need to have your server and ngrok running to complete your app's Event
124+
Subscription setup
125+
126+
.. code::
127+
128+
python example.py
129+
130+
131+
**🤖 Subscribe your app to events**
132+
133+
Add your **Request URL** (your ngrok URL + ``/slack/events``) and subscribe your app to `message.channels` under bot events. **Save** and toggle **Enable Events** to `on`
134+
135+
.. image:: https://user-images.githubusercontent.com/1573454/30185162-644d0cb8-93ee-11e7-96af-55fe10d9d5c8.png
136+
137+
.. image:: https://cloud.githubusercontent.com/assets/32463/24877931/e119181a-1de5-11e7-8b0c-fcbc3419bad7.png
138+
139+
**🎉 Once your app has been installed and subscribed to Bot Events, you will begin receiving event data from Slack**
140+
141+
**👋 Interact with your bot:**
142+
143+
Invite your bot to a public channel, then say hi and your bot will respond
144+
145+
hi @bot 👋
146+
147+
.. image:: https://cloud.githubusercontent.com/assets/32463/23047918/964defec-f467-11e6-87c3-9c7da11fc810.gif
148+
149+
🤔 Support
150+
------------
151+
152+
Need help? Join `Bot Developer Hangout`_ and talk to us in `#slack-api`_.
153+
154+
You can also `create an Issue`_ right here on GitHub.
155+
156+
.. _Bot Developer Hangout: http://dev4slack.xoxco.com/
157+
.. _#slack-api: https://dev4slack.slack.com/messages/slack-api/
158+
.. _create an Issue: https://github.com/slackapi/node-slack-events-api/issues/new

example/python2/example.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from slackeventsapi import SlackEventAdapter
2+
from slackclient import SlackClient
3+
import os
4+
5+
# Our app's Slack Event Adapter for receiving actions via the Events API
6+
slack_signing_secret = os.environ["SLACK_SIGNING_SECRET"]
7+
slack_events_adapter = SlackEventAdapter(slack_signing_secret, "/slack/events")
8+
9+
# Create a SlackClient for your bot to use for Web API requests
10+
slack_bot_token = os.environ["SLACK_BOT_TOKEN"]
11+
slack_client = SlackClient(slack_bot_token)
12+
13+
# Example responder to greetings
14+
@slack_events_adapter.on("message")
15+
def handle_message(event_data):
16+
message = event_data["event"]
17+
# If the incoming message contains "hi", then respond with a "Hello" message
18+
if message.get("subtype") is None and "hi" in message.get('text'):
19+
channel = message["channel"]
20+
message = "Hello <@%s>! :tada:" % message["user"]
21+
slack_client.api_call("chat.postMessage", channel=channel, text=message)
22+
23+
24+
# Example reaction emoji echo
25+
@slack_events_adapter.on("reaction_added")
26+
def reaction_added(event_data):
27+
event = event_data["event"]
28+
emoji = event["reaction"]
29+
channel = event["item"]["channel"]
30+
text = ":%s:" % emoji
31+
slack_client.api_call("chat.postMessage", channel=channel, text=text)
32+
33+
# Error events
34+
@slack_events_adapter.on("error")
35+
def error_handler(err):
36+
print("ERROR: " + str(err))
37+
38+
# Once we have our event listeners configured, we can start the
39+
# Flask server with the default `/events` endpoint on port 3000
40+
slack_events_adapter.start(port=3000)

example/python2/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
slackclient<2
2+
slackeventsapi>=0.1

example/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
slackclient>=2
2-
slackeventsapi>=0.1
2+
slackeventsapi>=2

0 commit comments

Comments
 (0)