-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflex_message_builder.py
More file actions
340 lines (324 loc) · 12.7 KB
/
flex_message_builder.py
File metadata and controls
340 lines (324 loc) · 12.7 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
from linebot.models import (
FlexSendMessage, BubbleContainer, BoxComponent,
TextComponent, ButtonComponent, SeparatorComponent,
CarouselContainer, URIAction, PostbackAction,
MessageAction
)
class FlexMessageBuilder:
"""建立 LINE Flex Message 卡片"""
@staticmethod
def create_reply_options_carousel(options):
"""建立回覆選項的輪播卡片"""
bubbles = []
for idx, option in enumerate(options):
bubble = BubbleContainer(
size='kilo',
header=BoxComponent(
layout='vertical',
contents=[
BoxComponent(
layout='horizontal',
contents=[
TextComponent(
text=option['emoji'],
size='sm',
flex=0
),
TextComponent(
text=option['title'],
weight='bold',
color='#1DB446',
size='sm',
flex=1,
margin='md'
)
]
)
],
backgroundColor='#F0F0F0',
paddingAll='10px'
),
body=BoxComponent(
layout='vertical',
contents=[
TextComponent(
text=option['text'],
wrap=True,
size='sm',
color='#333333'
)
],
paddingAll='15px'
),
footer=BoxComponent(
layout='vertical',
contents=[
ButtonComponent(
action=MessageAction(
label='📋 使用這個',
text=option['text']
),
style='primary',
color='#1DB446',
height='sm'
),
ButtonComponent(
action=PostbackAction(
label='✏️ 調整語氣',
data=f'action=adjust_tone&index={idx}&style={option["style"]}'
),
style='secondary',
height='sm',
margin='sm'
)
],
spacing='sm',
paddingAll='10px'
)
)
bubbles.append(bubble)
carousel = CarouselContainer(contents=bubbles)
return FlexSendMessage(alt_text='回覆選項', contents=carousel)
@staticmethod
def create_quick_scenarios_menu():
"""建立快速情境選單"""
bubble = BubbleContainer(
header=BoxComponent(
layout='vertical',
contents=[
TextComponent(
text='🚀 快速情境',
weight='bold',
size='md',
color='#FFFFFF'
)
],
backgroundColor='#1DB446',
paddingAll='15px'
),
body=BoxComponent(
layout='vertical',
contents=[
TextComponent(
text='選擇常見情境,立即獲得回覆建議:',
size='sm',
color='#666666',
wrap=True
),
SeparatorComponent(margin='md'),
# 請假
BoxComponent(
layout='horizontal',
contents=[
TextComponent(text='🏥', flex=0, size='lg'),
ButtonComponent(
action=PostbackAction(
label='請假',
data='scenario=請假'
),
flex=1,
style='link',
height='sm'
)
],
margin='md',
spacing='md'
),
# 拒絕加班
BoxComponent(
layout='horizontal',
contents=[
TextComponent(text='🌙', flex=0, size='lg'),
ButtonComponent(
action=PostbackAction(
label='拒絕加班',
data='scenario=拒絕加班'
),
flex=1,
style='link',
height='sm'
)
],
spacing='md'
),
# 催進度
BoxComponent(
layout='horizontal',
contents=[
TextComponent(text='⏰', flex=0, size='lg'),
ButtonComponent(
action=PostbackAction(
label='催進度',
data='scenario=催進度'
),
flex=1,
style='link',
height='sm'
)
],
spacing='md'
),
# 道歉
BoxComponent(
layout='horizontal',
contents=[
TextComponent(text='🙏', flex=0, size='lg'),
ButtonComponent(
action=PostbackAction(
label='道歉',
data='scenario=道歉'
),
flex=1,
style='link',
height='sm'
)
],
spacing='md'
),
# 自訂
BoxComponent(
layout='horizontal',
contents=[
TextComponent(text='💭', flex=0, size='lg'),
ButtonComponent(
action=MessageAction(
label='自訂情境',
text='我要自訂情境'
),
flex=1,
style='link',
height='sm'
)
],
spacing='md'
)
],
spacing='sm',
paddingAll='20px'
)
)
return FlexSendMessage(alt_text='快速情境選單', contents=bubble)
@staticmethod
def create_tone_adjustment_menu(original_text):
"""建立語氣調整選單"""
bubble = BubbleContainer(
header=BoxComponent(
layout='vertical',
contents=[
TextComponent(
text='✏️ 調整語氣',
weight='bold',
size='md',
color='#FFFFFF'
)
],
backgroundColor='#FF6B6B',
paddingAll='15px'
),
body=BoxComponent(
layout='vertical',
contents=[
TextComponent(
text='原文:',
size='xs',
color='#999999'
),
TextComponent(
text=original_text[:50] + '...' if len(original_text) > 50 else original_text,
size='sm',
wrap=True,
margin='sm'
),
SeparatorComponent(margin='md'),
TextComponent(
text='選擇想要的語氣:',
size='sm',
color='#666666',
margin='md'
),
BoxComponent(
layout='vertical',
contents=[
ButtonComponent(
action=PostbackAction(
label='👔 更正式',
data=f'tone=formal&text={original_text[:100]}'
),
style='secondary',
height='sm'
),
ButtonComponent(
action=PostbackAction(
label='😊 更輕鬆',
data=f'tone=casual&text={original_text[:100]}'
),
style='secondary',
height='sm',
margin='sm'
),
ButtonComponent(
action=PostbackAction(
label='🤝 更委婉',
data=f'tone=polite&text={original_text[:100]}'
),
style='secondary',
height='sm',
margin='sm'
),
ButtonComponent(
action=PostbackAction(
label='💪 更直接',
data=f'tone=direct&text={original_text[:100]}'
),
style='secondary',
height='sm',
margin='sm'
)
],
margin='md',
spacing='sm'
)
],
paddingAll='20px'
)
)
return FlexSendMessage(alt_text='調整語氣', contents=bubble)
@staticmethod
def create_simple_reply_card(text, title="建議回覆"):
"""建立簡單的回覆卡片(單一選項)"""
bubble = BubbleContainer(
size='kilo',
body=BoxComponent(
layout='vertical',
contents=[
TextComponent(
text=title,
weight='bold',
size='sm',
color='#1DB446'
),
TextComponent(
text=text,
wrap=True,
size='md',
margin='md'
)
],
paddingAll='20px'
),
footer=BoxComponent(
layout='horizontal',
contents=[
ButtonComponent(
action=MessageAction(
label='📋 複製使用',
text=text
),
style='primary',
color='#1DB446'
)
],
paddingAll='10px'
)
)
return FlexSendMessage(alt_text=title, contents=bubble)