Skip to content

Commit d604141

Browse files
authored
Example question (#16)
* feat(postback.py): add example questions with quick replies Adds example questions for different categories (law, housing, club, activity, course, admin procedure) using quick replies. Also adds a hint in the welcome message to reset the bot if it becomes unresponsive. The addition of example questions with quick replies enhances user experience by providing guidance on the types of questions the bot can answer. The hint in the welcome message provides a solution for users experiencing issues with the bot's responsiveness. * fix(common.py): increase loading animation duration to 60 seconds The default loading animation duration is increased to prevent premature termination of the animation, especially during longer operations. * feat: add example question to clubs * Add law example question * fix(dify.py): reduce max_retries to 1 to prevent long delays The number of retries was reduced to prevent the bot from taking too long to respond when the Dify server is down. * Add course example question
1 parent 9dbbf39 commit d604141

3 files changed

Lines changed: 92 additions & 4 deletions

File tree

linebot/app/api/dify.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ class DifyConfig:
2626

2727
api_key: str
2828
base_url: str = "https://dify-ncku-chatbot.yenslife.top/v1"
29-
max_retries: int = 3
29+
max_retries: int = 1
3030
retry_delay: float = 1.0
3131

3232

33-
def retry_on_error(max_retries: int = 3, delay: float = 1.0):
33+
def retry_on_error(max_retries: int = 1, delay: float = 1.0):
3434
"""Decorator for implementing retry logic."""
3535

3636
def decorator(func):

linebot/app/services/handlers/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def create_quick_reply() -> QuickReply:
3737
)
3838

3939

40-
def show_loading_animation(user_id, duration=5):
40+
def show_loading_animation(user_id, duration=60):
4141
"""顯示 LINE Bot loading 動畫"""
4242
try:
4343
url = "https://api.line.me/v2/bot/chat/loading/start"

linebot/app/services/handlers/postback.py

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
無論是校園資訊、活動查詢、選課資訊還是校內生活大小事,我都可以為你服務!
1818
你可以試試點選下方泡泡來問我
1919
或者直接傳訊息給我吧!我會盡快幫你找到答案喔~😉
20-
有問題也隨時告訴我,讓你的校園生活更便利!"""
20+
有問題也隨時告訴我,讓你的校園生活更便利!
21+
(小提示:如果你發現我變笨了,可以試著點選「清除對話紀錄」來重置我,這樣我就能重新學習了!)"""
2122

2223
TERMS_MESSAGE = """📜 服務條款
2324
NCKU Chatbot 的回覆基於現有資料與自然語言處理技術,可能存在誤差或未能即時更新的情況。使用者應自行判斷回覆內容的準確性,並以學校官方公告與相關單位提供的資訊為準。本服務不對因使用 NCKU Chatbot 所產生的任何後果負責。
@@ -34,6 +35,15 @@ def create_quickreply():
3435
)
3536

3637

38+
def create_example_question_quickreply(questions: list[str]):
39+
return QuickReply(
40+
items=[
41+
QuickReplyButton(action=MessageAction(label=question, text=f"{question}"))
42+
for question in questions
43+
]
44+
)
45+
46+
3747
def handle_postback_event(event):
3848
data = event.postback.data
3949
user_id = event.source.user_id
@@ -142,5 +152,83 @@ def handle_postback_event(event):
142152
),
143153
),
144154
]
155+
### Example Questions using quick reply
156+
elif data == "example_question_law":
157+
logger.info(f"User {user_id} requested example question for law.")
158+
return [
159+
TextSendMessage(
160+
text="以下是一些關於成大法規的問題範例,您可以點選其中一個來詢問我:",
161+
quick_reply=create_example_question_quickreply(
162+
[
163+
"如果我遇到性騷擾案件應該找誰處理?",
164+
"宿舍違規審議小組是什麼",
165+
"查詢圖書館室內空間使用規定",
166+
]
167+
),
168+
)
169+
]
170+
elif data == "example_question_housing":
171+
logger.info(f"User {user_id} requested example question for housing.")
172+
return [
173+
TextSendMessage(
174+
text="以下是一些關於宿舍的問題範例,您可以點選其中一個來詢問我:",
175+
quick_reply=create_example_question_quickreply(
176+
["光二宿舍熱水時間", "住宿服務組在哪裡?", "宿舍收費標準?"]
177+
),
178+
)
179+
]
180+
elif data == "example_question_club":
181+
logger.info(f"User {user_id} requested example question for club.")
182+
return [
183+
TextSendMessage(
184+
text="以下是一些關於社團的問題範例,您可以點選其中一個來詢問我:",
185+
quick_reply=create_example_question_quickreply(
186+
[
187+
"有沒有推薦的戶外社團?",
188+
"學校有熱舞社嗎?",
189+
"成大服務性質的社團有哪些?",
190+
]
191+
),
192+
)
193+
]
194+
elif data == "example_question_activity":
195+
logger.info(f"User {user_id} requested example question for activity.")
196+
return [
197+
TextSendMessage(
198+
text="以下是一些關於學校活動的問題範例,您可以點選其中一個來詢問我:",
199+
quick_reply=create_example_question_quickreply(
200+
[
201+
"成大的活動如何報名?",
202+
"我可以取消報名嗎?",
203+
"下星期成大的活動有哪些?",
204+
"有什麼通識講座可以參加?",
205+
]
206+
),
207+
)
208+
]
209+
elif data == "example_question_course":
210+
logger.info(f"User {user_id} requested example question for course.")
211+
return [
212+
TextSendMessage(
213+
text="以下是一些關於課程的問題範例,您可以點選其中一個來詢問我:",
214+
quick_reply=create_example_question_quickreply(
215+
[
216+
"有又甜又涼的通識嗎?",
217+
"有推薦的國文課嗎?",
218+
"成大有開設AI相關的課程嗎?",
219+
]
220+
),
221+
)
222+
]
223+
elif data == "example_question_admin_procedure":
224+
logger.info(f"User {user_id} requested example question for admin procedure.")
225+
return [
226+
TextSendMessage(
227+
text="以下是一些關於常見行政手續的問題範例,您可以點選其中一個來詢問我:",
228+
quick_reply=create_example_question_quickreply(
229+
["機車證申請方式?", "學費申請流程?", "新生資料表填寫?"]
230+
),
231+
)
232+
]
145233
else:
146234
logger.warning(f"Unknown postback data: {data} from user {user_id}")

0 commit comments

Comments
 (0)