Stack Overflow and Glitch Notifications in Slack with FaunaDB

FaunaDB Cloud user Nicolas Grenié has created two open source applications connecting popular services with Slack chat. If you’ve ever wanted Slack notifications when there’s online activity, his code is a good template, so I’ll link to some highlights from this post. Check out how simple FaunaDB makes these operations.

Nicolas Grenié agrees, remarking in chat: “Fauna is my default go-to solution for any project I am starting that involves a database.”

Stack Overflow Monitor

Stack Overflow is a popular question and answer site, making it an important place for companies to keep up with what users are asking about their products. Stack Overflow Monitor simply posts a message to a Slack channel when new questions match your search terms.

As a serverless application, it requires no operational support from you once it is up and running. No servers to upgrade or restart, just code. Installing it only requires altering one settings file, and using the serverless command to deploy functions to AWS Lambda. Once configured with Stack Overflow, Slack, and FaunaDB API credentials, and your chosen search terms, it will automatically update your Slack channel as long as you keep your function in place.

The entire application logic is implemented in 101 lines of JavaScript and is triggered by AWS Lambda’s cron-like task scheduler. Each time it runs, it searches Stack Overflow for recent matches, and then uses FaunaDB to look up the message id. If a record exists that means it’s already been posted, otherwise the message is posted to Slack and a record is created. To give you an idea how simple that code is:

        client.query(
          q.Create(
            q.Class('questions'),
              {data: question }
            ));
        sendToSlack(question);

Because it is a simple Stack Overflow monitor, it doesn’t take advantage of FaunaDB’s transactional capabilities. Hardening the script for edge cases might involve tracking Slack API call success / failure state in a FaunaDB object as well, so that no messages are dropped.

To install Stack Overflow Monitor, follow the instructions in the blog post.

Glitch Notifier

Glitch is a community where developers can help each other learn new skills. Answerers may only be interested in answering a narrow set of questions, so it’s nice to be alerted about activity instead of visiting the site to check for updates. Glitch Notifier is a service that alerts you in a Slack channel when new questions are available on Glitch.

Rather than a single-tenant serverless application, where you deploy an instance according to your parameters, Glitch notifier runs as a SaaS endpoint, allowing Glitch users to connect their Slack accounts and specify Glitch tags to follow. You can view the source code here, and if you sign up for a Glitch account, you can even launch your own fork of it.

Glitch Notifier uses FaunaDB not only to store the message ids of questions it has processed, but also to store each user’s notification preferences. Glitch questions are tagged, and users tell Glitch notifier which tags they’d like to be alerted on. Here is the query where the notifier loads all the users interested in the tags for a question.

  var users_in_db = client.query(
              q.Paginate(
              q.Union(
                q.Map(question.details.tags,
                  function(tag) {
                    return q.Match(q.Index("user_by_tag"), tag)
                  })
              )));

Once the user list is loaded, Glitch Notifier posts the messages to Slack using the endpoint stored with each user record.

Conclusion

Nicolas says helping others learn new skills is “a great way to give back to the community that welcomed me when I began.” We hope his code example above help you get started. If you’d like a code-level introduction to FaunaDB, here is our hello world tutorial.

FaunaDB Cloud costs nothing to get started with, and has a pay-as-you-go model that makes it easy to get started. Sign up today and you can create your own apps for free. Here are two more serverless starter kits: Authentication Boilerplate, and Distributed Ledger.