私はPythonとFlaskでTwilioを使うことを学んでいます。私はいくつかの番号に電話をして、人に2つの選択肢を与え、自分の選択に応じて、その人にボイスメッセージを再生し、答えをSMSメッセージで送信できるようにしたい。私はすでにいくつかのコードを書かれている同じ応答での音声とメッセージの応答
:
from flask import Flask, request
from TwilioPhoneCall import app
from twilio.twiml.voice_response import VoiceResponse, Gather
from twilio.twiml.messaging_response import MessagingResponse, Message
@app.route("/", methods=['GET', 'POST'])
def message():
resp = VoiceResponse()
gather = Gather(num_digits=1, action='/gather')
gather.say('Hello there! Do you like apples?.'+
' type one for yes '+
' type two for no.')
resp.append(gather)
resp.redirect('/')
return str(resp)
@app.route('/gather', methods=['GET', 'POST'])
def gather():
resp = VoiceResponse()
if 'Digits' in request.values:
choice = request.values['Digits']
if choice == '1':
resp.say('Nice! Apples are very healthy!')
msg = Message(to='myphonenumber').body('He likes apples!')
resp.append(msg)
return str(resp)
elif choice == '2':
resp.say('Shame on you! Apples are very healthy!')
msg = Message(to='myphonenumber').body('He said no.')
resp.append(msg)
return str(resp)
else:
resp.say('Sorry, choose one or two.')
resp.redirect('/')
return str(resp)
は、ほとんどすべてのものが正常に動作します:すべての音声メッセージと選択オプションの作業を。問題は、決して送信されないSMSメッセージにあります。
私はMessagingResponseを追加して、それとVoiceResponseの両方を返そうとしましたが、アプリケーションのエラーが発生するよりも、応答が1つ(音声とメッセージ)の2つの<response></response>
タグを持っているので、 。
これを行う方法はありますか?
はい!それがトリックでした。フィルとTwilioの良い仕事をお祝いをありがとう。私は本当に多くの機能を手に入れて楽しんでいます。 – gunslinger
恐ろしい!あなたの手のひらがうまくいくことを願っています。他の質問があれば、私はここにいます! – philnash