使用拨号盘和 CVS 文件发送自动短信时出错

问题描述 投票:0回答:1

我正在使用 python 通过 Dialpad 使用 CVS 文件中的联系人发送自动短信(格式为姓名、电话号码)。

但是我不断收到此错误 - 此处使用的示例名称和数字:

Error sending SMS to Bob (+16124855917): b'Not Found'
Error sending SMS to Larry (+17699643423): b'Not Found'
Error sending SMS to Harry (+12538355287): b'Not Found'
Error sending SMS to Barry (+16128562388): b'Not Found'
Error sending SMS to Gary (+12129748516): b'Not Found'

Python代码:

    import csv
    import requests

    DIALPAD_API_KEY = 'MY-API-KEY'
    DIALPAD_PHONE_NUMBER = 'MY-PHONE-NUMBER'

    def send_sms_message(name, phone_number):
        if not phone_number.startswith('+'):
            phone_number = f'+1{phone_number}'
        url = 'https://dialpad.com/api/v2/sms_messages'
        headers = {
            'Content-Type': 'application/json',
            'Authorization': f'Bearer {DIALPAD_API_KEY}'
        }
        data = {
            'from_number': DIALPAD_PHONE_NUMBER,
            'to_number': phone_number,
            'text': f'Is this {name}? I want to make sure I have the right number.'
        }
        response = requests.post(url, headers=headers, json=data)
        if response.status_code == 200:
            print(f'SMS sent to {name} ({phone_number}) successfully')
        else:
            print(f'Error sending SMS to {name} ({phone_number}): {response.content}')
        return response.status_code

    with open('recipients.csv', 'r') as f:
        reader = csv.reader(f)
        for row in reader:
            name = row[0]
            phone_number = row[1].replace(' ', '')  # Remove any whitespace
            send_sms_message(name, phone_number)

我希望这条消息会发送到每个电话号码并附上他们的名字。

如何解决这个问题?

python sms
1个回答
0
投票

我不确定这是否是您问题的原因,但当前的 DialPad 文档 指定了一个不同的 URL 来发送短信 (

https://dialpad.com/api/v2/sms
)。

© www.soinside.com 2019 - 2024. All rights reserved.