Building an AI Chatbot with make, Perplexity AI, and Slack

Introduction

Are you looking to build a powerful AI chatbot for your Slack workspace? Let me introduce you to a solution that combines make, Perplexity AI, and Slack to create an intelligent, internet-aware chatbot without complex coding.

This integration allows you to:

  • Process Slack events effortlessly using make’s intuitive workflow automation
  • Leverage Perplexity AI’s API to provide real-time internet search capabilities
  • Maintain conversation history for contextual responses

In this comprehensive guide, I’ll walk you through the entire process of setting up this integration, from handling Slack events to managing conversation flows with make’s powerful tools. Whether you’re new to automation or an experienced developer, you’ll find this solution both practical and easy to implement.

Prerequisites

Environment

The following environment components are required:

  • Slack (Free plan)
  • make (Free plan)
  • Perplexity API ($5 credit required)

API

This article won’t cover the setup process for Slack and Perplexity API here, but you can refer to the following resources:

Overview

Here’s the scenario:

When you mention the Slack bot, it provides responses from Perplexity AI. You can continue the conversation by replying within the thread of the mentioned message. In the following sections, I’ll explain each component while highlighting the key points.

Section 1: Webhook Responses and Branching

This scenario receive webhooks triggered by Slack’s Event Subscriptions. The URL generated from make’s “Custom Webhook” Module is registered in Event Subscriptions.

The branching logic is as follows:

  1. For Challenge Response of Event Subscriptions
  2. For Regular Event Subscriptions Response
  3. Handling thread messages in Slack
  4. Handling bot mentions

Key Points

Only Respond to Non-Bot User Events

Since the bot will post Perplexity API responses, set up a filter to prevent the bot from responding to its own messages. If you don’t, the bot will respond to itself, causing chaos.

“2nd: Initial Event Subscriptions Response” is Mandatory

According to Slack’s Events API specification, if you don’t return a response within 3 seconds of receiving a webhook, it will be resent. Since Perplexity API takes time to generate responses (around 15 seconds), this setting is crucial. A simple 200 response is sufficient for the webhook.

Reference: Slack - Events API > Error Handling

Can it Pick Up Private Channels?

Add “message.groups” to Event Subscriptions. I haven’t tested it, but it should work.

Section 2: Data Processing and Transformation

Continuation of 3rd: Save Slack messages to Data Store → Search and aggregate thread messages from Data Store into an array

Continuation of 4th: Process Slack messages with Text Parser and save to Data Store

Key Points

Pass Conversation History as Prompts to AI
With Perplexity API and other generative AI services, you can send the conversation history between users and AI. You can pass the data in an alternating format of user and assistant roles as shown below.

[
    {
      "role": "user",
      "content": "hoge-nanoda"
    },
    {
      "role": "assistant",
      "content": "fuga-nanoda"
    },
    {
      "role": "user",
      "content": "hoge-fuga-nodada"
    },
]

In this scenario, store user messages and enable retrieval of thread interactions using timestamps.

Reference: Perplexity - API Reference

Set a Key When Saving to Data Store

When saving records to Data Store, you can specify a Key. While omitting it won’t cause errors, there are Modules that are practically unusable without setting a Key. Although this Scenario doesn’t use such Modules, it’s recommended to set keys to avoid potential issues.

In this case, I save using the format {{channel ID}}-{{timestamp}} based on data received from Slack webhook.

Section 3: User Response and Data Processing

This section connects to the 3rd and 4th branches explained in Section 1. Both branches use exactly the same Modules and perform identical processes, so we’ll explain based on “3rd: Handling thread messages in Slack”.

The following process flow is implemented. Since it’s difficult to explain in text alone, I use a Mermaid diagram.

Key Points

Keep Users Informed

At the beginning of Section 3, send a “Generating response…” notification from make to Slack.

Additionally, when errors occur during processing, it’s considerate to implement error notifications since users can’t see the status in Slack.

Process Perplexity AI Responses

As a technical note, Perplexity AI provides responses with URLs from internet searches. These URLs are returned as citations[].

The Perplexity AI response text includes numbers that correspond to Citations. Since we need to number the Citations as well, we use Iterator + Text Aggregator to add these numbers.

Processing Data (Messages) for Perplexity AI

We process this using Array Aggregator following Search records. We can specify the Messages structure for the Perplexity API using Target Structure type, which automatically detects the structure. However, please execute it once before specifying.

Conclusion

Thanks to make, we were able to create this integration with remarkable speed and efficiency. If you have any questions, please post them in the thread.

1 Like