-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
368 lines (295 loc) Β· 10.3 KB
/
bot.py
File metadata and controls
368 lines (295 loc) Β· 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
from telegram import (
Update,
InlineKeyboardButton,
InlineKeyboardMarkup,
)
from telegram.ext import (
ApplicationBuilder,
CommandHandler,
CallbackQueryHandler,
MessageHandler,
ContextTypes,
filters,
)
import threading
import schedule
import time
from config import TELEGRAM_TOKEN, OWNER_ID
from security import is_owner
from buffer import (
add_daily_log,
add_achievement,
add_failure,
read_buffer
)
from github_logger import commit_buffer
from search_engine import search_logs
from export_engine import export_data
from ai_engine import generate_daily_summary
# =====================================================
# SIMPLE IN-MEMORY HISTORY (last messages)
# =====================================================
history = []
# =====================================================
# KEYBOARDS
# =====================================================
def home_keyboard():
return InlineKeyboardMarkup([
[InlineKeyboardButton("π Daily Log", callback_data="daily")],
[InlineKeyboardButton("π Achievement", callback_data="achievement")],
[InlineKeyboardButton("β οΈ Failure", callback_data="failure")],
[InlineKeyboardButton("π Search Logs", callback_data="search")],
[InlineKeyboardButton("π Stats", callback_data="stats")],
[InlineKeyboardButton("π Commit Now", callback_data="commit")],
])
def back_home_keyboard():
return InlineKeyboardMarkup([
[InlineKeyboardButton("π Back to Home", callback_data="home")]
])
# =====================================================
# START / HOME
# =====================================================
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not is_owner(update.effective_user.id):
await update.message.reply_text("β Unauthorized.")
return
await show_home(update, context)
async def show_home(update, context):
text = (
"π§ *Life Logger*\n\n"
"Your private life operating system.\n\n"
"β’ Log your days\n"
"β’ Record achievements\n"
"β’ Learn from failures\n"
"β’ Search your past\n"
"β’ Track consistency\n\n"
"_Everything you log becomes future intelligence._"
)
if update.callback_query:
await update.callback_query.edit_message_text(
text,
parse_mode="Markdown",
reply_markup=home_keyboard()
)
else:
await update.message.reply_text(
text,
parse_mode="Markdown",
reply_markup=home_keyboard()
)
# =====================================================
# BUTTON HANDLER
# =====================================================
async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
if not is_owner(query.from_user.id):
await query.edit_message_text("β Unauthorized.")
return
context.user_data.clear()
action = query.data
if action == "home":
await show_home(update, context)
elif action == "daily":
context.user_data["state"] = "daily"
await query.edit_message_text(
"π *Daily Log*\n\nSend anything you want to record.",
parse_mode="Markdown",
reply_markup=back_home_keyboard()
)
elif action == "achievement":
context.user_data["state"] = "ach_title"
await query.edit_message_text(
"π *Achievement*\n\nSend the TITLE:",
parse_mode="Markdown",
reply_markup=back_home_keyboard()
)
elif action == "failure":
context.user_data["state"] = "fail_title"
await query.edit_message_text(
"β οΈ *Failure*\n\nSend a short TITLE:",
parse_mode="Markdown",
reply_markup=back_home_keyboard()
)
elif action == "search":
context.user_data["state"] = "search"
await query.edit_message_text(
"π *Search Logs*\n\nSend a keyword to search your life history.",
parse_mode="Markdown",
reply_markup=back_home_keyboard()
)
elif action == "stats":
await send_stats(update, context)
elif action == "commit":
count = commit_buffer()
await query.edit_message_text(
f"π Commit Complete\n\nβ
{count} entries saved.",
reply_markup=home_keyboard()
)
# =====================================================
# MESSAGE HANDLER (STATE MACHINE)
# =====================================================
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not is_owner(update.effective_user.id):
return
text = update.message.text.strip()
state = context.user_data.get("state")
history.append(text)
if len(history) > 5:
history.pop(0)
try:
# DAILY
if state == "daily":
add_daily_log(text)
await update.message.reply_text(
"β
Logged.",
reply_markup=home_keyboard()
)
# ACHIEVEMENT FLOW
elif state == "ach_title":
context.user_data["title"] = text
context.user_data["state"] = "ach_desc"
await update.message.reply_text("Describe it:")
elif state == "ach_desc":
context.user_data["desc"] = text
context.user_data["state"] = "ach_how"
await update.message.reply_text("How did you achieve it?")
elif state == "ach_how":
add_achievement(
context.user_data["title"],
context.user_data["desc"],
text
)
await update.message.reply_text(
"π Achievement saved.",
reply_markup=home_keyboard()
)
# FAILURE FLOW
elif state == "fail_title":
context.user_data["title"] = text
context.user_data["state"] = "fail_reason"
await update.message.reply_text("What caused it?")
elif state == "fail_reason":
context.user_data["reason"] = text
context.user_data["state"] = "fail_lesson"
await update.message.reply_text("What did you learn?")
elif state == "fail_lesson":
add_failure(
context.user_data["title"],
context.user_data["reason"],
text
)
await update.message.reply_text(
"β οΈ Failure logged.",
reply_markup=home_keyboard()
)
# SEARCH
elif state == "search":
results = search_logs(text)
if not results:
msg = "No results found."
else:
msg = "\n".join(results[:10])
await update.message.reply_text(
f"π Results:\n\n{msg}",
reply_markup=home_keyboard()
)
else:
await update.message.reply_text(
"Choose an option from the menu π",
reply_markup=home_keyboard()
)
except Exception as e:
await update.message.reply_text(
"β Something went wrong.\nYour data is safe.",
reply_markup=home_keyboard()
)
print(e)
# =====================================================
# STATS
# =====================================================
async def send_stats(update, context):
buffer_data = read_buffer()
lines = buffer_data.split("\n") if buffer_data else []
daily = sum(1 for l in lines if "|| DAILY ||" in l)
ach = sum(1 for l in lines if "|| ACHIEVEMENT ||" in l)
fail = sum(1 for l in lines if "|| FAILURE ||" in l)
text = (
"π *Stats (Buffer)*\n\n"
f"π Daily: {daily}\n"
f"π Achievements: {ach}\n"
f"β οΈ Failures: {fail}\n\n"
"_Auto-commit runs every 6 hours._"
)
if update.callback_query:
await update.callback_query.edit_message_text(
text,
parse_mode="Markdown",
reply_markup=home_keyboard()
)
else:
await update.message.reply_text(
text,
parse_mode="Markdown",
reply_markup=home_keyboard()
)
# =====================================================
# AUTO COMMIT SCHEDULER
# =====================================================
def scheduler_loop(app):
def auto_commit():
count = commit_buffer()
if count > 0:
app.bot.send_message(
chat_id=OWNER_ID,
text=f"π€ Auto-Commit Done\n\nβ
{count} logs saved."
)
schedule.every(6).hours.do(auto_commit)
while True:
schedule.run_pending()
time.sleep(30)
async def export_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not is_owner(update.effective_user.id):
return
if not context.args:
await update.message.reply_text(
"Usage:\n\n"
"/export daily\n"
"/export achievements\n"
"/export failures\n"
"/export all"
)
return
category = context.args[0].lower()
data = export_data(category)
if not data:
await update.message.reply_text("Invalid category.")
return
# Telegram prefers files for large text
with open("export.txt", "w", encoding="utf-8") as f:
f.write(data)
await update.message.reply_document(
document=open("export.txt", "rb"),
filename=f"{category}_export.txt"
)
# =====================================================
# MAIN
# =====================================================
def main():
print("π Life Logger starting...")
app = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("stats", send_stats))
app.add_handler(CommandHandler("search", handle_message))
app.add_handler(CallbackQueryHandler(button_handler))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
app.add_handler(CommandHandler("export", export_command))
threading.Thread(
target=scheduler_loop,
args=(app,),
daemon=True
).start()
print("β
Bot running.")
app.run_polling()
if __name__ == "__main__":
main()