【问题标题】:python sending sms with twiliopython用twilio发送短信
【发布时间】:2016-07-19 23:43:18
【问题描述】:
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)

为什么不发送短信?

【问题讨论】:

    标签: python sms twilio


    【解决方案1】:

    好的,我解决了,所以如果你在 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 中配置您的活动号码。呼叫者呼叫的号码应指向 /caller,代理呼叫的号码应指向 /agent。祝你好运...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      相关资源
      最近更新 更多