Skip to content

A simple Discord and Slack bot that that checks the stock status of selected Raspberry Pi models on Adafruit and sends a message to a Discord and/or Slack channel when one comes in stock.

License

Notifications You must be signed in to change notification settings

EthyMoney/Adafruit-Pi-Stock-Bot

Repository files navigation

Adafruit Raspberry Pi Stock Alert Bot

GitHub tag License issues - Adafruit-Pi-Stock-Bot Made For - Discord Made For - Slack Node.js - >=20.9.0 Discord.js - 14.14.1


Alert Icon       Raspberry Pi 5 Model B       Alert Icon


DigitalOcean Referral Badge

Contents

What This Is

A simple Discord and Slack bot that checks the stock status of selected Raspberry Pi models on Adafruit and sends a message to a Discord/Slack channel when one comes in stock. This bot is designed to be self-hosted and run for use in your own Discord server or Slack workspace.

Why?

Because Adafruit's stock notification system is lacking. It's a FIFO queue that does not reliably trigger notifications in a timely manner and sometimes removes your notification entirely even when you never got one! This means that every time any restock happens at all, even if it's small and doesn't trigger your notification, you'll likely miss it AND have to go back and re-subscribe to the notifications. This bot removes the need for that by allowing you to quickly get a @mention in your Discord server or a message in your Slack channel every time there is a restock, without a delay!

How It Works

On a set interval, the bot will query Adafruit's product pages for the models you have enabled to watch and will check for the stock statuses changing to in stock. If one or more of the models come in stock, a notification is sent out to the configured Discord server channel with accompanying @role mentions. For Slack, it will just send the notification to the configured channel since Slack doesn't have a roles system like Discord does. In either case, the notification will contain a direct link to the page of the SKU that's in stock so you can buy it right away. Stock statuses are tracked between update intervals, so you won't have to get spammed with the same notification on every check if the bot has already sent a notification for a current stock event of a particular model. This is handled in a smart way to ensure you always get one notification every time any model comes in stock, while never missing a restock!

How to Set Up and Run

  • Install Node.js LTS edition for your specific environment using the site or a package manager. Node.js is supported basically everywhere which allows this bot to be multi-platform!
  • Clone the repo, then run npm install from a terminal in the root project folder. This installs all necessary dependencies for you.
  • Follow the below instructions for setting up your very own Discord or Slack bot (or even both!). Be sure to complete the final steps as well once you finish the Discord and Slack specific instructions.

Discord Bot Set Up

  • Go to the Discord Developer Portal and click "New Application" at the top.
  • Give your bot application a name and hit create.
  • From the left side navigation column, select the "Bot" tab (has a puzzle piece icon), click "Add Bot" and confirm with the "Yes, do it!" button.
  • From here, go ahead and set a username and avatar for your new bot. You'll want to uncheck the "Public Bot" option as well.
  • Now you need to make an invite link so you can add the bot to your server. From the left side navigation column, select the "General Information" tab.
  • Copy your "Application ID" shown there. You will put this into the following template link so it can identify your bot.
  • Use this invite link as your template: https://discord.com/oauth2/authorize?client_id=APPLICATION_ID_HERE&scope=bot&permissions=412652817488
  • Replace APPLICATION_ID_HERE in that link with your actual application ID you copied earlier.
  • Now go ahead and use that link to add your bot to your server. Be sure to leave all permissions checked! These are pre-configured for you.
  • It's important that you add the bot to your server before you proceed. The bot program expects to already have access to the server when it starts up.
  • Now, you need to configure the config.json file for your use. This file is located in the /config directory. Open the file in a text editor.
  • Enter your bot's token under the token field of the discord section of the config. Your token can be found back in the developer portal under the "Bot" tab again. Click on "Reset Token" and copy it. KEEP THIS SAFE AND PRIVATE!
  • Now enter the ID number of the server you added the bot to earlier for the serverID field. You can get from within Discord by right clicking on the server icon (with developer options enabled in settings)
  • Now enter the name of the channel in your server where you'd like to have updates posted for the channelName field. You can leave this blank if you want the bot to create a new one for you (will be named pi-stock-notifications)

