Skip to content

Commit 10ae955

Browse files
author
Adriano Sanges
committed
Add Telegram bot integration for new property notifications
- Implement Telegram bot messaging functionality - Add python-telegram-bot dependency - Create new function to get and format new properties - Modify GitHub Actions workflow to run periodically - Update scan_properties.py to send Telegram messages for new listings
1 parent c31d4e2 commit 10ae955

File tree

6 files changed

+157
-5
lines changed

6 files changed

+157
-5
lines changed

.github/workflows/run-project.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ on:
44
push:
55
branches:
66
- main
7-
pull_request:
7+
schedule:
8+
- cron: '0 */3 * * *'
89

910
jobs:
1011
run-python:

real-estate-etl/database.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,8 @@ def clean_properties(con) -> None:
5151
con.sql(insert_query_only_new)
5252
con.sql(insert_query)
5353
con.sql("DELETE FROM main.properties;")
54+
55+
56+
def get_new_properties(con) -> None:
57+
df = con.sql("SELECT * FROM main.cleaned_properties;").pl()
58+
return df

real-estate-etl/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies = [
1010
"polars>=1.21.0",
1111
"pyarrow>=19.0.0",
1212
"python-dotenv>=1.0.1",
13+
"python-telegram-bot==13.13",
1314
"requests>=2.32.3",
1415
]
1516

real-estate-etl/scan_properties.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import polars
66
import duckdb
77
from scraper import parse_listing
8-
from database import clean_properties
8+
from database import clean_properties, get_new_properties
99
from dotenv import load_dotenv
1010
import os
11-
11+
from telegram_api import send_message, format_property_message
1212

1313

1414

@@ -24,14 +24,24 @@
2424
data = parse_listing(url)
2525

2626
polars_df = polars.DataFrame(data)
27-
print(polars_df)
2827

2928
con = duckdb.connect(f"md:{warehouse_name}?motherduck_token={motherduck_token}")
3029

3130
con.sql("create table if not exists main.properties as select * from polars_df")
3231

3332
clean_properties(con)
3433

35-
34+
new_properties = get_new_properties(con)
35+
# Iterate over the DataFrame and format each property
36+
messages = [format_property_message(row) for row in new_properties.iter_rows(named=True)]
37+
38+
# Send messages in chunks of two
39+
for i in range(0, len(messages), 2):
40+
# Get the current chunk of two messages
41+
message_chunk = messages[i:i+2]
42+
# Join the two messages with a separator
43+
full_message = "\n\n".join(message_chunk)
44+
# Send the combined message
45+
send_message(full_message)
3646

3747

real-estate-etl/telegram_api.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import telegram
2+
3+
4+
5+
TELEGRAM_BOT_API_KEY = '6596026864:AAFkT1-yG7abTakVEgGeqfiNyA-2mE3dO20'
6+
chat_id = '-4799482591'
7+
chat_tag = '@affittinapoliadriano'
8+
bot = telegram.Bot(TELEGRAM_BOT_API_KEY)
9+
10+
11+
# Function to format the message
12+
def format_property_message(row):
13+
return (
14+
f"🏠 **{row['title']}**\n"
15+
f"📍 Location: {row['city']}, {row['neighbourhood']}, {row['road']}\n"
16+
f"📏 Size: {row['square_meters']}\n"
17+
f"🏢 Floor: {row['floor']}\n"
18+
f"💰 Price: €{row['price']:,}\n"
19+
f"🔗 [View Listing]({row['url']})\n"
20+
"----------------------------------------"
21+
)
22+
23+
24+
def send_message(message):
25+
26+
bot.sendMessage(chat_id=chat_id, text=message, parse_mode=telegram.ParseMode.MARKDOWN)
27+

real-estate-etl/uv.lock

Lines changed: 108 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)