Search code examples
iosswiftsmstwiliotwilio-functions

How do I send an SMS to multiple numbers via Twilio Functions?


I have a page with multiple UITextFields where a user can type in multiple contact numbers. When the send button is clicked it should send a preset text message to the contact numbers listed. I'm using Twilio to run this and I'm using the functions feature so that I don't have to create a separate server. The issue I'm having is that it doesn't send the message out when there is more than one number listed. How do I go about fixing it so that when a user keys in several numbers, it will send the preset message to those said numbers?

I have tried multiple times to fix it but it keeps failing

This is my code in swift:

    @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
    }


}

This is the code in my Twilio function:

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);
    });

};

I would like it to work so that it sends a message to all the numbers listed in the UITextFields


Solution

  • Twilio developer evangelist here. In the sendButton function, I'd make an array of the phone numbers from the textboxes like this with a global variable numArray:

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

    Then in that same sendButton function, I'd use urlSession to send a POST request to your Twilio Function 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()
    

    Then, your Twilio Function should contain code like this to loop through the array of phone numbers and message each number:

    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));
        });
    };
    

    Hope this helps!