问题描述
我正在做一个小项目,让用户使用 Twilio 轻松地给一组客户发短信。我最近不得不向大约 400 位客户发送一条消息,其中有 2 个号码格式错误(11 位而不是 10 位)。发送完所有消息后,我检查了我的 Twilio 控制台,发现了几个错误 - 30005 Message Delivery - UnkNown destination handset
,但所有其他消息都期望传递这两个消息。
我遇到的问题是我的代码,预期的行为应该是:
- 我正在写要发送给我的客户的消息
- 选择客户列表(存储在 MongoDB 中)
- 单击将调用
launch()
方法的按钮 -
launch()
方法将调用contact()
方法并遍历列表中的每个数字以发送该消息 - 最后,我应该被重定向到包含成功失败次数的广告系列概览
当前行为:
- 每当我从 Twilio API 收到任何类型的错误(例如
30005 Message Delivery
)时,我都会被重定向到“service unavailable
”页面。但是,所有拥有正确号码的客户都会收到消息。
我想如何处理 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)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)