我正在尝试创建一个Telegram机器人,该机器人可让用户搜索仅需要其手头配料的食谱。它是用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