2016-07-19 34 views
0
import os 

from flask import Flask 
from flask import request 
from flask import url_for 
from flask import render_template 
from twilio.rest import TwilioRestClient 


from twilio import twiml 

聲明和配置應用

app = Flask(__name__, static_url_path='/static') 

ACCOUNT_SID = "AACxxxxx" 
AUTH_TOKEN = "xxxxxx" 

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN) 

配置此號碼免費電話Twilio號接受來電。

@app.route('/caller', methods=['GET', 'POST']) 
def caller(): 
    response = twiml.Response() 
    response.say("Thank you for calling" \ 
      "Please hold.") 
    response.enqueue("Queue Demo", waitUrl='/wait') 
    return str(response) 

配置候車室通知當前位置的用戶在隊列中,

發揮Twilio的咖啡廳集合的甜,舒緩的聲音。

@app.route('/wait', methods=['GET', 'POST']) 
def wait(): 
    response = twiml.Response() 
    twilio_client.sms.messages.create(
    to="+44xxxxxxxxxx", 
    from_="+44xxxxxxxxxx", 
    body="Hey Jenny! Good luck on the bar exam!", 
) 

    response.say("You are number %s in line." % request.form['QueuePosition']) 
    response.play("https://s3-us-west-2.amazonaws.com/" \ 
      "twilio/music1.mp3") 
    response.redirect('/wait') 
    return str(response) 

連接到支持隊列 - 分配給代理呼叫的Twilio號碼。

@app.route('/agent', methods=['GET', 'POST']) 
def agent(): 
    response = twiml.Response() 
    with response.dial() as dial: 
     dial.queue("Queue Demo") 
    return str(response) 

如果PORT未由環境指定,則假定爲開發配置。

if __name__ == '__main__': 
    port = int(os.environ.get("PORT", 5000)) 
    if port == 5000: 
     app.debug = False 
    app.run(host='0.0.0.0', port=port) 

爲何不發短信?蟒蛇發送短信與twilio

回答

0

好的,我解決了它,所以如果你在twilio上使用python,這是代碼讓你的電話系統應答呼叫,暫停呼叫者播放音樂,然後發送短信給你,然後你可以打電話給號碼來幫助來電者。

這就是:

import os 

from flask import Flask 
from flask import request 
from flask import url_for 
from flask import render_template 
from twilio.rest import TwilioRestClient 

from twilio import twiml 

聲明和配置應用

app = Flask(__name__, static_url_path='/static') 

配置這個號碼接受來電。

@app.route('/caller', methods=['GET', 'POST']) 
def caller(): 
    response = twiml.Response() 
    response.say("Thank you for calling " \ 
      "Please hold.") 
    response.enqueue("Queue Demo", waitUrl='/wait') 

    return str(response) 

配置等候室以通知用戶他們在隊列中的當前位置並播放持有音樂或營銷消息。

@app.route('/wait', methods=['GET', 'POST']) 

    def wait(): 
     response = twiml.Response() 
     response.say("You are number %s in line." % request.form['QueuePosition']) 
     response.play("https://s3-us-west-2.amazonaws.com/" \ 
       "triptorigatwilio/eventpremrigaholdmusic1.mp3") 
     response.redirect('/wait') 

通過短信

client = TwilioRestClient("your account sid...AC...", "your account auth token...xxxx") 
client.sms.messages.create(
to="put number to send sms here", 
from_="put the twilio number to send sms from here", 
body="A caller is in the queue. Call now to help them.", 
) 

return str(response) 

連接電話通知代理支持隊列 - 分配到Twilio號的代理人去造訪。

@app.route('/agent', methods=['GET', 'POST']) 
def agent(): 
    response = twiml.Response() 
    with response.dial() as dial: 
     dial.queue("Queue Demo") 
    return str(response) 

如果PORT未由環境指定,則假定爲開發配置。

if __name__ == '__main__': 
    port = int(os.environ.get("PORT", 5000)) 
    if port == 5000: 
     app.debug = True 
    app.run(host='0.0.0.0', port=port) 

不要忘了在twilio配置活躍的數字。主叫方呼叫的號碼應指向/主叫方,主叫方的電話號碼應指向/代理。祝你好運...