Hey there! Ever find yourself constantly refreshing your website to make sure it's still up and running? Or worse, getting that dreaded message from a customer saying, "Hey, your site's down!"? Well, today, we're going to tackle this issue head-on by creating our very own uptime monitor using BotFleet.
What's the Game Plan?
Here's what we're aiming for: We want to set up a bot that pings our website every minute. If everything's fine (meaning our site responds with a 200 status code), the bot goes back to quietly monitoring. But if our site is down (anything other than 200), we're going to get an email letting us know it's time to take action!
Getting Your Tools Ready
Before we dive into the code, make sure you've got a BotFleet account and some basic Python know-how. You'll also need access to an email service that allows sending emails via API or SMTP (like SendGrid, Mailgun, or even Gmail for starters).
Writing the Python Script
Let's get our hands dirty with some code. Our script is going to do two things: check our website's status and send an email if there's trouble. Here's a simple version to start with:
import requests
import os
from smtplib import SMTP
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Your website URL
website_url = "https://your-website.com"
# Email details
sender_email = "[email protected]"
receiver_email = "[email protected]"
password = os.environ.get("EMAIL_PASSWORD")
def check_site():
try:
response = requests.get(website_url)
if response.status_code == 200:
print("Website is up and running!")
else:
send_email()
except requests.exceptions.RequestException as e:
print("Error checking the website:", e)
send_email()
def send_email():
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = "Website Down Alert!"
body = f"Oops! It looks like {website_url} is not responding correctly."
message.attach(MIMEText(body, "plain"))
with SMTP("smtp.example.com", 587) as server:
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message.as_string())
print("Sent an alert email about the website's down status.")
def main():
check_site()
Break It Down Now!
check_site
function: Pings our website. If it gets a 200, it means all's well. If not, it calls send_email to alert us.
send_email
function: Crafts a simple email saying our site's down and sends it off. Remember to replace "smtp.example.com" with your email provider's SMTP server details and adjust the port if necessary.
Setting Up the Bot on BotFleet
Head over to BotFleet and go to the bots section. Name it something memorable, like "UptimeMonitor".
Indicating script
Copy the Python script we just wrote into the script section.
Indicating requirements
We're using the requests library to check our website's status. Let's add this to our requirements:
requests
Indicating environment variables
Set EMAIL_PASSWORD
as an environment variable:
EMAIL_PASSWORD=your-email-password
Schedule It
Set your bot to run every minute. This way, it keeps an eye on your site without you having to lift a finger. You can use the following cron expression to run your bot every minute:
* * * * *
What's Next?
After setting this up, you're essentially done! Your bot is now a diligent guardian of your website's uptime, ready to notify you the minute it detects a snooze.
Wrapping Up
Remember, this is a basic setup. Real-world applications might need more robust error handling, logging, and maybe even fancier alerting mechanisms (like integrating with Slack or Discord). Feel free to get creative and build upon this foundation.
And that's it! You've just automated one of the most crucial monitoring tasks for any website owner. No more manual checks, no more unpleasant surprises from customers. Just peace of mind and a bit more free time on your hands.
Got questions or cool ideas on how to extend this bot? Reach out.