Tutorial: Trigger a Python Script Using Slack
Tags: tutorial, python, slack, beginners
Published: 2020-05-13 12:55:06

---
title: "Tutorial: Trigger a Python Script Using Slack"
canonical_url: https://wayscript.com/learn/trigger-python-script-using-slack
---
{% youtube DJo1uCyNWKE %}

##Introduction
Chances are that you are already using Slack in your personal or workplace communications. Let's learn how we can make your slack channels even more powerful with [WayScript](https://wayscript.com/). In this tutorial, let's take a look at how we can execute python code whenever a message is sent to a specified channel in slack.

##Prerequisites
No prerequisites but some content you might find helpful:
[Working with Slack](https://docs.wayscript.com/library/modules/slack)
[Working with Python](https://docs.wayscript.com/library/modules/python)

##Activating a Script with Slack
In WayScript, we activate our scripts using triggers. We have a variety of different triggers available, but in this tutorial, let's use a slack trigger.

![Trigger](https://cdn.wayscript.com/blog_img/87/1.png)

This slack trigger allows us to activate the workflow depending on the event type. In this example, let's say we want to activate whenever a message is posted to a certain, specified channel.

![Slack](https://cdn.wayscript.com/blog_img/87/2.png)

Then, if we want to create personalized response depending on the message text, we can create a variable using the outputs. We do this by selecting the output "Slack Message Text."

![Message](https://cdn.wayscript.com/blog_img/87/3.png)

##Passing the Slack Message to Python
Since we imported the variable of slack message text into our workflow, we can access it anywhere downstream of the trigger. Let's use it in a python module to send back personalized results.

![pass message](https://cdn.wayscript.com/blog_img/87/4.png)

Here, we have limitless options on what we can do with the user's input. We could send requests to different APIs, process the posted data, lookup the data against our databases, and so much more. An example personalized response may be something like sending a request to the website the user has posted in their slack message.

```python
# reading in the variable we created in slack into our python script
python_variabe_name = variables['Slack_Message_Text']
​
# import default modules
# If a module isn't already imported, you can install your own modules using the requirements.txt
import requests

# Custom get request depending on slack message contents
r = requests.get(python_variable_name)
website_status = r.status_code

# "Exporting" the variable from python back into the wayscript editor
variables[ 'wayscript_variable_status' ] = website_status
```


##Sending a Slack Response
We have created the variable we want to send back to the user whenever they post into slack with python, so now we just need a final slack module to finish this operation.

![slack response](https://cdn.wayscript.com/blog_img/87/5.png)

This time, instead of pulling data, we'll be writing data to the same channel the user posted into.

![slack channel](https://cdn.wayscript.com/blog_img/87/6.png)

This will write the custom response back to the user whenever they post in that channel.

##Need Help?
If there's any questions feel free to message us on discord. We're happy to help! If you want to see this full script template, you can find it [here](https://wayscript.com/user/derricks/N06ui2CQ)