This project creates a Reddit bot that listens for mentions of its username in a specific subreddit and responds using the Shapes API.
- Connects to Reddit using Snoowrap
- Listens for mentions of the bot's username in a specified subreddit
- Processes user prompts using your custom Shapes API model/Shape Username
- Sends generated responses back to Reddit comments
- Node.js (v16 or newer)
- npm or yarn
- A Reddit account and Reddit API credentials (client ID, client secret, username, password)
- A Shapes Inc account with an API key
git clone git clone https://github.com/shapesinc/api-examples.git
cd shapes-redditnpm installCreate a .env file in the root directory with the following variables:
SHAPESINC_API_KEY=your_shapes_api_key
SHAPESINC_SHAPE_USERNAME=your_shapes_username
REDDIT_CLIENT_ID=your_reddit_client_id
REDDIT_CLIENT_SECRET=your_reddit_client_secret
REDDIT_USERNAME=your_reddit_username
REDDIT_PASSWORD=your_reddit_password
REDDIT_SUBREDDIT=your_subreddit_name
POLL_TIME=5000
LIMIT=5
To get your Reddit API credentials:
- Go to Reddit's developer site 'https://www.reddit.com/prefs/apps/'
- Create a new developer application (script type)
- Copy the client ID, client secret, username, and password for your bot
npm run startOnce the bot is running and connected to Reddit, it will automatically start listening for comments that mention its username in the specified subreddit. It will then respond to those comments by processing the content through the Shapes API.
- The bot checks for mentions of
u/your_reddit_usernameor/u/your_reddit_usernamein comments. - The bot processes the comment content through the Shapes API, sending the author's name as X-User-Id and the subreddit name as X-Channel-Id.
- The response from the Shapes API is posted back to the comment as a reply.
- The bot ignores comments made by itself.
- Reddit authentication failures: Ensure your Reddit API credentials are correctly set up in the
.envfile. - Shapes API errors: Verify your Shapes API key and username are correct and that the API is accessible.
- No responses: Check that the bot is properly connected to Reddit and is listening to the correct subreddit. Ensure the bot's username is correctly mentioned in comments.
The application logs all incoming comments, API interactions, and any errors encountered to the console. Check the console logs to identify issues.
- Initialization: The bot loads environment variables, initializes the Snoowrap client for Reddit interaction, and sets up the OpenAI client pointing to the Shapes API.
- Comment Stream: It starts a comment stream using snoostorm to monitor new comments in the specified subreddit.
- Mention Detection: For each new comment, it checks if the bot's username is mentioned using canSummon. It also filters out comments made by the bot itself or made before the bot started.
- Shapes API Processing: If a mention is detected, the comment body is sent to the processWithShapes function. This function calls the Shapes API with the comment content, user ID (comment author), and channel ID (subreddit name).
- Replying to Comment: The response received from the Shapes API is then used to reply to the original comment on Reddit.
- Error Handling: Basic error handling is included for both Shapes API calls and Reddit replies, logging errors and attempting to post a generic error message on Reddit if a reply fails.
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to submit issues or pull requests to improve the bot. Ensure any changes are tested and maintain compatibility with Reddit's API and the Shapes API.