You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 28, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: docs/Chapter-3.md
+11-16Lines changed: 11 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,10 +6,6 @@ If you open up this branch's [bot.py](bot.py) file you'll find some changes. Now
6
6
7
7
We can store bits of information we'll want to access later as attributes on the bot object and actions we want our bot to take as methods.
8
8
9
-
### Init Your Bot's Identity
10
-
11
-
In the `__init__` function of the Bot class, which (as the name suggests) initializes the bot, we're giving our bot a touch of identity. Feel free to change the name and emoji here!
12
-
13
9
Instead of importing the `python-slackclient` library in our `app.py` file like we did before, we'll import it into this file and access the `SlackClient` through an attribute on our bot. This makes it easier to handle OAuth since our bot object's attributes can be accessed in the other places it will live, such as the `app.py` file.
14
10
15
11
### Teach Bot How to Auth
@@ -38,7 +34,7 @@ If you peek on over at `app.py` again you'll see we've updated your `/thanks` ro
38
34
39
35
Now that we've been authorized, let's teach our bot to respond to users who want to say hello!
40
36
41
-
Earlier ,we subscribed to the Slack Events API endpoint called [message.channels](https://api.slack.com/events/message.channels). We'll listen for these events, check to see if anyone is saying hello to our bot, and then build a method on our Bot class to respond.
37
+
Earlier, we subscribed to the Slack Events API endpoint called [message.channels](https://api.slack.com/events/message.channels). We'll listen for these events, check to see if anyone is saying hello to our bot, and then build a method on our Bot class to respond.
# using regex, search for 'hello' in the users' text
64
60
hello_match = re.search('hello', users_text)
65
61
if bot_mention_match and hello_match:
66
-
# identify who said hello
67
-
user_id = slack_event["user"]
68
62
# and have your bot respond
69
-
mybot.say_hello(user_id)
63
+
mybot.say_hello()
70
64
```
71
65
72
66
### Bot Response Methods
73
67
74
68
Once `app.py` holds a function to handle the `message` events we want our bot to respond to, we'll need to build a method on our `Bot` class to respond.
75
69
76
-
In `bot.py` you'll see we've started a method called `say_hello` to respond to a user's message in the `#general` channel. Since we already know that we want to say hello at this point, what we'll need to do is get the `user_id` that we've passed as a parameter and build a call to the Slack API's `chat.postMessage` endpoint.
70
+
In `bot.py` you'll see we've started a method called `say_hello` to respond to a user's message in the `#general` channel. At this pointwe'll need to call to the Slack API's `chat.postMessage` endpoint to send our response.
77
71
78
72
It should look something like this:
79
73
80
74
_bot.py_
81
75
```python
82
-
check
76
+
defsay_hello(self):
77
+
""" A method to respond to a user who says hello. """
78
+
hello_response ="I want to live! Please build me."
79
+
self.client.api_call("chat.postMessage",
80
+
channel="#general",
81
+
text=hello_response)
83
82
```
84
83
85
84
Let's start our app back up again and test that it's working in the browser! Double check that your virtual environment is turned on, your secrets are exported to the environment and your ngrok tunnel is open.
@@ -96,13 +95,9 @@ Once you've installed your bot successfully, invite your bot to the `#general` c
96
95
97
96
## You Did It! :sparkles:
98
97
99
-
You should have a working bot! If you're looking for ways to make your bot fancier, look into adding more interaction with [message buttons](https://api.slack.com/docs/message-buttons) or integrating some NLP with a lovely service like [api.ai.](https://api.ai/)
100
-
101
-
We can't wait to see what you build next!
102
-
103
-
Thanks for joining us!
98
+
You should have a working bot! If you're looking for ways to make your bot fancier, we'll go over adding more interaction with [message buttons](https://api.slack.com/docs/message-buttons) and integrating some NLP with a lovely service like [api.ai](https://api.ai/) in the next two chapters.
104
99
105
100
---
106
101
###### Documentation Navigation
107
-
**Next [Give Us Feedback!](https://goo.gl/forms/8FlqD5roZtCl7wx92)**
0 commit comments