Mr-Help commited on
Commit
1df64e9
·
verified ·
1 Parent(s): b1c645f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +338 -5
main.py CHANGED
@@ -5,8 +5,308 @@ from fastapi import FastAPI, Request
5
 
6
  app = FastAPI()
7
 
8
- NOTION_SECRET = os.getenv("NOTION_SECRET") # ضع secret هنا أو كمتغير بيئة
9
- NOTION_VERSION = "2022-06-28"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
 
12
  async def get_page_title(page_id: str) -> str:
@@ -22,7 +322,6 @@ async def get_page_title(page_id: str) -> str:
22
 
23
  data = res.json()
24
 
25
- # نجيب الـ Title من الـ property الخاص بالاسم
26
  props = data.get("properties", {})
27
  for prop in props.values():
28
  if prop.get("type") == "title":
@@ -33,6 +332,30 @@ async def get_page_title(page_id: str) -> str:
33
  return "(No Title)"
34
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  @app.post("/webhook")
37
  async def webhook(request: Request):
38
  body = await request.json()
@@ -42,9 +365,10 @@ async def webhook(request: Request):
42
  timestamp = body.get("timestamp")
43
 
44
  print("\n===== NEW WEBHOOK EVENT =====")
 
45
 
46
- # لو الحدث page.created أو page.deleted
47
- if event_type in ["page.created", "page.deleted"]:
48
  title = await get_page_title(page_id)
49
 
50
  print(f"Event Type : {event_type}")
@@ -52,6 +376,15 @@ async def webhook(request: Request):
52
  print(f"Page ID : {page_id}")
53
  print(f"Timestamp : {timestamp}")
54
 
 
 
 
 
 
 
 
 
 
55
  else:
56
  print("Event received but ignored:", event_type)
57
 
 
5
 
6
  app = FastAPI()
7
 
