-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_generator.py
More file actions
385 lines (335 loc) · 15.4 KB
/
message_generator.py
File metadata and controls
385 lines (335 loc) · 15.4 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env python3
"""
Professional Message Template Generator for WhatsApp
"""
import flet as ft
import os
import json
from PIL import Image, ImageDraw, ImageFont
import io
import base64
class MessageTemplateGenerator:
def __init__(self):
self.templates = [
{
"name": "Placement Announcement",
"template": "🎓 Heartiest Congratulations!\n\n*AMAZING PLACEMENT - {year}*\n\n{candidates}\n\n*{college_name}*\n\nFor admission details, contact us now!"
},
{
"name": "Course Promotion",
"template": "🚀 *NEW BATCH STARTING SOON!*\n\n📚 {course_name}\n⏰ Starting: {start_date}\n💰 Fee: {fee}\n\n✅ Limited seats available\n✅ Placement assistance\n\nRegister now! 📞"
},
{
"name": "Event Invitation",
"template": "🎉 *YOU'RE INVITED!* 🎉\n\n📅 {event_name}\n📆 Date: {event_date}\n🕒 Time: {event_time}\n📍 Venue: {venue}\n\nSpecial Guest: {guest}\n\nRSVP by {rsvp_date}"
},
{
"name": "Offer Announcement",
"template": "🔥 *SPECIAL OFFER!* 🔥\n\n💯 {offer_title}\n\n🏷️ {discount}% OFF\n⏰ Valid till: {valid_till}\n\nUse code: *{coupon_code}*\n\nHurry! Limited time offer."
}
]
self.candidate_template = "*{name}*\n{role} | {company}\nPackage: {salary} LPA\n"
# Default values
self.default_values = {
"year": "2025",
"college_name": "VSB Engineering College",
"course_name": "Python Full Stack Development",
"start_date": "August 15, 2025",
"fee": "₹25,000",
"event_name": "Tech Summit 2025",
"event_date": "September 10, 2025",
"event_time": "10:00 AM",
"venue": "Main Auditorium",
"guest": "Mr. Sundar Pichai",
"rsvp_date": "September 1, 2025",
"offer_title": "Summer Coding Bootcamp",
"discount": "30",
"valid_till": "July 31, 2025",
"coupon_code": "SUMMER30"
}
# Candidates
self.candidates = [
{"name": "Dinesh S", "role": "SDE Intern", "company": "Amazon", "salary": "47"},
{"name": "Irfan Ahamed S", "role": "SDE Intern", "company": "Amazon", "salary": "47"},
{"name": "Thilakraman R V", "role": "SDE Intern", "company": "Google", "salary": "44"},
{"name": "Deva Dharshini B", "role": "SDE Intern", "company": "Microsoft", "salary": "44"},
{"name": "Surender S", "role": "SDE Intern", "company": "Accenture", "salary": "40"}
]
def main(self, page: ft.Page):
page.title = "Professional Message Generator"
page.theme_mode = ft.ThemeMode.DARK
page.window_width = 1000
page.window_height = 800
page.window_resizable = True
# Template selector
self.template_dropdown = ft.Dropdown(
label="Select Template",
width=400,
options=[ft.dropdown.Option(t["name"]) for t in self.templates],
value=self.templates[0]["name"],
on_change=self.update_template
)
# Template fields container
self.fields_container = ft.Column(spacing=10)
# Candidate fields (for placement template)
self.candidates_container = ft.Column(spacing=10)
# Preview section
self.preview_text = ft.TextField(
label="Message Preview",
multiline=True,
min_lines=10,
max_lines=15,
read_only=True,
width=600
)
# Image preview
self.image_preview = ft.Image(
width=600,
height=400,
fit=ft.ImageFit.CONTAIN,
visible=False
)
# Buttons
self.generate_image_btn = ft.ElevatedButton(
"Generate Image",
icon=ft.Icons.IMAGE,
on_click=self.generate_image
)
self.copy_text_btn = ft.ElevatedButton(
"Copy Text",
icon=ft.Icons.COPY,
on_click=self.copy_text
)
self.save_template_btn = ft.ElevatedButton(
"Save to WhatsApp Sender",
icon=ft.Icons.SAVE,
on_click=self.save_to_whatsapp
)
# Layout
page.add(
ft.Container(
content=ft.Column([
# Header
ft.Container(
content=ft.Row([
ft.Icon(ft.Icons.MESSAGE, size=40, color=ft.Colors.BLUE),
ft.Text("Professional Message Generator", size=28, weight=ft.FontWeight.BOLD),
]),
padding=20,
bgcolor=ft.Colors.ON_SURFACE_VARIANT,
border_radius=10,
margin=ft.margin.only(bottom=20)
),
# Template selector
ft.Container(
content=ft.Column([
ft.Text("Select Template", size=20, weight=ft.FontWeight.BOLD),
self.template_dropdown,
]),
padding=15,
bgcolor=ft.colors.SURFACE_VARIANT,
border_radius=10,
margin=ft.margin.only(bottom=20)
),
# Template fields
ft.Container(
content=ft.Column([
ft.Text("Template Fields", size=20, weight=ft.FontWeight.BOLD),
self.fields_container,
]),
padding=15,
bgcolor=ft.colors.SURFACE_VARIANT,
border_radius=10,
margin=ft.margin.only(bottom=20)
),
# Candidate fields (for placement template)
ft.Container(
content=ft.Column([
ft.Row([
ft.Text("Candidates", size=20, weight=ft.FontWeight.BOLD),
ft.ElevatedButton("Add Candidate", icon=ft.Icons.ADD, on_click=self.add_candidate)
]),
self.candidates_container,
]),
padding=15,
bgcolor=ft.colors.SURFACE_VARIANT,
border_radius=10,
margin=ft.margin.only(bottom=20),
visible=True # Only visible for placement template
),
# Preview
ft.Container(
content=ft.Column([
ft.Text("Preview", size=20, weight=ft.FontWeight.BOLD),
self.preview_text,
self.image_preview,
ft.Row([
self.generate_image_btn,
self.copy_text_btn,
self.save_template_btn
])
]),
padding=15,
bgcolor=ft.colors.SURFACE_VARIANT,
border_radius=10
)
]),
padding=20
)
)
self.page = page
# Initialize with first template
self.update_template(None)
def update_template(self, e):
"""Update fields based on selected template"""
template_name = self.template_dropdown.value
template = next((t for t in self.templates if t["name"] == template_name), None)
if not template:
return
# Clear existing fields
self.fields_container.controls.clear()
# Add fields based on template
if template_name == "Placement Announcement":
self.fields_container.controls.extend([
ft.TextField(label="Year", value=self.default_values["year"], width=400),
ft.TextField(label="College Name", value=self.default_values["college_name"], width=400)
])
# Show candidates section
self.candidates_container.visible = True
self.update_candidates()
elif template_name == "Course Promotion":
self.fields_container.controls.extend([
ft.TextField(label="Course Name", value=self.default_values["course_name"], width=400),
ft.TextField(label="Start Date", value=self.default_values["start_date"], width=400),
ft.TextField(label="Fee", value=self.default_values["fee"], width=400)
])
self.candidates_container.visible = False
elif template_name == "Event Invitation":
self.fields_container.controls.extend([
ft.TextField(label="Event Name", value=self.default_values["event_name"], width=400),
ft.TextField(label="Event Date", value=self.default_values["event_date"], width=400),
ft.TextField(label="Event Time", value=self.default_values["event_time"], width=400),
ft.TextField(label="Venue", value=self.default_values["venue"], width=400),
ft.TextField(label="Guest", value=self.default_values["guest"], width=400),
ft.TextField(label="RSVP Date", value=self.default_values["rsvp_date"], width=400)
])
self.candidates_container.visible = False
elif template_name == "Offer Announcement":
self.fields_container.controls.extend([
ft.TextField(label="Offer Title", value=self.default_values["offer_title"], width=400),
ft.TextField(label="Discount (%)", value=self.default_values["discount"], width=400),
ft.TextField(label="Valid Till", value=self.default_values["valid_till"], width=400),
ft.TextField(label="Coupon Code", value=self.default_values["coupon_code"], width=400)
])
self.candidates_container.visible = False
# Update preview
self.update_preview()
self.page.update()
def update_candidates(self):
"""Update candidate fields"""
self.candidates_container.controls.clear()
for i, candidate in enumerate(self.candidates):
self.candidates_container.controls.append(
ft.Container(
content=ft.Column([
ft.Text(f"Candidate {i+1}", weight=ft.FontWeight.BOLD),
ft.Row([
ft.TextField(label="Name", value=candidate["name"], width=200),
ft.TextField(label="Role", value=candidate["role"], width=200),
ft.TextField(label="Company", value=candidate["company"], width=200),
ft.TextField(label="Salary (LPA)", value=candidate["salary"], width=100),
ft.IconButton(
icon=ft.Icons.DELETE,
on_click=lambda e, idx=i: self.remove_candidate(idx)
)
])
]),
padding=10,
border=ft.border.all(1, ft.colors.GREY_400),
border_radius=5,
margin=5
)
)
def add_candidate(self, e):
"""Add a new candidate"""
self.candidates.append({
"name": "New Candidate",
"role": "SDE Intern",
"company": "Company",
"salary": "40"
})
self.update_candidates()
self.update_preview()
self.page.update()
def remove_candidate(self, index):
"""Remove a candidate"""
if len(self.candidates) > 1: # Keep at least one candidate
self.candidates.pop(index)
self.update_candidates()
self.update_preview()
self.page.update()
def update_preview(self):
"""Update the preview text"""
template_name = self.template_dropdown.value
template = next((t for t in self.templates if t["name"] == template_name), None)
if not template:
return
# Get field values
field_values = {}
for control in self.fields_container.controls:
if isinstance(control, ft.TextField):
field_values[control.label.lower().replace(' ', '_')] = control.value
# Format template
message = template["template"]
# Special handling for candidates in placement template
if template_name == "Placement Announcement":
candidates_text = ""
for i, container in enumerate(self.candidates_container.controls):
candidate_data = {}
for control in container.content.controls[1].controls:
if isinstance(control, ft.TextField):
candidate_data[control.label.lower().replace(' ', '_')] = control.value
if candidate_data:
candidates_text += self.candidate_template.format(
name=candidate_data.get("name", ""),
role=candidate_data.get("role", ""),
company=candidate_data.get("company", ""),
salary=candidate_data.get("salary_(lpa)", "")
)
field_values["candidates"] = candidates_text
# Format the message with field values
try:
formatted_message = message.format(**field_values)
self.preview_text.value = formatted_message
except KeyError as e:
self.preview_text.value = f"Error: Missing field {e}"
self.page.update()
def generate_image(self, e):
"""Generate an image from the template"""
# This would generate an image based on the template
# For now, just show a placeholder
self.image_preview.visible = True
self.page.update()
def copy_text(self, e):
"""Copy the preview text to clipboard"""
self.page.set_clipboard(self.preview_text.value)
self.show_snackbar("Message copied to clipboard!")
def save_to_whatsapp(self, e):
"""Save the template to WhatsApp sender"""
try:
# Update the WhatsApp GUI with this message
with open("whatsapp_message.txt", "w", encoding="utf-8") as f:
f.write(self.preview_text.value)
self.show_snackbar("Message saved to WhatsApp sender!")
except Exception as ex:
self.show_snackbar(f"Error saving: {str(ex)}")
def show_snackbar(self, message):
"""Show a snackbar message"""
self.page.snack_bar = ft.SnackBar(content=ft.Text(message))
self.page.snack_bar.open = True
self.page.update()
def main():
app = MessageTemplateGenerator()
ft.app(target=app.main)
if __name__ == "__main__":
main()