1818FAQ_ASSISTANT_URL = os .getenv ('FAQ_ASSISTANT_URL' , '' ).strip ()
1919FAQ_ASSISTANT_SHARED_SECRET = os .getenv ('FAQ_ASSISTANT_SHARED_SECRET' , '' )
2020FAQ_ASSISTANT_TIMEOUT = int (os .getenv ('FAQ_ASSISTANT_TIMEOUT' , '55' ))
21+ FAQ_ASSISTANT_ERROR_MESSAGE = (
22+ 'The FAQ assistant is currently not available. '
23+ 'In the meantime, you can try to find the answer here:'
24+ )
25+ FAQ_ASSISTANT_GENERIC_RESOURCES = {
26+ 'docs' : 'https://datatalks.club/docs/' ,
27+ 'faq' : 'https://datatalks.club/faq/' ,
28+ }
2129
2230
2331
@@ -93,6 +101,33 @@ def call_faq_assistant(payload):
93101 return response .json ()
94102
95103
104+ def faq_assistant_fallback_resources (payload ):
105+ course = payload .get ('course' )
106+ if not course :
107+ return FAQ_ASSISTANT_GENERIC_RESOURCES
108+
109+ return {
110+ 'docs' : f'https://datatalks.club/docs/courses/{ course } /' ,
111+ 'faq' : f'https://datatalks.club/faq/{ course } .html' ,
112+ 'repo' : f'https://github.com/DataTalksClub/{ course } ' ,
113+ }
114+
115+
116+ def format_faq_assistant_error_message (payload ):
117+ resources = faq_assistant_fallback_resources (payload )
118+ lines = [
119+ f"- <{ resources ['docs' ]} |Docs>" ,
120+ f"- <{ resources ['faq' ]} |FAQ>" ,
121+ ]
122+ if resources .get ('repo' ):
123+ lines .append (f"- <{ resources ['repo' ]} |Course repo>" )
124+
125+ return (
126+ f'{ FAQ_ASSISTANT_ERROR_MESSAGE } \n \n '
127+ + '\n ' .join (lines )
128+ )
129+
130+
96131def post_faq_assistant_answer (channel , thread_ts , payload ):
97132 if not channel or not thread_ts :
98133 logger .info ('FAQ assistant request missing channel or ts' )
@@ -109,7 +144,21 @@ def post_faq_assistant_answer(channel, thread_ts, payload):
109144 f"FAQ assistant request: scope={ payload ['scope' ]} "
110145 f"course={ payload .get ('course' , '' )} "
111146 )
112- answer = call_faq_assistant (payload )
147+ try :
148+ answer = call_faq_assistant (payload )
149+ except requests .RequestException as exc :
150+ response = getattr (exc , 'response' , None )
151+ status = getattr (response , 'status_code' , None )
152+ body = getattr (response , 'text' , '' ) if response is not None else ''
153+ logger .info (
154+ f"FAQ assistant request failed: status={ status } "
155+ f"error={ exc } body={ body [:500 ]} "
156+ )
157+ slack .post_message_to_thread (
158+ channel , thread_ts , format_faq_assistant_error_message (payload )
159+ )
160+ return
161+
113162 if not answer :
114163 return
115164
0 commit comments