8
+ # لو حابب تحط قيمة افتراضية للتست
9
+ NOTION_SECRET = os.getenv("NOTION_SECRET", "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")
10
+ NOTION_VERSION = "2025-09-03"
11
+
12
+
13
+ def build_content_template_children():
14
+ """نفس التمبلت بالملّي من كود 2 لكن كـ children فقط."""
15
+ return [
16
+ # ===== Assets link =====
17
+ {
18
+ "object": "block",
19
+ "type": "callout",
20
+ "callout": {
21
+ "rich_text": [
22
+ {
23
+ "type": "text",
24
+ "text": {"content": "Assets link - روابط اللقطات الخام"},
25
+ "annotations": {"bold": True}
26
+ },
27
+ {
28
+ "type": "text",
29
+ "text": {
30
+ "content": "\nهنا هنتم وضع روابط لقطات خام يمكن استخدامها في التصميم أو الفيديو"
31
+ }
32
+ }
33
+ ],
34
+ "icon": {"emoji": "📁"},
35
+ "color": "brown_background"
36
+ }
37
+ },
38
+
39
+ # سطر فاضي
40
+ {
41
+ "object": "block",
42
+ "type": "paragraph",
43
+ "paragraph": {"rich_text": []}
44
+ },
45
+
46
+ # ===== Media Buyer =====
47
+ {
48
+ "object": "block",
49
+ "type": "callout",
50
+ "callout": {
51
+ "rich_text": [
52
+ {
53
+ "type": "text",
54
+ "text": {"content": "Media Buyer"},
55
+ "annotations": {"bold": True}
56
+ },
57
+ {
58
+ "type": "text",
59
+ "text": {
60
+ "content": "\nهنا الجزء الخاص بالميديا باير (يتضمن طريقة تسمية الإعلان - الكانڤسر الخاصة بكل منصة - نسخ الكوبي المختلفة للإعلان)"
61
+ }
62
+ }
63
+ ],
64
+ "icon": {"emoji": "🎯"},
65
+ "color": "brown_background"
66
+ }
67
+ },
68
+
69
+ # Net Name callout منفصل
70
+ {
71
+ "object": "block",
72
+ "type": "callout",
73
+ "callout": {
74
+ "rich_text": [
75
+ {
76
+ "type": "text",
77
+ "text": {
78
+ "content": "Net Name = Name-(CreatorName/Vo)-(Offer)"
79
+ },
80
+ "annotations": {
81
+ "code": True,
82
+ "bold": True
83
+ }
84
+ }
85
+ ],
86
+ "icon": {"emoji": "🔤"},
87
+ "color": "yellow_background"
88
+ }
89
+ },
90
+
91
+ # سطر فاضي
92
+ {
93
+ "object": "block",
94
+ "type": "paragraph",
95
+ "paragraph": {"rich_text": []}
96
+ },
97
+
98
+ # ===== Ad Captions =====
99
+ {
100
+ "object": "block",
101
+ "type": "callout",
102
+ "callout": {
103
+ "rich_text": [
104
+ {
105
+ "type": "text",
106
+ "text": {"content": "Ad Captions"},
107
+ "annotations": {"bold": True}
108
+ },
109
+ {
110
+ "type": "text",
111
+ "text": {
112
+ "content": "\nهنا كابشنز المنصات اللي هيستخدمها الميديا باير أثناء إطلاق الإعلان"
113
+ }
114
+ }
115
+ ],
116
+ "icon": {"emoji": "✍️"},
117
+ "color": "purple_background"
118
+ }
119
+ },
120
+
121
+ # سطر فاضي
122
+ {
123
+ "object": "block",
124
+ "type": "paragraph",
125
+ "paragraph": {"rich_text": []}
126
+ },
127
+
128
+ # ===== منصات الكابشنز كـ H3 toggle =====
129
+ {
130
+ "object": "block",
131
+ "type": "heading_3",
132
+ "heading_3": {
133
+ "rich_text": [
134
+ {"type": "text", "text": {"content": "SNAPCHAT"}}
135
+ ],
136
+ "is_toggleable": True
137
+ }
138
+ },
139
+ {
140
+ "object": "block",
141
+ "type": "heading_3",
142
+ "heading_3": {
143
+ "rich_text": [
144
+ {"type": "text", "text": {"content": "INSTAGRAM"}}
145
+ ],
146
+ "is_toggleable": True
147
+ }
148
+ },
149
+ {
150
+ "object": "block",
151
+ "type": "heading_3",
152
+ "heading_3": {
153
+ "rich_text": [
154
+ {"type": "text", "text": {"content": "TIKTOK"}}
155
+ ],
156
+ "is_toggleable": True
157
+ }
158
+ },
159
+ {
160
+ "object": "block",
161
+ "type": "heading_3",
162
+ "heading_3": {
163
+ "rich_text": [
164
+ {"type": "text", "text": {"content": "Google"}}
165
+ ],
166
+ "is_toggleable": True
167
+ }
168
+ },
169
+
170
+ # سطر فاضي
171
+ {
172
+ "object": "block",
173
+ "type": "paragraph",
174
+ "paragraph": {"rich_text": []}
175
+ },
176
+
177
+ # ===== Copy Variations =====
178
+ {
179
+ "object": "block",
180
+ "type": "callout",
181
+ "callout": {
182
+ "rich_text": [
183
+ {
184
+ "type": "text",
185
+ "text": {"content": "Copy Variations"},
186
+ "annotations": {"bold": True}
187
+ },
188
+ {
189
+ "type": "text",
190
+ "text": {
191
+ "content": "\nهنا نسخ الكوبي اللي هنتم وضعها أثناء إطلاق الإعلان"
192
+ }
193
+ }
194
+ ],
195
+ "icon": {"emoji": "📄"},
196
+ "color": "gray_background"
197
+ }
198
+ },
199
+
200
+ # سطر فاضي
201
+ {
202
+ "object": "block",
203
+ "type": "paragraph",
204
+ "paragraph": {"rich_text": []}
205
+ },
206
+
207
+ # ===== جدول الكوبيز a / b =====
208
+ {
209
+ "object": "block",
210
+ "type": "table",
211
+ "table": {
212
+ "table_width": 2,
213
+ "has_column_header": True,
214
+ "has_row_header": False,
215
+ "children": [
216
+ {
217
+ "object": "block",
218
+ "type": "table_row",
219
+ "table_row": {
220
+ "cells": [
221
+ [
222
+ {
223
+ "type": "text",
224
+ "text": {"content": "Version"}
225
+ }
226
+ ],
227
+ [
228
+ {
229
+ "type": "text",
230
+ "text": {"content": "Copy"}
231
+ }
232
+ ]
233
+ ]
234
+ }
235
+ },
236
+ {
237
+ "object": "block",
238
+ "type": "table_row",
239
+ "table_row": {
240
+ "cells": [
241
+ [
242
+ {
243
+ "type": "text",
244
+ "text": {"content": "a"}
245
+ }
246
+ ],
247
+ [
248
+ {
249
+ "type": "text",
250
+ "text": {"content": ""}
251
+ }
252
+ ]
253
+ ]
254
+ }
255
+ },
256
+ {
257
+ "object": "block",
258
+ "type": "table_row",
259
+ "table_row": {
260
+ "cells": [
261
+ [
262
+ {
263
+ "type": "text",
264
+ "text": {"content": "b"}
265
+ }
266
+ ],
267
+ [
268
+ {
269
+ "type": "text",
270
+ "text": {"content": ""}
271
+ }
272
+ ]
273
+ ]
274
+ }
275
+ }
276
+ ]
277
+ }
278
+ },
279
+
280
+ # سطر فاضي
281
+ {
282
+ "object": "block",
283
+ "type": "paragraph",
284
+ "paragraph": {"rich_text": []}
285
+ },
286
+
287
+ # ===== NOTES =====
288
+ {
289
+ "object": "block",
290
+ "type": "callout",
291
+ "callout": {
292
+ "rich_text": [
293
+ {
294
+ "type": "text",
295
+ "text": {"content": "NOTES (References if needed)"},
296
+ "annotations": {"bold": True}
297
+ },
298
+ {
299
+ "type": "text",
300
+ "text": {
301
+ "content": "\nهنا لو في نوتس مهم نشتغل عليها جميعًا سواء كاتب المحتوى، المصمم أو الميديا باير"
302
+ }
303
+ }
304
+ ],
305
+ "icon": {"emoji": "📌"},
306
+ "color": "brown_background"
307
+ }
308
+ }
309
+ ]
310
 
