33import telegram .ext
44import logging
55from typing import Callable , Awaitable
6- from spellbook_client import VariantsApi , InvalidUrlResponse , ApiException , Variant , FindMyCombosApi , DeckRequest , CardInDeckRequest , CardListFromUrlApi
6+ from spellbook_client import VariantsApi , InvalidUrlResponse , ApiException , Variant , FindMyCombosApi , DeckRequest , CardInDeckRequest , CardListFromUrlApi , VariantsQueryValidationError
77from bot_utils import parse_queries , uri_validator , SpellbookQuery , API , url_from_variant , compute_variant_recipe , compute_variant_name , compute_variant_results
88from text_utils import telegram_chunk , chunk_diff_async
99
@@ -125,42 +125,65 @@ async def search_inline(update: telegram.Update, context: telegram.ext.ContextTy
125125 ))
126126 return
127127 current_offset = int (inline_query .offset or 0 )
128- query_info = SpellbookQuery (query )
129- async with API () as api_client :
130- api = VariantsApi (api_client )
131- result = await api .variants_list (
132- q = query_info .patched_query ,
133- limit = max_search_result ,
134- ordering = '-popularity' ,
135- offset = current_offset ,
136- )
137128 next_offset = current_offset + max_search_result
138- inline_results = [
139- telegram .InlineQueryResultArticle (
140- id = str (hash (query_info .patched_query )),
141- title = f'Found { result .count } results for { query_info .query } ' ,
142- url = query_info .url ,
143- hide_url = True ,
144- input_message_content = telegram .InputTextMessageContent (
145- message_text = query_info .summary ,
146- parse_mode = telegram .constants .ParseMode .MARKDOWN ,
147- ),
129+ query_info = SpellbookQuery (query )
130+ result = None
131+ try :
132+ async with API () as api_client :
133+ api = VariantsApi (api_client )
134+ result = await api .variants_list (
135+ q = query_info .patched_query ,
136+ limit = max_search_result ,
137+ ordering = '-popularity' ,
138+ offset = current_offset ,
139+ )
140+ inline_results = [
141+ telegram .InlineQueryResultArticle (
142+ id = str (hash (query_info .patched_query )),
143+ title = f'Found { result .count } results for { query_info .query } ' ,
144+ url = query_info .url ,
145+ input_message_content = telegram .InputTextMessageContent (
146+ message_text = query_info .summary ,
147+ parse_mode = telegram .constants .ParseMode .MARKDOWN ,
148+ ),
149+ )
150+ ] if current_offset == 0 else []
151+ inline_results .extend (
152+ telegram .InlineQueryResultArticle (
153+ id = str (variant .id ),
154+ title = compute_variant_name (variant ),
155+ description = compute_variant_results (variant ),
156+ input_message_content = telegram .InputTextMessageContent (
157+ message_text = f'[{ compute_variant_recipe (variant )} ]({ url_from_variant (variant )} )' ,
158+ parse_mode = telegram .constants .ParseMode .MARKDOWN ,
159+ ),
160+ url = url_from_variant (variant ),
161+ )
162+ for variant in result .results
148163 )
149- ] if current_offset == 0 else []
150- inline_results .extend ([
151- telegram .InlineQueryResultArticle (
152- id = str (variant .id ),
153- title = compute_variant_name (variant ),
154- description = compute_variant_results (variant ),
155- input_message_content = telegram .InputTextMessageContent (
156- message_text = f'[{ compute_variant_recipe (variant )} ]({ url_from_variant (variant )} )' ,
157- parse_mode = telegram .constants .ParseMode .MARKDOWN ,
164+ except ApiException as e :
165+ data = e .data
166+ if isinstance (data , VariantsQueryValidationError ):
167+ error_messages = data .q or []
168+ reply = f'Errors in your query for { query_info .summary } :\n ' + '\n ' .join (f'• { msg } ' for msg in error_messages )
169+ title = 'Query Error'
170+ description = ',\n ' .join (error_messages )
171+ else :
172+ reply = f'Failed to fetch results for { query_info .summary } '
173+ title = 'Error'
174+ description = 'An unexpected error occurred.'
175+ inline_results = [
176+ telegram .InlineQueryResultArticle (
177+ id = str (hash (query_info .patched_query )),
178+ title = title ,
179+ url = 'https://commanderspellbook.com/syntax-guide/' ,
180+ description = description ,
181+ input_message_content = telegram .InputTextMessageContent (
182+ message_text = reply ,
183+ parse_mode = telegram .constants .ParseMode .MARKDOWN ,
184+ ),
158185 ),
159- url = url_from_variant (variant ),
160- hide_url = True ,
161- )
162- for variant in result .results
163- ])
186+ ]
164187 if len (query ) < 10 :
165188 inline_button = telegram .InlineQueryResultsButton (
166189 text = 'Syntax Guide' ,
@@ -172,7 +195,7 @@ async def search_inline(update: telegram.Update, context: telegram.ext.ContextTy
172195 results = inline_results ,
173196 button = inline_button ,
174197 cache_time = 24 * 60 * 60 ,
175- next_offset = str (next_offset ) if next_offset < result .count else None ,
198+ next_offset = str (next_offset ) if result and next_offset < result .count else None ,
176199 )
177200
178201
0 commit comments