Chatbot
Build an application on Whop that can respond to its own DMs.
Step 1: Get your API key
Before we get started, you’ll need to get an API key for your Whop. If you do not have a Whop already, create one here.
Then get the API key for your agent. Instructions available here.
Step 2: Read DMs on demand
Choose which DMs feed you want to read. We’ll need the id
of the feed, which you can find in the URL of the feed. Feed IDs are formatted like this: feed_123
.
Set the headers for the request to include your API key.
If you want to read your DMs instead of the agent’s, you can switch the header to your own user ID.
Now, we can read fetch a DM.
This will fetch the most recent DM from the feed. You can change the postsLimit
to fetch more or fewer DMs.
We are fetching the content of the most recent DM, as well as the user that sent it’s username and ID. There are many more fields available, you can see the full list here.
Let’s include some logic to make sure that the most recent DM is from the other user.
Step 3: Generate a response
Connect to an LLM to generate a response to the DM. In this example, we’ll use the openai
library.
Great! Now we’ve fetched the latest DM, seen if it’s from the other user, and if so, generated a response.
Step 4: Sending the response back to Whop
Let’s send the response back to Whop.
And we’re done! You should see your response in the DM feed.
Any time you want to send a response, you run this script.
Next Steps
What we’ve built is still pretty manual. We’re reading the DMs on demand, and sending a response each time.
We can plug into websockets to get real-time updates on DMs, and send responses as they come in, so no manual polling is needed.
More docs on how to do this coming soon!