311
 
312
  async def get_page_title(page_id: str) -> str:
 
322
 
323
  data = res.json()
324
 
 
325
  props = data.get("properties", {})
326
  for prop in props.values():
327
  if prop.get("type") == "title":
 
332
  return "(No Title)"
333
 
334
 
335
+ async def apply_template_to_page(page_id: str):
336
+ """Append التمبلت دي لجسم الصفحة (الـ body) باستخدام blocks API."""
337
+ children = build_content_template_children()
338
+
339
+ print(f"[TEMPLATE] Applying template to page: {page_id}")
340
+
341
+ async with httpx.AsyncClient() as client:
342
+ res = await client.patch(
343
+ f"https://api.notion.com/v1/blocks/{page_id}/children",
344
+ headers={
345
+ "Authorization": f"Bearer {NOTION_SECRET}",
346
+ "Notion-Version": NOTION_VERSION,
347
+ "Content-Type": "application/json",
348
+ },
349
+ json={"children": children},
350
+ )
351
+
352
+ print(f"[TEMPLATE] Status: {res.status_code}")
353
+ try:
354
+ print("[TEMPLATE] Response:", res.json())
355
+ except Exception:
356
+ print("[TEMPLATE] Raw Response:", res.text)
357
+
358
+
359
  @app.post("/webhook")
360
  async def webhook(request: Request):
361
  body = await request.json()
 
365
  timestamp = body.get("timestamp")
366
 
367
  print("\n===== NEW WEBHOOK EVENT =====")
368
+ print("Raw body:", body)
369
 
370
+ # نتعامل مع إنشاء صفحة جديدة فقط
371
+ if event_type == "page.created":
372
  title = await get_page_title(page_id)
373
 
374
  print(f"Event Type : {event_type}")
 
376
  print(f"Page ID : {page_id}")
377
  print(f"Timestamp : {timestamp}")
378
 
379
+ # نطبّق التمبلت على الـ Page دي
380
+ try:
381
+ await apply_template_to_page(page_id)
382
+ except Exception as e:
383
+ print("[ERROR] While applying template:", e)
384
+
385
+ elif event_type == "page.deleted":
386
+ print(f"Page deleted. ID: {page_id}, Timestamp: {timestamp}")
387
+
388
  else:
389
  print("Event received but ignored:", event_type)
390