使用Python在Twilio中转发未接来电

huangapple go评论48阅读模式
英文:

Forward unanswered calls in Twilio using python

问题

以下是代码部分的翻译:

有人能够捕获Twilio的DialCallStatus吗它在许多Twilio在线文档中被提到但我在调试Python脚本时从未看到过我只看到CallStatus例如在request.values的以下转储中

REQUEST VALUES >>> CombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([('AccountSid', 'ACxxxxxxx'), ('ApiVersion', '2010-04-01'), ('CallSid', 'CA0c9f4e7eb73dfcd72f273451c6aa249c'), ('**CallStatus', 'in-progress**'), ('Called', '+1785xxxxxxx'), ('CalledCity', 'TOPEKA'), ('CalledCountry', 'US'), ('CalledState', 'KS'), ('CalledZip', '66603'), ('Caller', '+1630xxxxxxx'), ('CallerCity', 'ROSELLE'), ('CallerCountry', 'US'), ('CallerState', 'IL'), ('CallerZip', '60193'), ('Digits', '1'), ('Direction', 'inbound'), ('FinishedOnKey', ''), ('From', '+1630xxxxxxx'), ('FromCity', 'ROSELLE'), ('FromCountry', 'US'), ('FromState', 'IL'), ('FromZip', '60193'), ('To', '+1785xxxxxxx'), ('ToCity', 'TOPEKA'), ('ToCountry', 'US'), ('ToState', 'KS'), ('ToZip', '66603'), ('msg', 'Gather End')])])

实际上我需要将未接听的来电转发到另一个电话号码而在回调事件中报告无回答似乎是一个很好的时机但在这一点上呼叫流已经结束response.dial.number('next-number')不再起作用

有人以前这样做过吗

#这是初始来电被接听的路由
@app.route('/gather', methods=['GET', 'POST'])
def gather():
    resp = VoiceResponse()
    dial = Dial(timeout=30)
    dial.number(
        '+1-initial-called-number',
        status_callback_event='initiated ringing answered completed busy failed no-answer canceled',
        status_callback='https://my.ngrok.io/response',
        status_callback_method='POST',
    )
    resp.append(dial)
    return str(resp)

@app.route('/response', methods=['POST']) #这是回调路由
def outbound():
    status = request.values.get('CallStatus', None)
    resp = VoiceResponse()
    if (status == 'no-answer'):
        resp.dial(timeout=20).number('+1-next-number')
    return str(resp)

希望这对您有所帮助。如果您有任何其他问题,请随时提出。

英文:

Has anybody been able to capture the Twilio DialCallStatus? It's referred in many Twilio online documents but I never see one while debugging python scripts. I only see CallStatus such as in the following dump of request.values.

REQUEST VALUES>>> CombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([('AccountSid', 'ACxxxxxxx'), ('ApiVersion', '2010-04-01'), ('CallSid', 'CA0c9f4e7eb73dfcd72f273451c6aa249c'), ('CallStatus', 'in-progress'), ('Called', '+1785xxxxxxx'), ('CalledCity', 'TOPEKA'), ('CalledCountry', 'US'), ('CalledState', 'KS'), ('CalledZip', '66603'), ('Caller', '+1630xxxxxxx'), ('CallerCity', 'ROSELLE'), ('CallerCountry', 'US'), ('CallerState', 'IL'), ('CallerZip', '60193'), ('Digits', '1'), ('Direction', 'inbound'), ('FinishedOnKey', ''), ('From', '+1630xxxxxxx'), ('FromCity', 'ROSELLE'), ('FromCountry', 'US'), ('FromState', 'IL'), ('FromZip', '60193'), ('To', '+1785xxxxxxx'), ('ToCity', 'TOPEKA'), ('ToCountry', 'US'), ('ToState', 'KS'), ('ToZip', '66603'), ('msg', 'Gather End')])])

Actually, I need to forward an unanswered incoming call to another phone number and it seems to be a good time to do that when "no-answer" is reported in call back events. However, at that point, it seems that the call flow has been ended and response.dial.number('next-number') does not work any more.

Has anybody done that in the past?

#This is the route where the initial incoming call is answered
@app.route('/gather', methods=['GET', 'POST'])  
def gather():
resp = VoiceResponse()
dial = Dial(timeout=30)
dial.number(
'+1-initial-called-number',
status_callback_event='initiated ringing answered completed busy failed no-answer canceled',
status_callback='https://my.ngrok.io/response',
status_callback_method='POST',
)
resp.append(dial)
return str(resp)
@app.route('/response', methods=['POST'])        #This is the call back route
def outbound():
status=request.values.get('CallStatus', None)
resp = VoiceResponse()
if (status=='no-answer'):
resp.dial(timeout=20).number('+1-next-number')
return str(resp)

答案1

得分: 0

是的,我收到一个DialCallStatus参数。这是在Dial完成并执行Action URL之后。以下是一些Twiml示例:

<?xml version="1.0" encoding="utf-8"?>
<Response>
    <Dial action="https:XXXXXX/api/twilio/voice/noanswer" timeout="20">
        <Sip>sip:XXXXXX.sip.us1.twilio.com</Sip>
    </Dial>
</Response>

在Dial完成时,https:XXXXXX/api/twilio/voice/noanswer端点会接收到DialCallStatus。示例中的Sip部分并不重要 - 对于Number、Client和Conference操作也是如此。一旦Sip、Number、Client或Conference完成Dial操作,将调用Action URL并具有该参数。文档 表示这是Dial命令的终端状态。

我不太懂Python,但似乎您的示例代码缺少Action URL。Twilio提供的Python示例代码在"指定Action URL和方法"中。

from twilio.twiml.voice_response import Dial, VoiceResponse, Say

response = VoiceResponse()
response.dial('415-123-4567', action='/api/twilio/voice/noanswer', method='GET')
response.say('I am unreachable')

print(response)
英文:

Yes, I receive a DialCallStatus parameter. This is after a Dial completes and executes the Action url. Here is some Twiml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;Response&gt;
&lt;Dial action=&quot;https:XXXXXX/api/twilio/voice/noanswer&quot; timeout=&quot;20&quot;&gt;
&lt;Sip&gt;sip:XXXXXX.sip.us1.twilio.com&lt;/Sip&gt;
&lt;/Dial&gt;
&lt;/Response&gt;

The https:XXXXXX/api/twilio/voice/noanswer endpoint receives the DialCallCstatus at the completion of the Dial. The Sip part in the example doesn't really matter - the same is true for Number, Client, and Conference actions. Once the Sip, Number, Client, or Conference completes the Dial action URL is called and will have that parameter. The documentation says it is the terminal status for the Dial command.

I do not know Python well but it seems your example code is missing the Action url. Example code from Twilio for Python is in the "Specify an action URL and method"

from twilio.twiml.voice_response import Dial, VoiceResponse, Say
response = VoiceResponse()
response.dial(&#39;415-123-4567&#39;, action=&#39;/api/twilio/voice/noanswer&#39;, method=&#39;GET&#39;)
response.say(&#39;I am unreachable&#39;)
print(response)

huangapple
  • 本文由 发表于 2023年2月8日 22:58:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定