How to build a "Chat with HubSpot" AI Slackbot
Introduction #
In this guide, we'll walk you through creating a Slackbot that uses AI to answer questions about HubSpot deals. This bot leverages the power of CrewAI, HubSpot's API, and OpenAI's language models to provide intelligent responses to deal-related queries.
By the end of this tutorial, you'll have a functional Slackbot that can fetch and answer queries about your HubSpot deals data.
Getting Started #
To begin, fork this template by clicking "Use Template" below:
Setting Up #
Slack App
- Go to the Slack Apps dashboard and create a new app.
- In your app's settings, navigate to the "Basic Information" page.
- Scroll down to the "App-Level Tokens" section.
- Click on "Generate Token and Scopes" to create a new app-level token.
- Provide a name for your token.
- Add the following scopes for your app:
- connections:write (for Socket Mode)
- Click "Generate" to create the token. This is your SLACK_APP_TOKEN: Your app token - it should start with xapp
- Navigate to "OAuth & Permissions" and find your "Bot User OAuth Token". This is your SLACK_BOT_TOKEN: Your bot user OAuth token - it should start with xoxb
- For your Bot Token, set the following permissions:
In Replit, go to the Secrets tab (Tools > Secrets) and add the credentials to your secrets within SLACK_APP_TOKEN and SLACK_BOT_TOKEN respectively.
If you want a character for your new Slack App, feel free to download this little guy:
HubSpot Private App Key
Log in to your HubSpot account and go to Settings > Integrations > Private Apps.
Click "Create a private app" and give it a name.
Set the required scopes (you'll need access to deals, contacts, companies, and owners).
Click "Create app" and you'll receive a private app access token.
In Replit, add this token to the Secrets tab as HUBSPOT_API_KEY.
OpenAI API Key
- Log in to the OpenAI developer platform.
- Navigate to API keys and create a new secret key.
- Copy the secret key and add it to Replit's Secrets tab as OPENAI_API_KEY.
Understanding the Code #
CrewAI Concepts
This project uses CrewAI, a framework for creating AI agents that work together. Here are the key concepts:
- Tools: Functions that agents can use to interact with external systems or perform specific tasks.
- Agents: AI entities with defined roles and goals.
- Tasks: Specific jobs assigned to agents.
- Crew: A group of agents working together to accomplish a goal.
To learn more about CrewAI, be sure to visit their documentation here.
Fetching Deal Properties and Stages
One unique feature of this implementation is how it fetches all potential deal properties and stages from HubSpot at the start. This allows the AI to build valid filters based on user queries later on.
Let's look at the relevant tools:
These tools fetch all deal properties and stages, which are then used by the AI to understand and formulate valid queries.
Fetching Matching Deals
The FetchMatchingDealsTool is a crucial component that searches for deals based on the user's query. Here's a snippet of the task that uses this tool:
This task instructs the AI on how to use the FetchMatchingDealsTool to find relevant deals based on the user's query.
Summarizing Results
The final step involves summarizing the found deal data. This is handled by the message_summarizer agent and its associated task:
This task ensures that the bot provides a concise, relevant summary based on the deals found and the user's original query.
Running Your Bot #
To run your bot:
- Make sure all required secrets are set in the Replit Secrets tab.
- Click the "Run" button in Replit.
- Your bot should now be active in your Slack workspace. You’ll see a message “⚡️ Bolt app is running!” in the Console.
To interact with your bot, mention it in a channel where it's been invited, followed by your query about HubSpot deals.
Add your bot to a channel by heading to Integrations in the top-right corner. Click on Add apps. Search for your new Slackbot and add it to the channel.
Deploying Your Bot #
In order to keep your bot running 24/7 and receive requests whenever someone mentions it in Slack, you'll need to deploy it on a hosted server.
Open a new tab in the Workspace and search for “Deployments” or open the control console by typing ⌘ + K (or Ctrl + K) and type "deploy". You should find a screen like this.
For bots like this that need always need to be up listening to requests, we recommend using a Reserved VM. On the next screen, click Approve and configure build settings most internal bots work fine with the default machine settings but if you need more power later, you can always come back and change these settings later. You can monitor your usage and billing at any time at: replit.com/usage.
On the next screen, you’ll be able to set your primary domain and edit the Secrets that will be in your production deployment. Usually, we keep these settings as they are.
Finally, click Deploy and watch your bot go live!
What's Next #
To further improve your HubSpot AI Slackbot, consider:
- Enhancing the DealSearchTool for better accuracy and performance. This could involve explicitly prompting the agents for more filter and search combinations.
- Adding more tools and tasks to expand the bot's capabilities. For example, you could create tools to:
- Update deal information
- Create new deals
- Fetch and analyze contact or company data
- Adding conversational context to allow for follow-up questions and more natural interactions.
If you’d like to bring this project and similar templates into your team, set some time here with the Replit team for a quick demo of Replit Teams.
Happy coding!