簡體   English   中英

python用twilio發送短信

[英]python sending sms with 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)

為什么不發送短信?

好的,我解決了,因此,如果您在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。 祝好運...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM