Tutorial: Building a Slack Bot that Writes Custom Queries against your Database
Tags: tutorial, sql, slack, regex
Published: 2020-05-19 15:32:20

##Introduction
Hey everyone! In this one, let's take a look at how we can allow users to query a database, even when they might not know any sql. In this tutorial, we'll build a system that takes a slack message keyword, queries a database using it, and returns a response.

##Prerequisites
Some understanding of regular expressions and SQL will be beneficial. Also, here's the documentation that covers the topics we'll use:
[Working with Slack](https://docs.wayscript.com/library/modules/slack)
[Working with Regex](https://docs.wayscript.com/library/modules/regex)
[Working with SQL](https://docs.wayscript.com/library/modules/sql)

##Slack Trigger
Our workflow should look something like this:
<ol>
<li>User submits a slack message containing string that will trigger the desired action.
<li>The string needs to be extracted using regular expressions
<li>The string extracted needs to query our database
<li>The queried value needs to be returned to the user as a slack message.
</ol>

To start off, we need to look for messages posted into the channel we want these operations to occur in. So we'll need a slack trigger for that.

![trigger](https://cdn.wayscript.com/blog_img/90/1.png)

We're able to configure this trigger how ever we would like. We can choose the workspace and the channel. Additionally, we need to be sure to select the output we want.

![output](https://cdn.wayscript.com/blog_img/90/2.png)

In this example, we're selecting the slack message text. This creates a variable below, inside the variables panel, that we can refer to in the next step. This will be what we apply our regular expression to.

##Conditional and Regular Expression
For the next step, we only want to return a response if a keyword is found. We'll just use "?" as a trigger to let us know we need to perform an action. We can do this by dragging in an if statement, and configuring it to check if the question mark character is present in the string.

![conditional statement](https://cdn.wayscript.com/blog_img/90/3.png)

![inputs](https://cdn.wayscript.com/blog_img/90/4.png)

##Querying the Database - Regex Pattern
Since we now know when to return a response, let's work on generating that response. The first step is to know what the specific response is that we need. We can extract words from our trigger symbol (?) using regular expressions, and then pass this on to an SQL module.

![sql](https://cdn.wayscript.com/blog_img/90/5.png)

The regular expression settings in my script look something like this:

![regular expression](https://cdn.wayscript.com/blog_img/90/6.png)

The regular expression inserted will just look for the contents following a question mark, and then we can return those contents as a captured group.


##Querying the Database - Building the SQL Statement
Now that we've successfully captured the text we want, we can use this to write our SQL statement. We'll drag in an SQL module below our regex module:

![regex](https://cdn.wayscript.com/blog_img/90/7.png)


In this SQL module, we can write our SQL using that captured group. Your statement might look something like this:

![sql](https://cdn.wayscript.com/blog_img/90/8.png)

In my example, we're looking for the first instance and returning this value. This is what we'll write back to slack. At this step, there's also the availability to write back multiple outputs from your SQL database.

##Writing Back to SQL
Now that we've returned the value back from the database, we can easily write it to Slack using a Slack module.

![Slack](https://cdn.wayscript.com/blog_img/90/9.png)

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

{% youtube wnFvooLAKWg %}