调用 python 函数时出现应用程序错误

Application error when calling python function

我正在构建一个语音邮件应用程序,它通过 Twilio 接收 phone 呼叫,接收消息,然后将信息记录在 Airtable 数据库中。我一切正常,但它有很多重复的代码,所以我想合并它,但我 运行 在调用函数时出错。

这是我现在拥有的...

@app.route("/call", methods=['GET', 'POST'])
def caller():

    phone = request.values.get('From', None)
    campaign = request.values.get('To')

    if campaign == brand_1.phone:
        brand = brand_1
        resp = VoiceResponse()
        resp.play(brand.recording)
        resp.record(maxLength="30")
        recording_url = request.values.get("RecordingUrl", None)
        brand.at.create('Leads', data = {'Phone':phone,'Campaign':campaign, 'Voicemail':recording_url})
        return str(resp)
    elif campaign == brand_2.phone:
        brand = brand_2
        resp = VoiceResponse()
        resp.play(brand.recording)
        resp.record(maxLength="30")
        recording_url = request.values.get("RecordingUrl", None)
        brand.at.create('Leads', data = {'Phone':phone,'Campaign':campaign, 'Voicemail':recording_url})
        return str(resp)
    elif campaign == brand_3.phone:
        brand = brand_3
        resp = VoiceResponse()
        resp.play(brand.recording)
        resp.record(maxLength="30")
        recording_url = request.values.get("RecordingUrl", None)
        brand.at.create('Leads', data = {'Phone':phone,'Campaign':campaign, 'Voicemail':recording_url})
        return str(resp)
    return "Success!"

如您所见,我将同一代码重复了三遍。它有效,但它很丑并且占用 space 所以我想创建一个单独的函数,看起来像这样...

def voicemail(brand, phone, campaign):
    resp = VoiceResponse()
    resp.play(brand.recording)
    resp.record(maxLength="30")
    recording_url = request.values.get("RecordingUrl", None)
    brand.at.create('Leads', data = {'Phone':phone,'Campaign':campaign, 'Voicemail':recording_url})
    return str(resp)

所以我想看起来像这样...

@app.route("/call", methods=['GET', 'POST'])
def caller():

    phone = request.values.get('From', None)
    campaign = request.values.get('To')

    if campaign == brand_1.phone:
        brand = brand_1
        voicemail(brand, phone, campaign)
    elif campaign == brand_2.phone:
        brand = brand_2
        voicemail(brand, phone, campaign)
    elif campaign == brand_3.phone:
        brand = brand_3
        voicemail(brand, phone, campaign)
    return "Success!"

这似乎应该有效,但实际上无效。 Twilio 只是说 "An application error has occurred"

伙计们,我错过了什么?

第二个模式使函数 caller() 只有 returns 最后的 "Success"。 你应该试试 :

return voicemail(brand, phone, campaign)