如何通过Twilio功能将SMS发送到多个号码?

大卫

我有一个包含多个UITextFields的页面,用户可以在其中键入多个联系电话。单击发送按钮后,它应将预设的短信发送到列出的联系电话。我正在使用Twilio来运行此程序,并且正在使用功能功能,因此不必创建单独的服务器。我遇到的问题是,当列出多个号码时,它不会发送消息。我该如何解决该问题,以便当用户键入多个数字时,它将向这些数字发送预设消息?

我已经尝试过多次修复它,但是它一直失败

这是我的代码迅速:

    @IBOutlet weak var phonenumber: UITextField!
    @IBOutlet weak var phonenumber1: UITextField!
    @IBOutlet weak var phonenumber2: UITextField!
    @IBOutlet weak var phonenumber3: UITextField!

    var currentTextField: UITextField?

    private let contactPicker = CNContactPickerViewController()

    override func viewDidLoad() {
        super.viewDidLoad()
        configureTextFields()
        configureTapGesture()

     }


    private func configureTextFields() {
        phonenumber.delegate = self
        phonenumber1.delegate = self
        phonenumber2.delegate = self
        phonenumber3.delegate = self

    }

    private func configureTapGesture(){
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SelfTestTimer.handleTap))
        viewcontact.addGestureRecognizer(tapGesture)

    }

    @objc private func handleTap(){
        viewcontact.endEditing(true)

    }

    @IBAction func sendbutton(_ sender: Any) {

        presentAlert(alertTitle: "", alertMessage: "Make sure all the contacts have a country code attached to it ie +60", lastAction: UIAlertAction(title: "Continue", style: .default) { [weak self] _ in



        let headers = [
            "Content-Type": "//urlencoded"
        ]


        let parameters: Parameters = [
            "To": self?.currentTextField?.text ?? "", // if "To": is set to just one text field ie "To": self?.phonenumber1.text ?? "", the sms is sent

            "Body": "Tester",


        ]

        Alamofire.request("//path", method: .post, parameters: parameters, headers: headers).response { response in
            print(response)

        }
        }
    )}

}

extension SelfTestTimer: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        currentTextField = nil
        textField.resignFirstResponder()
        return true
    }


    func textFieldDidBeginEditing(_ textField: UITextField) {


        if textField.hasText{
            //dont do anything

        }else{
        currentTextField = textField
        contactPicker.delegate = self
        self.present(contactPicker, animated: true, completion: nil)
        }
        return
    }


}

这是我的Twilio函数中的代码:

exports.handler = function(context, event, callback) {
    const client = context.getTwilioClient();
    const to = event.To;
    const body = event.Body;
    client.messages.create({
      from: 'Twilio Phone Number',
      to: to,
      body: body,

    }).then(msg => {
      callback(null);
    });

};

我希望它能正常工作,以便它向邮件中列出的所有号码发送一条消息 UITextFields

莉齐皮卡

Twilio开发人员布道者在这里。sendButton函数中,我将使用全局变量从这样的文本框中创建一个电话号码数组numArray

numArray = [phonenumber.text!, phonenumber1.text!, phonenumber2.text!, phonenumber3.text!]

然后在同一个sendButton函数中,我将使用urlSession一个POST请求发送到您的Twilio函数URL。

let Url = String(format: "REPLACE-WITH-YOUR-TWILIO-FUNCTION-URL")
        guard let serviceUrl = URL(string: Url) else { return }
        var request = URLRequest(url: serviceUrl)
        request.httpMethod = "POST"
        request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
        guard let httpBody = try? JSONSerialization.data(withJSONObject: numArray, options:[]) else {
            return
        }
        request.httpBody = httpBody

        let session = URLSession.shared
        session.dataTask(with: request) { (data, response, error) in
            if let response = response {
                print(response)
            }
            if let data = data {
                do {
                    let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)
                    print("json ", json)
                } catch {
                    print(error)
                }
            }
        }.resume()

然后,您的Twilio函数应包含如下代码,以遍历电话号码数组并向每个号码发送消息:

exports.handler = function(context, event, callback) {
    const client = context.getTwilioClient();
    var nums = [event[0], event[1], event[2], event[3]]; //hardcoded for 4 textboxes
    nums.forEach(function(arrayNum) {
        client.messages.create({
            to: arrayNum,
            from: "REPLACE-WITH-YOUR-TWILIO-NUMBER",
            body: "REPLACE WITH YOUR MESSAGE/WHATEVER MESSAGE YOU WANT!"
        }).then(msg => {
            callback(null, msg.sid);
        }).catch(err => callback(err));
    });
};

希望这可以帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何访问通过Twilio发送到我的Spring Boot Controller的SMS文本?

如何使用云功能将Firebase数据发送到Slack

AWS:SNS将SMS消息发送到多个电话号码

如何配置我的Android手机以通过彩信将图像发送到Twilio?

使用AWS Pinpoint将SMS消息发送到特定的电话号码

如何通过Twilio WhatsApp API将英雄卡从Bot框架发送到WhatsApp

如何通过PHP将推送通知发送到多个IOS设备?

如何使用Firebase云功能将条带响应发送到客户端(Swift)

如何通过一个API调用将多个文档发送到Elastic

如何通过CFHTTP将此API调用发送到Twilio Flow

如何在Zoiper中使用Twilio号码发送/接收SMS?

范围功能将数据发送到错误的行

使用twilio Passthrough API将消息发送到多个号码

如何使用电子邮件或电话号码通过API将DM发送到Google聊天

如何使用C#使用twilio将whatsApp消息发送到任何号码

如何通过Twilio号码接收和发送语音邮件?

通过云功能将Firebase数据库数据发送到客户端

Twilio SMS消息未发送到我的印度手机号码

如何通过ajax将多个单选按钮值发送到php

如何通过一个过程调用将多个文件发送到打印机

Twilio-在收到SMS消息时将HTTP请求发送到端点

是否可以通过Twilio号码关联发送的原始SMS和收到的答复SMS?

如何通过php脚本将多个输入从表单发送到电子邮件

将号码从 NumberPicker 发送到 Firebase

不能将 nonce 发送到 Braintree

如何通过 Ajax 将多个数据字段发送到 php 页面?

如何发送功能多个电话号码?

如何使用phpmailer通过位置选择将邮件发送到多个邮件地址?

如何使用 swift UI 中的表单功能将用户输入发送到数据库?