python用twilio发送短信

时间:2016-07-19 23:43:19

标签: python sms twilio

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)

为什么不发送短信?

1 个答案:

答案 0 :(得分: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中配置您的活动号码。呼叫者呼叫的号码应指向/呼叫者,代理呼叫的号码应指向/ agent。祝你好运......