Building a Telegram Bot for Home Notifications

Automate home notifications by building a Telegram bot that sends alerts from your smart home, scripts, or Python projects.
Building a Telegram Bot for Home Notifications
Automate home notifications by building a Telegram bot that sends alerts from your smart home, scripts, or Python projects.
Prerequisites
- Python 3.8+
- A Telegram account
- (Optional) Home Assistant instance
Step 1: Create Your Telegram Bot
- Open Telegram and search for @BotFather
- Send
/newbotand follow the prompts - Choose a name and username (must end in
bot) - Copy the bot token — keep it secret!
Step 2: Get Your Chat ID
- Search for your new bot and send it a message (anything)
- Visit:
https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates - Find
"chat":{"id":...}in the JSON response — that's your chat ID
Step 3: Send Notifications with Python
import requests
BOT_TOKEN = "your_bot_token_here"
CHAT_ID = "your_chat_id_here"
def send_message(text):
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
payload = {"chat_id": CHAT_ID, "text": text, "parse_mode": "Markdown"}
response = requests.post(url, json=payload)
return response.json()
if __name__ == "__main__":
send_message("🏠 *Home Alert*: Front door opened!")Install dependencies:
pip install requestsStep 4: Home Assistant Integration
Add to your configuration.yaml:
notify:
- name: telegram_bot
platform: telegram
chat_id: YOUR_CHAT_ID
token: YOUR_BOT_TOKENSend notifications from automations:
automation:
- alias: "Front Door Alert"
trigger:
- platform: state
entity_id: binary_sensor.front_door
to: "on"
action:
- service: notify.telegram_bot
data:
message: "🚪 Front door opened!"
data:
inline_keyboard:
- "Silence:/silence"Step 5: Advanced Features
Send Images
def send_photo(image_path, caption=""):
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendPhoto"
with open(image_path, "rb") as photo:
files = {"photo": photo}
data = {"chat_id": CHAT_ID, "caption": caption}
requests.post(url, files=files, data=data)Interactive Buttons
def send_with_buttons(text):
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
keyboard = {
"inline_keyboard": [
[{"text": "Turn on lights", "callback_data": "lights_on"}],
[{"text": "Status", "callback_data": "status"}]
]
}
payload = {"chat_id": CHAT_ID, "text": text, "reply_markup": keyboard}
requests.post(url, json=payload)Complete Example with Environment Variables
Create a .env file:
TELEGRAM_BOT_TOKEN=your_bot_token_here
TELEGRAM_CHAT_ID=your_chat_id_hereUpdate the script:
import requests
import os
from dotenv import load_dotenv
load_dotenv()
BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
def send_message(text, parse_mode="Markdown"):
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
payload = {"chat_id": CHAT_ID, "text": text, "parse_mode": parse_mode}
response = requests.post(url, json=payload)
return response.json()
def send_photo(image_path, caption=""):
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendPhoto"
with open(image_path, "rb") as photo:
files = {"photo": photo}
data = {"chat_id": CHAT_ID, "caption": caption}
requests.post(url, files=files, data=data)
def send_with_buttons(text, buttons):
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
keyboard = {"inline_keyboard": buttons}
payload = {"chat_id": CHAT_ID, "text": text, "reply_markup": keyboard}
requests.post(url, json=payload)
if __name__ == "__main__":
send_message("🏠 *Home Alert System Active*")Install additional dependency:
pip install python-dotenvUse Cases
- Motion detected → camera snapshot to Telegram
- Washing machine finished → notification
- Server down → immediate alert
- Daily weather forecast at 8 AM
- Power outage detection via smart plug
Home Assistant Automation Examples
Motion Alert with Snapshot
automation:
- alias: "Motion Alert with Snapshot"
trigger:
- platform: state
entity_id: binary_sensor.living_room_motion
to: "on"
action:
- service: camera.snapshot
target:
entity_id: camera.living_room
data:
filename: "/tmp/motion_snapshot.jpg"
- delay: "00:00:02"
- service: notify.telegram_bot
data:
message: "📷 Motion detected in living room!"
data:
photo:
- file: "/tmp/motion_snapshot.jpg"Daily Weather Report
automation:
- alias: "Morning Weather Report"
trigger:
- platform: time
at: "08:00:00"
action:
- service: notify.telegram_bot
data:
message: >
🌤 *Good Morning!*
Temperature: {{ states('sensor.outdoor_temperature') }}°C
Humidity: {{ states('sensor.outdoor_humidity') }}%
Condition: {{ states('sensor.weather_condition') }}Security Tips
- Store tokens in environment variables
- Use
.gitignoreto avoid committing secrets - Consider using a separate "home" chat for the bot
- Regularly rotate your bot token via @BotFather
- Don't hardcode credentials in your scripts
Troubleshooting
Bot not responding
- Check that you've started a chat with the bot
- Verify the token is correct
- Ensure your bot isn't blocked
Messages not arriving
- Confirm chat_id is correct
- Check if the bot has permission to send messages
- Verify your Home Assistant logs for errors
SSL Errors
If you encounter SSL errors, you may need to update certificates:
pip install --upgrade certifiConclusion
A Telegram bot is a simple yet powerful way to receive home notifications. Whether you're integrating with Home Assistant or building custom Python scripts, the Telegram API is beginner-friendly and reliable.
Start small with basic text notifications, then expand to images, buttons, and complex automations as you get comfortable.
Happy automating! The MDX file is ready at /Users/robintehofstee/telegram_bot_tutorial.mdx. It includes:
Frontmatter with metadata Step-by-step tutorial (bot creation, Python script, Home Assistant integration) Advanced features (images, buttons, environment variables) Automation examples (motion alerts, weather reports) Security tips & troubleshooting Upload it to your blog's blog/ directory and it should render correctly.

Written by Robin te Hofstee
Technology enthusiast with a deep love for programming. Sharing practical guides on React, Next.js, Linux and self-hosting.
Related Posts
How to Set Up a Python Web Server
A detailed tutorial on setting up a Python web server using Flask and deploying it locally.
Next.js App Router Deep Dive
A practical deep dive into the Next.js App Router: layouts, dynamic routes, Server and Client Components, data fetching, caching, Server Actions, route handlers, and the pitfalls to avoid.
Self-Hosting n8n on Proxmox
Deploying n8n on Proxmox using the Community Scripts LXC installer offers data privacy, unlimited execution, and full control compared to n8n Cloud, ideal for a homelab.


