首页 > 解决方案 > Twilio:在循环发送短信的手机列表时尝试期望

问题描述

我正在做一个小项目,让用户使用 Twilio 轻松地给一组客户发短信。我最近不得不向大约 400 位客户和其中 2 位格式错误的号码(11 位而不是 10 位)发送消息。发送完所有消息后,我检查了我的 Twilio 控制台并遇到了一些错误- 30005 Message Delivery - Unknown destination handset,但所有其他消息都希望这 2 条消息已发送。

我遇到的问题是我的代码,预期的行为应该是:

当前行为:

我想用 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)

标签: apierror-handlingtwiliotwilio-api

解决方案


推荐阅读