我正在做一个小项目,让用户轻松使用Twilio文本一组客户。我最近不得不给大约400个客户发送消息,其中有2个格式化错误(11位而不是10位)。在所有的消息发送后,我检查了我的Twilio控制台,有几个错误- 30005消息传递-未知目的地手机,但所有其他消息期望那些2在那里传递。
我的问题是我的代码预期的行为应该是:
我正在写信息发送给我的客户
选择客户列表(存储在MongoDB中)
单击将调用的按钮launch()
method方法
的launch()
met方法将调用contact()
method and loop o方法并循环遍历列表中的每个编号以发送该消息
最后,我将转向包含成功与失败数量的活动概述
当前的行为:
每当我从Twilio API得到任何类型的错误,例如30005 Message Delivery
I am being redirected to a 'service unavailable
' page. 我被重定向到一个'service unavailable
' page. However all the custo的页面。然而,所有拥有正确号码的客户都得到了消息。
我想做的Twilio短信API错误:
我只是想跳过它,只要有任何类型的错误,并继续循环的电话号码列表
代码
@texting.route("/launch-campaign", methods=['POST'])
@login_required
def launch():
""" Launch a new texting-campaign """
message = request.form['body']
selected_list = request.form['list']
#TO FIX
return text_customers(message)
def contact(phone, message_body):
""" Send the text message to a list of recipients """
international_number = "+33" + phone
twilio_client.messages.create(
from_=TWILIO_PHONE_NUMBER,
body=message_body,
to=international_number)
def text_customers(message):
""" Send a text-message to all customers-customers """
collection = mongo.db[customers_production]
cursor = collection.find()
errors = 0
success = 0
for customer in cursor:
try:
contact(customer['Phone'], message)
success = success + 1
except:
errors = errors + 1
print(traceback.format_exc())
return str(cursor)