【问题标题】:Can't get Twilio app to work properly with ngrok无法让 Twilio 应用程序与 ngrok 一起正常工作
【发布时间】:2023-03-04 19:42:01
【问题描述】:

我对下面的代码/项目有以下问题。

  1. Twilio:我应该使用什么作为语音/消息传递的 webhook URL?我目前都在使用 ngrok url,并且我都配置为 POST。这是正确的吗?还是应该是 GET?

  2. 我相信我需要在下面创建一个静态 ngrok url 并使其 https:///voice -- 我需要获取付费版本使这个静态?

我相信在做了这两件事之后,应用程序应该可以工作,因为代码可以正常工作。

(另外,如果您有任何链接,以便我更好地了解这些应用程序以及它们的使用方式,那就太棒了。)

(电话和钥匙已编辑)

##API info for initiaiting the call

from twilio.rest import Client

account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = '5dXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
client = Client(account_sid, auth_token)

call = client.calls.create(
    url='https://your-ngrok-url.ngrok.io/voice',
    to='+19511231234',
    from_='+12311231234'
)

##this gathers user input from the caller
from flask import Flask
from twilio.twiml.voice_response import VoiceResponse, Gather

app = Flask(__name__)

@app.route("/voice", methods=['GET', 'POST'])
def voice():
    # Start a TwiML response
    resp = VoiceResponse()
    gather = Gather(num_digits=1, action='/gather')
    gather.say('Hello, this is Alice from your marketing company. I am calling to test the lines for an upcoming campaigm. Please press 1 as confimation of receipt of this test.')
    resp.append(gather)
    resp.redirect('/voice')
    return str(resp)

@app.route('/gather', methods=['GET', 'POST'])
def gather():
    """Processes results from the <Gather> prompt in /voice"""
    # Start TwiML response
    resp = VoiceResponse()

    # If Twilio's request to our app included already gathered digits,
    # process them
    if 'Digits' in request.values:
        # Get which digit the caller chose
        choice = request.values['Digits']

        # <Say> a different message depending on the caller's choice
        if choice == '1':
            resp.say("Thank you, goodbye!")
            return str(resp)
        elif choice == '2':
            resp.say("Sorry, I don't understand that choice. Please press 1")
            return str(resp)
        else:
            # If the caller didn't choose 1 or 2, apologize and ask them again
            resp.say("Sorry, I don't understand that choice. Please press 1.")

    # If the user didn't choose 1 or 2 (or anything), send them back to /voice
    resp.redirect('/voice')

    return str(resp)

【问题讨论】:

    标签: python twilio ngrok


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    Twilio:我应该使用什么作为语音/消息传递的 webhook URL?我目前都在使用 ngrok url,并且我都配置为 POST。这是正确的吗?还是应该是 GET?

    Twilio 可以将 webhook 作为 GET 或 POST 请求发送,但我建议使用 POST 请求,因为参数可能很长并且different servers have different limits to query string parameters

    我相信我需要在下面制作一个静态的 ngrok url 并将其设为 https:///voice -- 我是否需要获得付费版本才能使其成为静态?

    您可以使用 ngrok URL 来测试您的应用程序,它是一个有用的工具,可以创建一个 URL 以通过隧道连接到您计算机上运行的应用程序。在生产中,我建议您将此应用程序托管在服务器上,并使用您拥有并可以指向该服务器的公共 URL。

    【讨论】:

      猜你喜欢
      • 2021-01-26
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 2019-10-31
      • 2013-07-12
      • 1970-01-01
      相关资源
      最近更新 更多