0

我正在构建一个语音邮件应用程序,它通过 Twilio 接收电话,接收消息,然后将信息记录在 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!"

如您所见,我将相同的代码重复了三遍。它可以工作,但它很丑而且占用空间,所以我想创建一个看起来像这样的单独函数......

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 只是说“发生应用程序错误”

我在这里想念什么?

4

1 回答 1

1

第二种模式使函数caller()只返回最终的“成功”。你应该试试 :

return voicemail(brand, phone, campaign)
于 2017-06-22T18:16:34.987 回答