Navigation

twilio.send()

Definition

twilio.send()

Sends an SMS text message with Twilio.

Usage

Example

exports = function() {
    const twilio = context.services.get("myTwilio");
    twilio.send({
        to: "+15558675309",
        from: "+15551234567",
        body: "Hello from Stitch!"
    });
};
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();
TwilioServiceClient twilio = client.getServiceClient(
  TwilioServiceClient.factory, "myTwilio"
);
String to = "+15558675309";
String from = "+15551234567";
String body = "Hello from Stitch!";
twilio.sendMessage(to, from, body)
  .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 in E.164 Format.
args.from string A phone number associated with your Twilio account in E.164 Format.
args.body string The message to send.
Parameter Type Description
to string The recipient’s phone number in E.164 Format.
from string A phone number associated with your Twilio account in E.164 Format
body string The message to send.
Parameter Type Description
to String The recipient’s phone number in E.164 Format.
from String A phone number associated with your Twilio account in E.164 Format.
body String The message to send.
Parameter Type Description
to String The recipient’s phone number in E.164 Format.
from String A phone number associated with your Twilio account in E.164 Format.
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;
}