Slack Bot Set Up

  • Go to the Slack App API and click "Create an app", then select "From scratch" in the popup that appears.
  • Give your Slack App a name and select your workspace you'd like to add the bot to, then click "Create App".
  • Under the "Add features and functionality" section select the "Bots" option.
  • Along the left side navigation under the "Features" section, select "OAuth & Permissions". Once selected, scroll down to the "Scopes" section.
  • Under "Bot Token Scopes", click the "Add an OAuth Scope" button and then add these scopes:
    • "chat:write",
    • "links:write",
    • "channels:manage",
    • "chat:write.customize",
    • "chat:write.public".
  • Now scroll back up and click the "Install to Workspace" button. Allow the app access to your workspace using the "Allow" button on the screen that appears.
  • You will now be shown a page with your bot token. Copy the "Bot User OAuth Token" and paste it in the token field of the slack section in the config.json. KEEP THIS TOKEN SAFE AND PRIVATE!
  • Create at least one channel for the bot to post into. Put the name of the channel into the config.json in the channelName field of the slack section.

Final Configuration Steps and Bot Startup

  • In the config.json file:
    • Indicate whether you are using the Discord bot, Slack bot, or even both, using the enableBot option in the Discord and Slack sections of the config file. These are both on(true) by default, adjust them accordingly if needed. Remember, you can't start without at least one on, but why would you try that anyway?
    • Enter the update interval in seconds for updateIntervalSeconds (default is 60 seconds).
    • Set any models you don't wish to monitor to false under the modelsSelection section (all are enabled(true) by default).
    • Choose whether or not you want to have sleep mode enabled using enableSleepMode. Sleep mode just prevents the bot from querying Adafruit overnight when restocks aren't happening (this is enabled(true) by default). Prevents needless spam to Adafruit's servers while they are closed.
    • Set whether you want to skip sending alerts for in-stock models right on bot startup using the skipStartupAlerts options in config.json. This is useful if you are restarting the bot multiple times up and don't want to get spammed with alerts for models that are currently in stock at every startup. This is disabled(false) by default. If enabled, it will skip sending a stock alert for currently in stock models when the bot starts, but will send them when they come back in stock after going out of stock while the bot is running.
  • Yay! You are now ready to start your bot! Go ahead and run npm start in a terminal of the project directory to launch the bot!.
  • If you are using the Discord bot, be sure to make use of the roles that the bot created! Add them to yourself and others so you get mentioned when stock comes in.
  • That's it! I hope you get the shiny new Pi you've been looking for! :)

Optional Final Configuration

  • You can daemonize the app using PM2. A PM2 process definition file has been provided to do so. Simply run pm2 start process.json in the project directory to start the bot as a daemon. You can also use the pm2 monit command to monitor the bot's status and log output. Starting the bot this way will allow it to run in the background and also restart automatically if it crashes for any reason. If on Linux, you can use the pm2 startup command to have the bot start on system boot. See the PM2 docs for more info. Highly recommended using this run method if you want more a of "set it and forget it" experience. It's great!

Running as Docker Container

If you prefer Docker, it is supported to deploy this bot as a container. A Docker Hub repository is maintained and updated with each release of the bot. You can find it here.

To run the container using the latest release, you can use the following command:

  • docker run -v adafruit-pi-bot:/usr/src/app/config:rw -d ultimate360/adafruit-pi-stock-bot

The /config directory is added as volume so you can access config files from your host. As written, it uses the default volumes location and names it adafruit-pi-bot. You can change this name or customize the mount path to whatever you want, just be sure to update the command above to match.

Once the container starts, you will notice it immediately exits. This is because the config file is missing values that you need to go fill in. Use the above normal instructions to fill in the config.json file located at the new volume mount we created. Once you have done this, you can restart the container and it will run normally.

If you wish to build the container yourself, like maybe if you want the latest commits in the branch above the last release, or you made your own modifications, there are npm commands to help you do this. You can run npm run docker-build to build the container, and npm run docker-run to run it. These scripts utilize pre-configured settings through a dockerfile and volume mounting of configuration files.

Customizing the Messages and Adding Additional Models

You may notice another file sitting in the /config directory, named models.json. The file contains all of the metadata the bot uses for the stock notifications. You can edit this file to change the notification messages to your liking, whether that be new descriptions, titles, names, links, images, etc. You can also add new models to the file if you want to monitor more than the default models. The bot will automatically pick up any changes you make to this file and use them. Just be sure to follow the same format as the other default models in the file and remember to add them as modelSelections options to the config.json with the name matching what you put for configFileName in the models file. Enjoy!


What the Messages Look Like

Discord Message Multi       Discord Message Single       Slack Message


One More Thing

Like this bot? Show some support! Give me a star on this repo and share it with your friends! You can also sign up for Digital Ocean to host this bot or whatever else on, I get a small referal kickback when you use the blue Digital Ocean button at the top of this page :)
Contributions are welcome and encouraged! Feel free to open a pull request or issue for things you notice or want to fix/improve.
If you want to chat, you can find me in the support Discord server of my other popular bot that I made called TsukiBot, Join Here!