2017-06-21 20 views
0

私は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>タグを持っているので、 。

これを行う方法はありますか?

答えて

0

ここではTwilioの開発者のエバンジェリストです。

あなたはここに非常に近いです。唯一の間違いは、着信メッセージに応答するときは<Message>を使用し、音声コールの一部としてSMSメッセージを送信する場合は<Sms>を使用することです。

だから、あなたのgatherルートは、このように、resp.smsを使用する必要があります。

@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!') 
      resp.sms('He likes apples!', to='myphonenumber') 
      return str(resp) 

     elif choice == '2': 
      resp.say('Shame on you! Apples are very healthy!') 
      resp.sms('He said no.', to='myphonenumber') 
      return str(resp) 

     else: 
      resp.say('Sorry, choose one or two.') 

    resp.redirect('/') 

    return str(resp) 

は、それがすべてで助けなら、私に教えてください。

+0

はい!それがトリックでした。フィルとTwilioの良い仕事をお祝いをありがとう。私は本当に多くの機能を手に入れて楽しんでいます。 – gunslinger

+0

恐ろしい!あなたの手のひらがうまくいくことを願っています。他の質問があれば、私はここにいます! – philnash

関連する問題