tw_get_phones: Purchase Twilio Phones Algorithmically

View source: R/twilio_meta.R

tw_get_phonesR Documentation

Purchase Twilio Phones Algorithmically

Description

Intended use: Take a random sample of phones from the list you need to text. Use this function to actually purchase and add to an existing message service.

Usage

tw_get_phones(
  phones, 
  locality = NA,
  state = NA, 
  project, 
  msgsvc_sid, 
  sid = Sys.getenv('TWILIO_ACCOUNT_SID'),
  key = Sys.getenv('TWILIO_KEY'),
  secret = Sys.getenv('TWILIO_SECRET'),
  max_dist=75
  )

Arguments

phones

Vector of phones number to match, 10 digit (2025555555) or 12 digit (+12025555555), or 3-digit area code. TODO: Other formats.

locality

Optional vector of localities, as a supplement or alternative to providing phones/area codes.

state

Optional vector of states — if state is supplied & twilio's response phone is out of state, phone will not be purchased.

project

Project name. Will be incorporated into phone Friendly Name.

msgsvc_sid

The message service SID to add the phone to.

sid

twilio credentials: Account SID.

key

twilio credentials: Account user key.

secret

twilio credentials: User secret. Don't store this in scripts!!!

max_dist

Maximum search distance from provided phone. Default 75 miles.

Examples


## Not run: 
source("C:/Users/lwolberg/Desktop/.ssh/twilio_auth.R")

x <- fr(filename)

# number of phones to buy
(nph <- ceiling(nrow(x)/200))

# before sample, remove low-traffic area codes
if(nchar(x$cell[1])==10) {
x[, areacode := substr(cell,1,3)]
} else if (nchar(x$cell[1])==12) {
  x[, areacode := substr(cell,3,5)]
} else stop("weird phone formatting, add an area code substring option.")
x
x %>%
  group_by(areacode) %>%
  tally %>%
  arrange(-n) %>%
  print(n=100)
(limit <- nrow(x)*.001)
# limit = 30 # if the limit is too low to make sense.
x %>%
  group_by(areacode) %>%
  tally %>%
  filter(n<limit) %>%
  as.data.table -> ac_rm
x[, sample := 1]
x[areacode %in% ac_rm$areacode, sample := 0]

phsamp <- sample(x[sample==1, cell], size=nph, replace=F)

y <- tw_get_phones(
  phones = phsamp,
  state = NA,
  project = "Working America",
  msgsvc_sid = "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  sid=sid, key=key, secret=secret
)


## End(Not run)

workingamerica/twilio documentation built on Jan. 30, 2024, 4:55 p.m.