如何从Twilio IVR系统获取当前呼叫者号码?

2024-04-25 06:40:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在Twilio中获取当前呼叫的“收件人”和“发件人”号码。我正在https://www.twilio.com/docs/voice/tutorials/ivr-phone-tree-python-flask使用Twilio IVR系统示例

这是我当前的代码:

rom twilio.twiml.voice_response import VoiceResponse

from ivr_phone_tree_python import app
from ivr_phone_tree_python.view_helpers import twiml

@app.route('/ivr/welcome', methods=['POST'])
def welcome():
    response = VoiceResponse() 
    # TODO
    # get current calls to and from numbers here
    # query subaccounts and main account and find which account the caller is calling based on the To number
    with response.gather(
        num_digits=1, action=url_for('menu'), method="POST"
    ) as g:
        g.say(message="Thanks for calling the E T Phone Home Service. " +
              "Please press 1 for directions." +
              "Press 2 for a list of planets to call.", loop=3)
    return twiml(response)


@app.route('/ivr/menu', methods=['POST'])
def menu():
    selected_option = request.form['Digits']
    option_actions = {'1': _give_instructions,
                      '2': _list_planets}

    if option_actions.has_key(selected_option):
        response = VoiceResponse()
        option_actions[selected_option](response)
        return twiml(response)

    return _redirect_welcome()


@app.route('/ivr/planets', methods=['POST'])
def planets():
    selected_option = request.form['Digits']
    option_actions = {'2': "+12024173378",
                      '3': "+12027336386",
                      "4": "+12027336637"}

    if selected_option in option_actions:
        response = VoiceResponse()
        response.dial(option_actions[selected_option])
        return twiml(response)

    return _redirect_welcome()


# private methods

def _give_instructions(response):
    response.say("To get to your extraction point, get on your bike and go " +
                 "down the street. Then Left down an alley. Avoid the police" +
                 " cars. Turn left into an unfinished housing development." +
                 "Fly over the roadblock. Go past the moon. Soon after " +
                 "you will see your mother ship.",
                 voice="alice", language="en-GB")

    response.say("Thank you for calling the E T Phone Home Service - the " +
                 "adventurous alien's first choice in intergalactic travel")

    response.hangup()
    return response


def _list_planets(response):
    with response.gather(
        numDigits=1, action=url_for('planets'), method="POST"
    ) as g:
        g.say("To call the planet Broh doe As O G, press 2. To call the " +
              "planet DuhGo bah, press 3. To call an oober asteroid " +
              "to your location, press 4. To go back to the main menu " +
              " press the star key.",
              voice="alice", language="en-GB", loop=3)

    return response


def _redirect_welcome():
    response = VoiceResponse()
    response.say("Returning to the main menu", voice="alice", language="en-GB")
    response.redirect(url_for('welcome'))

    return twiml(response)

如果我想在更新到付费帐户时对我的所有子帐户使用此IVR结构,ngrok https会输出并将其发布到我的Twilio仪表板上的所有子帐户吗?这就是我想知道谁是IVR呼叫中的收件人和发件人的原因,这样我就可以区分他们呼叫的是哪个子帐户(并出于其他原因使用呼叫者号码)


Tags: thetoactionsforreturnresponsedefoption
1条回答
网友
1楼 · 发布于 2024-04-25 06:40:23

要获取往返号码,您需要执行以下操作request.POST['From']request.POST['To']这是在Django中执行的方式,您需要修改flask。ngrok是一种在web上托管本地主机网站的方法,这样Twilio就可以与之通信。您需要在Twilio仪表板中手动或通过Twilio api更新Twilio需要关注的ngrok url。还可以查看TwilioML应用程序

相关问题 更多 >