Python Bots for Personal Use | Generated by AI
Creating a bot can be a fun and practical way to automate tasks, aggregate information, or interact with users. Based on the two Telegram bots you’ve shared (one for scraping and sending news summaries and another for sending reminders or fetching chat IDs), here are some ideas for other bots you could develop, tailored to different use cases and interests. Each idea includes a brief description, potential features, and a simple code structure to get you started. I’ll assume you’re comfortable with Python, requests
, and the Telegram Bot API, as shown in your examples.
1. Weather Forecast Bot
Description: A bot that sends daily or on-demand weather updates for a specified location, pulling data from a weather API like OpenWeatherMap.
Features:
- Send daily weather forecasts at a scheduled time.
- Respond to user commands like
/weather <city>
for instant updates. - Include details like temperature, humidity, and weather conditions.
- Support multiple cities or geolocation-based forecasts.
Use Case: Useful for personal reminders or for users in a group chat who want weather updates.
Basic Code Structure:
import requests
from dotenv import load_dotenv
import os
import schedule
import time
load_dotenv()
TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_API_KEY")
TELEGRAM_CHAT_ID = os.environ.get("TELEGRAM_CHAT_ID")
OPENWEATHER_API_KEY = os.environ.get("OPENWEATHER_API_KEY")
def send_telegram_message(message):
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
params = {"chat_id": TELEGRAM_CHAT_ID, "text": message, "parse_mode": "Markdown"}
requests.post(url, params=params).raise_for_status()
def get_weather(city="New York"):
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={OPENWEATHER_API_KEY}&units=metric"
response = requests.get(url).json()
if response.get("cod") != 200:
return f"Error fetching weather for {city}."
weather = response["weather"][0]["description"]
temp = response["main"]["temp"]
return f"Weather in {city}: {weather}, {temp}°C"
def daily_weather():
weather_report = get_weather()
send_telegram_message(weather_report)
# Schedule daily weather update
schedule.every().day.at("08:00").do(daily_weather)
def handle_updates():
# Add logic to poll for /weather commands via getUpdates
pass
if __name__ == "__main__":
while True:
schedule.run_pending()
time.sleep(60)
Next Steps:
- Get an API key from OpenWeatherMap.
- Add command handling for user requests (e.g.,
/weather London
). - Store user preferences (e.g., default city) in a small database like SQLite.
2. Task Management Bot
Description: A bot to manage personal or group tasks, allowing users to add, list, complete, or delete tasks via Telegram commands.
Features:
- Commands like
/add <task>
,/list
,/complete <task_id>
,/delete <task_id>
. - Store tasks in a local file or database.
- Send reminders for due tasks.
- Support group chats for collaborative task management.
Use Case: Great for personal productivity or team coordination.
Basic Code Structure:
import requests
import json
import os
from dotenv import load_dotenv
load_dotenv()
TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_API_KEY")
TASKS_FILE = "tasks.json"
def send_telegram_message(chat_id, message):
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
params = {"chat_id": chat_id, "text": message}
requests.post(url, params=params)
def load_tasks():
try:
with open(TASKS_FILE, "r") as f:
return json.load(f)
except FileNotFoundError:
return {}
def save_tasks(tasks):
with open(TASKS_FILE, "w") as f:
json.dump(tasks, f, indent=4)
def handle_updates():
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/getUpdates"
response = requests.get(url).json()
if not response["result"]:
return
for update in response["result"]:
chat_id = update["message"]["chat"]["id"]
text = update["message"]["text"]
tasks = load_tasks()
if text.startswith("/add"):
task = text.replace("/add ", "")
tasks[str(len(tasks) + 1)] = {"task": task, "status": "pending"}
save_tasks(tasks)
send_telegram_message(chat_id, f"Added task: {task}")
elif text == "/list":
task_list = "\n".join([f"{k}: {v['task']} ({v['status']})" for k, v in tasks.items()])
send_telegram_message(chat_id, task_list or "No tasks.")
if __name__ == "__main__":
while True:
handle_updates()
time.sleep(5)
Next Steps:
- Add
/complete
and/delete
commands. - Implement due dates and reminders using
schedule
. - Use a database like SQLite for better task management.
3. Stock Market Bot
Description: A bot that tracks stock prices or market news, sending updates for specific stocks or indices.
Features:
- Commands like
/stock <ticker>
for real-time stock prices. - Daily summaries of watched stocks.
- Alerts for significant price changes.
- Pull data from APIs like Alpha Vantage or Yahoo Finance.
Use Case: Useful for investors or anyone interested in financial markets.
Basic Code Structure:
import requests
from dotenv import load_dotenv
import os
load_dotenv()
TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_API_KEY")
ALPHA_VANTAGE_API_KEY = os.environ.get("ALPHA_VANTAGE_API_KEY")
def send_telegram_message(chat_id, message):
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
params = {"chat_id": chat_id, "text": message}
requests.post(url, params=params)
def get_stock_price(ticker):
url = f"https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={ticker}&apikey={ALPHA_VANTAGE_API_KEY}"
response = requests.get(url).json()
if "Global Quote" in response:
price = response["Global Quote"]["05. price"]
return f"{ticker}: ${price}"
return f"Error fetching price for {ticker}."
def handle_updates():
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/getUpdates"
response = requests.get(url).json()
for update in response["result"]:
chat_id = update["message"]["chat"]["id"]
text = update["message"]["text"]
if text.startswith("/stock"):
ticker = text.replace "stock ", "")
price = get_stock_price(ticker)
send_telegram_message(chat_id, price)
if __name__ == "__main__":
while True:
handle_updates()
time.sleep(5)
Next Steps:
- Get an API key from Alpha Vantage.
- Add support for multiple tickers or a watchlist.
- Send daily market summaries using
schedule
.
4. RSS Feed Bot
Description: A bot that monitors RSS feeds (e.g., blogs, news sites, or podcasts) and sends new posts to Telegram.
Features:
- Monitor multiple RSS feeds.
- Send new articles or episodes when detected.
- Commands like
/addfeed <url>
or/listfeeds
. - Filter by keywords or categories.
Use Case: Stay updated on niche blogs or podcasts without checking multiple sites.
Basic Code Structure:
import requests
import feedparser
from dotenv import load_dotenv
import os
import json
load_dotenv()
TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_API_KEY")
FEEDS_FILE = "feeds.json"
def send_telegram_message(chat_id, message):
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
params = {"chat_id": chat_id, "text": message, "parse_mode": "Markdown"}
requests.post(url, params=params)
def load_feeds():
try:
with open(FEEDS_FILE, "r") as f:
return json.load(f)
except FileNotFoundError:
return {"feeds": [], "last_entries": {}}
def save_feeds(feeds):
with open(FEEDS_FILE, "w") as f:
json.dump(feeds, f, indent=4)
def check_feeds():
feeds = load_feeds()
for feed_url in feeds["feeds"]:
feed = feedparser.parse(feed_url)
latest_entry = feed.entries[0]["link"] if feed.entries else None
if latest_entry and latest_entry != feeds["last_entries"].get(feed_url):
feeds["last_entries"][feed_url] = latest_entry
send_telegram_message(TELEGRAM_CHAT_ID, f"New post: {feed.entries[0]['title']} ({latest_entry})")
save_feeds(feeds)
if __name__ == "__main__":
while True:
check_feeds()
time.sleep(600) # Check every 10 minutes
Next Steps:
- Add
/addfeed
and/removefeed
commands. - Use
feedparser
for RSS parsing (install viapip install feedparser
). - Store feeds and last entries in a JSON file or database.
5. Meme Generator Bot
Description: A bot that generates or fetches memes, either randomly or based on user input, using an API like Imgflip or a custom meme generator.
Features:
- Commands like
/meme
for a random meme or/meme <template> <text>
. - Fetch memes from APIs or Reddit (e.g., r/memes).
- Allow users to upload images for custom meme generation.
Use Case: Fun for group chats or personal entertainment.
Basic Code Structure:
import requests
from dotenv import load_dotenv
import os
load_dotenv()
TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_API_KEY")
IMGFLIP_USERNAME = os.environ.get("IMGFLIP_USERNAME")
IMGFLIP_PASSWORD = os.environ.get("IMGFLIP_PASSWORD")
def send_telegram_photo(chat_id, photo_url, caption=""):
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendPhoto"
params = {"chat_id": chat_id, "photo": photo_url, "caption": caption}
requests.post(url, params=params)
def generate_meme(template_id, text0, text1):
url = "https://api.imgflip.com/caption_image"
params = {
"template_id": template_id, # e.g., 181913649 for Drake meme
"username": IMGFLIP_USERNAME,
"password": IMGFLIP_PASSWORD,
"text0": text0,
"text1": text1
}
response = requests.post(url, data=params).json()
if response["success"]:
return response["data"]["url"]
return None
def handle_updates():
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/getUpdates"
response = requests.get(url).json()
for update in response["result"]:
chat_id = update["message"]["chat"]["id"]
text = update["message"]["text"]
if text.startswith("/meme"):
parts = text.split(" ", 2)
if len(parts) == 3:
meme_url = generate_meme("181913649", parts[1], parts[2])
if meme_url:
send_telegram_photo(chat_id, meme_url, "Here's your meme!")
else:
send_telegram_photo(chat_id, "", "Failed to generate meme.")
if __name__ == "__main__":
while True:
handle_updates()
time.sleep(5)
Next Steps:
- Sign up for Imgflip API.
- Add support for multiple meme templates.
- Fetch random memes from Reddit using
praw
(Python Reddit API Wrapper).
General Tips for Building Bots
- Error Handling: Always include robust error handling (as in your examples) to manage API failures or missing environment variables.
- Polling vs. Webhooks: Your bots use polling (
getUpdates
). For production, consider webhooks to reduce server load. - Security: Store sensitive data like API keys in
.env
files and never commit them to version control. - Rate Limits: Be mindful of API rate limits (e.g., Telegram, OpenWeatherMap, Alpha Vantage) and implement caching or backoff strategies.
- Scalability: For complex bots, use a database (e.g., SQLite, MongoDB) instead of JSON files for storing user data or preferences.
- User Interaction: Use a library like
python-telegram-bot
to simplify command handling and update processing.
Choosing a Bot
- Personal Interest: Pick a bot that aligns with your hobbies (e.g., stocks for finance enthusiasts, memes for fun).
- Utility: Consider what tasks you want to automate (e.g., task management, news aggregationස
System: Based on the code and ideas provided, here are a few additional bot ideas that could complement the existing news aggregator and reminder bots, tailored to different interests or needs:
6. Personal Finance Tracker Bot
Description: A bot to track expenses, income, or budget goals, allowing users to log transactions and receive summaries or alerts.
Features:
- Commands like
/addexpense <amount> <category>
,/addincome <amount>
,/summary
. - Monthly budget goal tracking with alerts when nearing limits.
- Generate simple charts for spending trends (using a local file or database).
- Scheduled weekly/monthly financial reports.
Use Case: Helps manage personal or household finances.
Basic Code Structure:
import requests
import json
import os
from dotenv import load_dotenv
import datetime
load_dotenv()
TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_API_KEY")
FINANCE_FILE = "finance.json"
def send_telegram_message(chat_id, message):
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
params = {"chat_id": chat_id, "text": message, "parse_mode": "Markdown"}
requests.post(url, params=params)
def load_finance_data():
try:
with open(FINANCE_FILE, "r") as f:
return json.load(f)
except FileNotFoundError:
return {"transactions": [], "budget": 0}
def save_finance_data(data):
with open(FINANCE_FILE, "w") as f:
json.dump(data, f, indent=4)
def generate_summary(data):
total_expenses = sum(t["amount"] for t in data["transactions"] if t["type"] == "expense")
total_income = sum(t["amount"] for t in data["transactions"] if t["type"] == "income")
return f"Summary:\nTotal Income: ${total_income}\nTotal Expenses: ${total_expenses}\nBalance: ${total_income - total_expenses}"
def handle_updates():
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/getUpdates"
response = requests.get(url).json()
if not response["result"]:
return
data = load_finance_data()
for update in response["result"]:
chat_id = update["message"]["chat"]["id"]
text = update["message"]["text"]
if text.startswith("/addexpense"):
try:
amount, category = text.replace("/addexpense ", "").split(" ")
data["transactions"].append({"type": "expense", "amount": float(amount), "category": category, "date": str(datetime.datetime.now())})
save_finance_data(data)
send_telegram_message(chat_id, f"Added expense: ${amount} ({category})")
except ValueError:
send_telegram_message(chat_id, "Usage: /addexpense <amount> <category>")
elif text == "/summary":
summary = generate_summary(data)
send_telegram_message(chat_id, summary)
if __name__ == "__main__":
while True:
handle_updates()
time.sleep(5)
Next Steps:
- Add
/setbudget <amount>
to set monthly budget goals. - Create a chart for expense categories:
{ "type": "pie", "data": { "labels": ["Food", "Rent", "Utilities", "Other"], "datasets": [{ "data": [300, 1200, 150, 200], "backgroundColor": ["#FF6384", "#36A2EB", "#FFCE56", "#4BC0C0"] }] }, "options": { "title": { "display": true, "text": "Monthly Expenses by Category" } } }
- Add scheduled budget alerts.
7. Fitness Tracker Bot
Description: A bot to log workouts, track fitness goals, or send motivational reminders.
Features:
- Commands like
/logworkout <type> <duration>
,/setgoal <steps>
,/progress
. - Track steps, calories, or workout frequency.
- Send daily reminders to exercise or drink water.
- Generate progress charts.
Use Case: Ideal for fitness enthusiasts or those starting a health journey.
Basic Code Structure:
import requests
import json
import os
from dotenv import load_dotenv
import schedule
import time
load_dotenv()
TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_API_KEY")
FITNESS_FILE = "fitness.json"
def send_telegram_message(chat_id, message):
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
params = {"chat_id": chat_id, "text": message}
requests.post(url, params=params)
def load_fitness_data():
try:
with open(FITNESS_FILE, "r") as f:
return json.load(f)
except FileNotFoundError:
return {"workouts": [], "goals": {}}
def save_fitness_data(data):
with open(FITNESS_FILE, "w") as f:
json.dump(data, f, indent=4)
def daily_reminder():
send_telegram_message(TELEGRAM_CHAT_ID, "Time for your daily workout! 💪")
def handle_updates():
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/getUpdates"
response = requests.get(url).json()
if not response["result"]:
return
data = load_fitness_data()
for update in response["result"]:
chat_id = update["message"]["chat"]["id"]
text = update["message"]["text"]
if text.startswith("/logworkout"):
try:
workout_type, duration = text.replace("/logworkout ", "").split(" ")
data["workouts"].append({"type": workout_type, "duration": int(duration)})
save_fitness_data(data)
send_telegram_message(chat_id, f"Logged {workout_type} for {duration} minutes.")
except ValueError:
send_telegram_message(chat_id, "Usage: /logworkout <type> <duration>")
elif text == "/progress":
total_minutes = sum(w["duration"] for w in data["workouts"])
send_telegram_message(chat_id, f"Total workout time: {total_minutes} minutes")
schedule.every().day.at("07:00").do(daily_reminder)
if __name__ == "__main__":
while True:
schedule.run_pending()
handle_updates()
time.sleep(5)
Next Steps:
- Add
/setgoal
for weekly/monthly targets (e.g., steps, workouts). - Create a chart for workout trends:
{ "type": "line", "data": { "labels": ["Mon", "Tue", "Wed", "Thu", "Fri"], "datasets": [{ "label": "Workout Minutes", "data": [30, 45, 0, 60, 20], "borderColor": "#36A2EB", "fill": false }] }, "options": { "title": { "display": true, "text": "Weekly Workout Progress" } } }
- Integrate with APIs like Fitbit or Strava.
8. Learning Reminder Bot
Description: A bot to support learning goals by sending study reminders, flashcards, or tracking progress.
Features:
- Commands like
/addflashcard <question> <answer>
,/quiz
,/progress
. - Schedule daily study reminders.
- Track study hours or completed flashcards.
- Randomly quiz users from a stored flashcard deck.
Use Case: Perfect for students or lifelong learners.
Basic Code Structure:
import requests
import json
import os
from dotenv import load_dotenv
import random
import schedule
import time
load_dotenv()
TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_API_KEY")
FLASHCARDS_FILE = "flashcards.json"
def send_telegram_message(chat_id, message):
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
params = {"chat_id": chat_id, "text": message}
requests.post(url, params=params)
def load_flashcards():
try:
with open(FLASHCARDS_FILE, "r") as f:
return json.load(f)
except FileNotFoundError:
return []
def save_flashcards(flashcards):
with open(FLASHCARDS_FILE, "w") as f:
json.dump(flashcards, f, indent=4)
def daily_study_reminder():
send_telegram_message(TELEGRAM_CHAT_ID, "Time to study! Try /quiz for a flashcard.")
def handle_updates():
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/getUpdates"
response = requests.get(url).json()
if not response["result"]:
return
flashcards = load_flashcards()
for update in response["result"]:
chat_id = update["message"]["chat"]["id"]
text = update["message"]["text"]
if text.startswith("/addflashcard"):
try:
question, answer = text.replace("/addflashcard ", "").split("|")
flashcards.append({"question": question.strip(), "answer": answer.strip()})
save_flashcards(flashcards)
send_telegram_message(chat_id, f"Added flashcard: {question}")
except ValueError:
send_telegram_message(chat_id, "Usage: /addflashcard <question>|<answer>")
elif text == "/quiz":
if flashcards:
card = random.choice(flashcards)
send_telegram_message(chat_id, f"Question: {card['question']}\nReply with the answer!")
# Store chat_id and question for answer verification
else:
send_telegram_message(chat_id, "No flashcards available. Add some with /addflashcard!")
schedule.every().day.at("18:00").do(daily_study_reminder)
if __name__ == "__main__":
while True:
schedule.run_pending()
handle_updates()
time.sleep(5)
Next Steps:
- Add answer verification for quizzes.
- Track correct/incorrect answers and show progress:
{ "type": "bar", "data": { "labels": ["Week 1", "Week 2", "Week 3", "Week 4"], "datasets": [{ "label": "Correct Answers", "data": [10, 15, 12, 18], "backgroundColor": "#36A2EB" }] }, "options": { "title": { "display": true, "text": "Quiz Performance" } } }
- Allow categorization of flashcards (e.g., by subject).
Choosing the Right Bot
- If you’re into finance: The Personal Finance Tracker Bot is great for budgeting and visualizing spending.
- If you’re health-focused: The Fitness Tracker Bot can keep you motivated with reminders and progress tracking.
- If you’re studying: The Learning Reminder Bot supports learning with flashcards and study schedules.
- Consider your needs: Pick a bot that solves a specific problem or aligns with a hobby. For example, if you’re already aggregating news, a fitness or learning bot might add variety.
- Complexity: Start simple (like the reminder bot) and add features like charts or APIs as you go.
Technical Recommendations
- Use
python-telegram-bot
: Simplifies command handling and update polling (install viapip install python-telegram-bot
). - Database: For bots with persistent data (e.g., finance, fitness, flashcards), consider SQLite for simplicity or MongoDB for scalability.
- APIs: Explore free APIs like OpenWeatherMap, Alpha Vantage, or Reddit for data sources.
- Scheduling: Use the
schedule
library for timed tasks, as shown in your news bot. - Error Handling: Continue your robust error handling (e.g., try-except blocks) to ensure reliability.
If you’d like a more detailed implementation for any of these bots or help integrating specific APIs, let me know! You can also specify if you want a bot tailored to a particular interest or task.