Actor picture

Twilio SMS Parser

onidivo/twilio-sms-parser

Parse incoming Twilio SMS message.

Author's avatarOnidivo Technologies
  • Modified
  • Users4
  • Runs24
Actor picture

Twilio SMS Parser

Dockerfile

# Dockerfile contains instructions how to build a Docker image that
# will contain all the code and configuration needed to run your actor.
# For a full Dockerfile reference,
# see https://docs.docker.com/engine/reference/builder/

# First, specify the base Docker image. Apify provides the following
# base images for your convenience:
#  apify/actor-node-basic (Node.js on Alpine Linux, small and fast)
#  apify/actor-node-chrome (Node.js + Chrome on Debian)
#  apify/actor-node-chrome-xvfb (Node.js + Chrome + Xvfb on Debian)
# For more information, see https://docs.apify.com/actor/build#base-images
# Note that you can use any other image from Docker Hub.
FROM apify/actor-node-basic

# Second, copy just package.json since it should be the only file
# that affects "npm install" in the next step, to speed up the build
COPY package.json ./

# Install NPM packages, skip optional and development dependencies to
# keep the image small. Avoid logging too much and print the dependency
# tree for debugging
RUN npm --quiet set progress=false \
 && npm install --only=prod --no-optional \
 && echo "Installed NPM packages:" \
 && npm list || true \
 && echo "Node.js version:" \
 && node --version \
 && echo "NPM version:" \
 && npm --version

# Next, copy the remaining files and directories with the source code.
# Since we do this after NPM install, quick build will be really fast
# for most source file changes.
COPY . ./

# Optionally, specify how to launch the source code of your actor.
# By default, Apify's base Docker images define the CMD instruction
# that runs the Node.js source code using the command specified
# in the "scripts.start" section of the package.json file.
# In short, the instruction looks something like this:
#
# CMD npm start

README.md

# Twilio SMS Parser

The actor parse incoming Twilio SMS message received via webhook.

# How To Setup

Add the following in the webhook input of SMSs settings for your Twilio phone number
https://api.apify.com/v2/acts/onidivo~twilio-sms-parser/runs?token=[APIFY_TOKEN]

# How To Get Your SMS Message

You can get the last received SMS message data from here
https://api.apify.com/v2/key-value-stores/[twilio-sms-parser_dataset_id]/records/[PHONE_NUMBER]

main.js

const Apify = require('apify');
const moment = require('moment');

const { log } = Apify.utils;
const IS_TEST_MODE = process.env.IS_TEST_MODE === 'true';
const TEST_MESSAGE_DATA = 'ToCountry=US&ToState=NJ&SmsMessageSid=SMba0d9db1db81dda8f486e5cd53015502&NumMedia=0&ToCity=HACKENSACK&FromZip=16915&SmsSid=SMba0d9db1db81&FromState=PA&SmsStatus=received&FromCity=GENESEE&Body=589665+is+your+activation+code.&FromCountry=US&To=%2B12015842345&ToZip=07930&NumSegments=1&MessageSid=SMba0d9db1db81dda8f48&AccountSid=ACd7d487f65588e45a5sdsds&From=%2B18142223452&ApiVersion=2010-04-01';

Apify.main(async () => {
    log.info('Getting message data from input');
    const messageDataAsString = IS_TEST_MODE
        ? TEST_MESSAGE_DATA
        : (await Apify.getInput()).toString();

    const messageData = {};
    for (const [k, v] of new URLSearchParams(messageDataAsString).entries()) {
        messageData[k] = v;
    }
    messageData.receivedAt = moment.utc().valueOf();

    const phoneNbre = messageData.To.replace('+', '00');

    const sharedKvs = await Apify.openKeyValueStore('twilio-sms-parser');

    log.info(`Saving message data for phone number "${phoneNbre}"`);
    await sharedKvs.setValue(phoneNbre, messageData);
});

package.json

{
    "name": "twilio-sms-parser",
    "version": "0.0.1",
    "dependencies": {
        "apify": "^0.20.2",
        "moment": "^2.24.0"
    },
    "scripts": {
        "start": "node main.js"
    },
    "author": "Youcef Islam Remichi - Onidivo"
}