Navigation

twilio.send()

Definition

twilio.send()

Sends an SMS text message with Twilio.

Usage Examples

Call from a Function

exports = function(message, recipient) {
    const twilio = context.services.get("myTwilio");
    twilio.send({
        to: recipient,
        from: "+15551234567",
        body: message
    });
};

Call from a Client SDK

import { Stitch } from 'mongodb-stitch-browser-sdk';
import { TwilioServiceClient } from 'mongodb-stitch-browser-services-twilio';
const app = Stitch.defaultAppClient;
const twilio = app.getServiceClient(TwilioServiceClient.factory, "myTwilio");

const args = {
  to: "+15558675309",
  from: "+15551234567",
  body: "Hello from Stitch!"
};
twilio.sendMessage(args.to, args.from, args.body);
StitchAppClient client = Stitch.getDefaultAppClient()
client.getServiceClient(TwilioServiceClient.factory, "myTwilio")
  .sendMessage("+15558675309", "+15551234567", "Hello from Stitch!")
  .addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull final Task<Void> task) {
      if (task.isSuccessful()) { Log.d("stitch", "Successfully sent text message!"); } else { Log.e("stitch", "Error sending text message:", task.getException()); }
    }
  });
let app = Stitch.defaultAppClient!
let twilio = app.serviceClient(
  fromFactory: twilioServiceClientFactory,
  withName: "myTwilio"
)
twilio.sendMessage(
  to: "+15558675309",
  from: "+15551234567",
  body: "Hello from Stitch!"
) { result in
  switch result {
  case .success:
    print("Successfully sent text message!")
  case .failure(let error):
    print("Error sending text message: \(error)")
  }
}

Parameters

Parameter Type Description
args document

A document of the following form:

{
    "to": <string>,   // recipient phone #
    "from": <string>, // sender phone #
    "body": <string>  // message
}
args.to string The recipient’s phone number. This must be in E. 164 format
args.from string A phone number owned by your Twilio account. This must be in E. 164 format
args.body string The message to send.

Return Value

The twilio.send() action does not return a value.

Rule Templates

Users Can Send Only Messages From a Specific Phone Number

{
  "%%args.from": "+15551234"
}

Users Can Only Send Messages To a Limited Set of Phone Numbers

{
  "%%args.to": {
    "$in": [
      "+15551234",
      "+18675309"
    ]
  }
}

Users Can Only Send Messages to Themselves

{
  "%%true": {
    "%function": {
      "name": "isCurrentUsersPhoneNumber",
      "arguments": [
        "%%args.to"
      ]
    }
  }
}

Note

This template calls an example function named isCurrentUsersPhoneNumber that does the following:

  1. Accepts the phone number provided in the to argument
  2. Queries MongoDB for a user document that matches the current user’s id
  3. Compares the provided phone number to the number listed in the user document
  4. Returns the boolean result of the comparison
exports = function(toPhone) {
  const mdb = context.services.get('mongodb-atlas');
  const users = mdb.db('demo').collection('users');
  const user = users.findOne({ _id: context.user.id });
  return user.phoneNumber === toPhone;
}