Spaces:
Runtime error
Runtime error
Kaveh
commited on
Create fetcher.py
Browse files- bot/fetcher.py +27 -0
bot/fetcher.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from telegram.ext import CallbackContext
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
# در حال حاضر فقط پیامهایی که خود ربات دریافت کرده رو نگه میداریم
|
| 6 |
+
# در مرحله بعدی میشه از database یا cache استفاده کرد
|
| 7 |
+
|
| 8 |
+
# حافظه موقتی پیامها
|
| 9 |
+
message_log = {}
|
| 10 |
+
|
| 11 |
+
def save_message(chat_id: int, text: str, timestamp: datetime):
|
| 12 |
+
if chat_id not in message_log:
|
| 13 |
+
message_log[chat_id] = []
|
| 14 |
+
message_log[chat_id].append((text, timestamp))
|
| 15 |
+
|
| 16 |
+
def fetch_messages(chat_id: int, count: int = 50, since: Optional[datetime] = None):
|
| 17 |
+
if chat_id not in message_log:
|
| 18 |
+
return []
|
| 19 |
+
|
| 20 |
+
messages = message_log[chat_id]
|
| 21 |
+
|
| 22 |
+
if since:
|
| 23 |
+
filtered = [text for text, ts in messages if ts >= since]
|
| 24 |
+
else:
|
| 25 |
+
filtered = [text for text, _ in messages]
|
| 26 |
+
|
| 27 |
+
return filtered[-count:]
|