@cmpsr/twilio
v0.3.0
Published
[![npm version](https://img.shields.io/npm/v/@cmpsr/twilio.svg?style=flat-square)](https://www.npmjs.com/package/@cmpsr/twilio)
Downloads
299
Readme
@cmpsr/twilio
Wrapper over Twilio API
Features
- Send sms using pre-registered numbers
- Build conversations with virtual spaces
Installing
Using npm:
$ npm install @cmpsr/twilio
Using yarn:
$ yarn add @cmpsr/twilio
Examples
CommonJS usage
const { useTwilioSms, useTwilioConversation } = require("@cmpsr/twilio");
ES usage
import { useTwilioSms, useTwilioConversation } from "@cmpsr/twilio";
Sending a sms
with ExpressJS
const express = require("express");
const { useTwilioSms } = require("@cmpsr/twilio");
const app = express();
app.use(express.json());
const twilioSms = useTwilioSms();
app.post("/send-sms", async (req, res) => {
const { message, from, to } = req.body;
const response = await twilioSms.send(message, from, to);
res.json(response);
});
Creating a conversation
with ExpressJS
const express = require("express");
const { useTwilioConversation } = require("@cmpsr/twilio");
const app = express();
app.use(express.json());
const twilioConversation = useTwilioConversation();
app.post("/", async (req, res) => {
const {
body: { friendlyName },
} = req;
const conversation = await twilioConversation.create(friendlyName);
res.json(conversation);
});
API
useTwilioSms([, accountSid[, authToken[, serviceId]]])
const accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const authToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const serviceId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const twilioSms = useTwilioSms(accountSid, authToken, serviceId);
NOTE
If you don't pass the optional parameters it will take those values from your environment variables, so you may want to create a .env
file or set global variables into your OS.
# in your .env file
TWILIO_ACCOUNT_SID = ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
TWILIO_AUTH_TOKEN = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
TWILIO_CHAT_SERVICE_SID = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Now, load your variables using the dotenv
package
require("dotenv").config();
const { useTwilioSms } = require("@cmpsr/twilio");
const twilioSms = useTwilioSms();
useTwilioConversation([, accountSid[, authToken[, serviceId]]])
const accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const authToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const serviceId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const twilioConversation = useTwilioConversation(
accountSid,
authToken,
serviceId
);
NOTE
If you don't pass the optional parameters it will take those values from your environment variables, so you may want to create a .env
file or set global variables into your OS.
# in your .env file
TWILIO_ACCOUNT_SID = ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
TWILIO_AUTH_TOKEN = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
TWILIO_CHAT_SERVICE_SID = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Now, load your variables using the dotenv
package
require("dotenv").config();
const { twilioConversation } = require("@cmpsr/twilio");
const twilioConversation = twilioConversation();
SMS Instance methods
The available instance methods are listed below.