我正在尝试创建一个电报机器人,允许用户搜索只需要他们手头上的配料的食谱。它是用Python构建的,我有一个粗略的代码,但是在我微调细节之前,我想让它的基本形式启动并运行。不幸的是,我在解析与用户在其消息中列出的成分对应的配方的API时遇到了困难。具体来说,记录的错误消息是“错误解析API”。有人能看看我的代码,帮我看看哪里出了问题吗
这是我的代码的相关部分:
def handle_messages(messages):
for message in messages:
mappedIngreds = []
for i in range(len(message.text)):
ingred = message.text[i].lower()
if i == 0:
mappedIngreds.append(ingred)
else:
mappedIngreds.append(f"+ {ingred}")
# get responses from API
try:
response = requests.get(f"{apiURL}{mappedIngreds}{apiId}{apiKey}")
response.raise_for_status() # for debugging
except requests.RequestException:
logging.error("Error connecting to the API")
return None
# format responses into list of recipes
try:
recipes = []
for i in response.json():
recipeInfo = {}
recipeInfo["name"] = i["label"]
recipeInfo["url"] = i["url"]
recipes.append(recipeInfo)
except (KeyError, TypeError, ValueError):
logging.error("Error parsing the API")
return None
# send list of recipes to user
try:
bot.reply_to(message.chat.id, "Try these recipes:", *recipeInfo["name"], *recipeInfo["url"], sep="\n")
except:
logging.error("Error printing recipes")
我的完整代码在这里:https://pastebin.com/W0CceAt9
目前没有回答
相关问题 更多 >
编程相关推荐