api - Twilio:在循环发送短信的手机列表时尝试期望
问题描述
我正在做一个小项目,让用户使用 Twilio 轻松地给一组客户发短信。我最近不得不向大约 400 位客户和其中 2 位格式错误的号码(11 位而不是 10 位)发送消息。发送完所有消息后,我检查了我的 Twilio 控制台并遇到了一些错误- 30005 Message Delivery - Unknown destination handset
,但所有其他消息都希望这 2 条消息已发送。
我遇到的问题是我的代码,预期的行为应该是:
- 我正在写消息发送给我的客户
- 选择客户列表(存储在 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)
解决方案
推荐阅读
- sql - 匹配四列作为完成左外连接的条件
- r - 在 ReactiveValues 中插入反应值
- php - 达到最大值时隐藏按钮:Jquery
- asp.net-core - Blazor 子组件
- angularjs - 从 AngularJS 升级时,Angular 6 上的 HttpClient POST 失败
- bash - 无法在 Azure 管道上执行 shell 脚本
- java - javafx 中如何将背景大小调整到窗口大小?
- amazon-web-services - S3:重命名对象失败,访问被拒绝
- python - 使用命令设计模式消除重复代码
- java - React Native Publishing APP on Play Store Rejected (permissions)