@yaks/voipms 0.0.0-stage → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +80 -2
- package/_dist/graph.d.ts +3 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/mod.d.ts +121 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/request.d.ts +27 -0
- package/_dist/request.d.ts.map +1 -0
- package/_dist/routes.d.ts +3 -0
- package/_dist/routes.d.ts.map +1 -0
- package/deno.json +1 -0
- package/graph.js +7 -0
- package/graph.js.map +1 -0
- package/graph.ts +7 -0
- package/mod.js +521 -0
- package/mod.js.map +1 -0
- package/mod.ts +606 -0
- package/package.json +26 -4
- package/request.js +115 -0
- package/request.js.map +1 -0
- package/request.ts +182 -0
- package/routes.js +7 -0
- package/routes.js.map +1 -0
- package/routes.ts +7 -0
- package/voice.js +240 -0
- package/voice.js.map +1 -0
- package/voice.ts +316 -0
package/mod.ts
ADDED
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
// voip.ms: a sender over its REST API, the numbers an account holds, and what
|
|
2
|
+
// its SMS URL callback says. A person's own number texts from here, signed in
|
|
3
|
+
// as their account: every call carries its username and API password, and
|
|
4
|
+
// comes from an address its API allowlist accepts. The callback is a GET
|
|
5
|
+
// nobody signs, so the URL a host gives it carries the host's own secret.
|
|
6
|
+
// https://voip.ms/m/apidocs.php (behind a sign-in; the parameters here are
|
|
7
|
+
// as its docs and an account's answers name them)
|
|
8
|
+
|
|
9
|
+
import { type Clock, lazy, system } from '@yaks/fp'
|
|
10
|
+
import { Unsent } from '@yaks/host/senders'
|
|
11
|
+
import {
|
|
12
|
+
type Account,
|
|
13
|
+
type Arrival,
|
|
14
|
+
type Attempt,
|
|
15
|
+
Blocked,
|
|
16
|
+
type Held,
|
|
17
|
+
type Numbers,
|
|
18
|
+
type Offer,
|
|
19
|
+
type Provider,
|
|
20
|
+
type Receipt,
|
|
21
|
+
type Text,
|
|
22
|
+
} from '@yaks/phone'
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
bounded,
|
|
26
|
+
type Call,
|
|
27
|
+
type Params,
|
|
28
|
+
requester,
|
|
29
|
+
TimedOut,
|
|
30
|
+
VOIPMS,
|
|
31
|
+
type Voipms,
|
|
32
|
+
} from './request.js'
|
|
33
|
+
import {
|
|
34
|
+
abandon,
|
|
35
|
+
accountFor,
|
|
36
|
+
allow,
|
|
37
|
+
callsAs,
|
|
38
|
+
cleanup,
|
|
39
|
+
heldDid,
|
|
40
|
+
login,
|
|
41
|
+
made,
|
|
42
|
+
release,
|
|
43
|
+
} from './voice.js'
|
|
44
|
+
export { VOIPMS, type Voipms } from './request.js'
|
|
45
|
+
|
|
46
|
+
// voip.ms writes a North American number as its ten digits.
|
|
47
|
+
let ten = (number: string) =>
|
|
48
|
+
number.replace(/\D/g, '').replace(/^1(?=\d{10}$)/, '')
|
|
49
|
+
|
|
50
|
+
/** A number as voip.ms writes it, in E.164 form: ten digits are North
|
|
51
|
+
* American. */
|
|
52
|
+
export let e164 = (digits: string): string => {
|
|
53
|
+
let d = digits.replace(/\D/g, '')
|
|
54
|
+
return d.length == 10 ? '+1' + d : d ? '+' + d : ''
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type Did = Record<string, string | number | null | undefined>
|
|
58
|
+
|
|
59
|
+
let didsOf = async (call: Call) =>
|
|
60
|
+
((await call('getDIDsInfo')).dids ?? []) as Did[]
|
|
61
|
+
|
|
62
|
+
let point = async (call: Call, at: string, url?: string, before?: string) => {
|
|
63
|
+
let did = (await didsOf(call)).find((d) =>
|
|
64
|
+
ten(String(d.did ?? '')) == ten(at)
|
|
65
|
+
)
|
|
66
|
+
if (!did) throw new Error(`${at} is not a number on this voip.ms account`)
|
|
67
|
+
// voip.ms is slow to set texts, and a setting already in place needs no
|
|
68
|
+
// second asking.
|
|
69
|
+
let on = String(did.sms_enabled) == '1' &&
|
|
70
|
+
String(did.sms_url_callback_enabled) == '1'
|
|
71
|
+
if (before && (!on || did.sms_url_callback != before)) return
|
|
72
|
+
if (url ? on && did.sms_url_callback == url : !on) return
|
|
73
|
+
// setSMS sets every way a number's texts reach its owner at once, so
|
|
74
|
+
// each is said again as the account has it; no url turns the callback off.
|
|
75
|
+
let flag = (v: unknown) => String(v) == '1' ? '1' : '0'
|
|
76
|
+
await call('setSMS', {
|
|
77
|
+
did: ten(at),
|
|
78
|
+
enable: '1',
|
|
79
|
+
email_enabled: flag(did.sms_email_enabled),
|
|
80
|
+
email_address: String(did.sms_email ?? ''),
|
|
81
|
+
sms_forward_enable: flag(did.sms_forward_enabled),
|
|
82
|
+
sms_forward: String(did.sms_forward ?? ''),
|
|
83
|
+
sms_sipaccount_enabled: flag(did.sms_sipaccount_enabled),
|
|
84
|
+
sms_sipaccount: String(did.sms_sipaccount ?? ''),
|
|
85
|
+
url_callback_enable: url ? '1' : '0',
|
|
86
|
+
url_callback: url ?? '',
|
|
87
|
+
url_callback_retry: url ? '1' : '0',
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** What a voip.ms {@link Account} asks for: its numbers, a text sent, the
|
|
92
|
+
* texts it sent, and where a number's texts go. */
|
|
93
|
+
export let TEXTING = [
|
|
94
|
+
'getDIDsInfo',
|
|
95
|
+
'sendSMS',
|
|
96
|
+
'sendMMS',
|
|
97
|
+
'getSMS',
|
|
98
|
+
'getMMS',
|
|
99
|
+
'setSMS',
|
|
100
|
+
]
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The id voip.ms gives a text `did` sent `dst` in exactly these words since
|
|
104
|
+
* `since`, as its sent texts list them (`getSMS`, or `getMMS` for a long one,
|
|
105
|
+
* in UTC); null when it lists none, and an error when it cannot say.
|
|
106
|
+
*/
|
|
107
|
+
let sentSince = async (
|
|
108
|
+
call: Call,
|
|
109
|
+
sms: boolean,
|
|
110
|
+
{ did, dst, message }: Params,
|
|
111
|
+
since: number,
|
|
112
|
+
now: number,
|
|
113
|
+
): Promise<string | null> => {
|
|
114
|
+
let day = (t: number) => new Date(t).toISOString().slice(0, 10)
|
|
115
|
+
let said: Record<string, unknown> = await call(sms ? 'getSMS' : 'getMMS', {
|
|
116
|
+
from: day(since - 60_000),
|
|
117
|
+
to: day(now),
|
|
118
|
+
did,
|
|
119
|
+
contact: dst,
|
|
120
|
+
type: '0',
|
|
121
|
+
timezone: '0',
|
|
122
|
+
limit: '50',
|
|
123
|
+
}).catch((error) => {
|
|
124
|
+
if (/no_(sms|mms)\b/.test(String(error))) return {}
|
|
125
|
+
throw error
|
|
126
|
+
})
|
|
127
|
+
let list = (said[sms ? 'sms' : 'mms'] ?? []) as Record<string, unknown>[]
|
|
128
|
+
let hit = list.find((t) =>
|
|
129
|
+
String(t.message) == message &&
|
|
130
|
+
Date.parse(String(t.date).replace(' ', 'T') + 'Z') >= since - 60_000
|
|
131
|
+
)
|
|
132
|
+
return hit ? String(hit.id) : null
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* A voip.ms {@link Account}, which can only read its numbers, text from them
|
|
137
|
+
* and point their texts: it is made from `methods` (default
|
|
138
|
+
* {@link TEXTING}), and its requests ask for no other. A person's own
|
|
139
|
+
* account is made from the methods its integration lists, so nothing it
|
|
140
|
+
* holds can cancel, release or reroute one of their numbers. A text of up to {@link VOIPMS}.sms characters
|
|
141
|
+
* goes as SMS, a longer one as MMS. A status voip.ms lists in
|
|
142
|
+
* {@link VOIPMS}.blocked rejects with {@link Blocked}; one in `denied`, which
|
|
143
|
+
* the account's owner mends, with {@link Denied}; a connection that failed
|
|
144
|
+
* or a 5xx with `Unsent`; anything else with an Error carrying voip.ms's
|
|
145
|
+
* words. voip.ms often takes a text and answers after the call's time is
|
|
146
|
+
* up: the text is then never handed over again, and whether it went is what
|
|
147
|
+
* voip.ms lists as sent, asked at once and again ten seconds later.
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* import { equal } from '@yaks/testing'
|
|
151
|
+
* import { voipms } from '@yaks/voipms'
|
|
152
|
+
* let asked: URL[] = []
|
|
153
|
+
* let line = voipms({
|
|
154
|
+
* user: 'jeff@example.com',
|
|
155
|
+
* key: 'pw',
|
|
156
|
+
* number: '+12695550100',
|
|
157
|
+
* fetch: (url) => {
|
|
158
|
+
* asked.push(new URL(String(url)))
|
|
159
|
+
* return Promise.resolve(Response.json({ status: 'success', sms: 41 }))
|
|
160
|
+
* },
|
|
161
|
+
* })
|
|
162
|
+
* equal(await line.send({ to: '+12695550142', text: 'Hi' }), { id: '41' })
|
|
163
|
+
* equal(asked[0].searchParams.get('method'), 'sendSMS')
|
|
164
|
+
* equal(asked[0].searchParams.get('dst'), '2695550142')
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export let voipms = (
|
|
168
|
+
{ number, methods = TEXTING, ...account }: Voipms,
|
|
169
|
+
time: Clock = system,
|
|
170
|
+
attempt?: Attempt,
|
|
171
|
+
): Account => {
|
|
172
|
+
let call = bounded(
|
|
173
|
+
requester({ ...account, methods }),
|
|
174
|
+
time,
|
|
175
|
+
attempt?.by,
|
|
176
|
+
!attempt,
|
|
177
|
+
)
|
|
178
|
+
let lines = async (): Promise<Held[]> =>
|
|
179
|
+
(await didsOf(call)).map((d) => ({
|
|
180
|
+
number: e164(String(d.did ?? '')),
|
|
181
|
+
sms: String(d.sms_enabled) == '1',
|
|
182
|
+
...d.description ? { label: String(d.description) } : {},
|
|
183
|
+
...String(d.sms_url_callback_enabled) == '1' && d.sms_url_callback
|
|
184
|
+
? { callback: String(d.sms_url_callback) }
|
|
185
|
+
: {},
|
|
186
|
+
}))
|
|
187
|
+
// The number texts go from, asked until it answers and kept as a value
|
|
188
|
+
// (@yaks/fp `lazy`): a client may serve many requests.
|
|
189
|
+
let from = lazy(() =>
|
|
190
|
+
number ? Promise.resolve(number) : lines().then((all) => {
|
|
191
|
+
let line = all.find((l) => l.sms)
|
|
192
|
+
if (!line) throw new Error('No number on this voip.ms account texts')
|
|
193
|
+
return line.number
|
|
194
|
+
})
|
|
195
|
+
)
|
|
196
|
+
return {
|
|
197
|
+
lines,
|
|
198
|
+
from,
|
|
199
|
+
point: (at, url, before) => point(call, at, url, before),
|
|
200
|
+
send: async ({ to, text, from: given }: Text): Promise<Receipt> => {
|
|
201
|
+
if (text.length > VOIPMS.mms) {
|
|
202
|
+
throw new Blocked(VOIPMS.blocked.sms_toolong, 'sms_toolong')
|
|
203
|
+
}
|
|
204
|
+
let sms = text.length <= VOIPMS.sms
|
|
205
|
+
let method = sms ? 'sendSMS' : 'sendMMS'
|
|
206
|
+
// A text goes at most once: one voip.ms may have taken before its
|
|
207
|
+
// answer timed out is never handed over again, so it is no Unsent.
|
|
208
|
+
// Whether it went is what voip.ms lists as sent since it was asked.
|
|
209
|
+
let asked = {
|
|
210
|
+
did: ten(given ?? await from()),
|
|
211
|
+
dst: ten(to),
|
|
212
|
+
message: text,
|
|
213
|
+
}
|
|
214
|
+
let since = time.now()
|
|
215
|
+
let said = await call(method, asked).catch(async (error) => {
|
|
216
|
+
if (!(error instanceof TimedOut)) throw error
|
|
217
|
+
let look = () => sentSince(call, sms, asked, since, time.now())
|
|
218
|
+
let id = await look().catch(() => null) ??
|
|
219
|
+
await new Promise((go) => time.after(10_000, () => go(null)))
|
|
220
|
+
.then(look).catch(() => null)
|
|
221
|
+
if (id) return { [sms ? 'sms' : 'mms']: id }
|
|
222
|
+
throw new Error(`voip.ms did not answer ${method} in time`)
|
|
223
|
+
})
|
|
224
|
+
let id = said[sms ? 'sms' : 'mms']
|
|
225
|
+
return id == null ? {} : { id: String(id) }
|
|
226
|
+
},
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* What a voip.ms SMS URL callback says, from the query {@link VOIPMS}.callback
|
|
232
|
+
* asks for, once the host has found its own secret in the URL, arriving `at`;
|
|
233
|
+
* null when it names no message.
|
|
234
|
+
*
|
|
235
|
+
* ```ts
|
|
236
|
+
* import { equal } from '@yaks/testing'
|
|
237
|
+
* import { called } from '@yaks/voipms'
|
|
238
|
+
* let arrival = called(new URLSearchParams(
|
|
239
|
+
* 'id=77&from=2695550142&to=2695550100&message=Tuesday%20works',
|
|
240
|
+
* ))
|
|
241
|
+
* equal(arrival?.from, '+12695550142')
|
|
242
|
+
* equal(arrival?.text, 'Tuesday works')
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
export let called = (
|
|
246
|
+
query: URLSearchParams,
|
|
247
|
+
at: string = new Date().toISOString(),
|
|
248
|
+
): Arrival | null => {
|
|
249
|
+
let id = query.get('id')
|
|
250
|
+
if (!id) return null
|
|
251
|
+
return {
|
|
252
|
+
id,
|
|
253
|
+
from: e164(query.get('from') ?? ''),
|
|
254
|
+
to: e164(query.get('to') ?? ''),
|
|
255
|
+
text: query.get('message') ?? '',
|
|
256
|
+
at,
|
|
257
|
+
rcs: false,
|
|
258
|
+
media: [],
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
type Available = Record<string, string | number | undefined>
|
|
263
|
+
|
|
264
|
+
// Letters alone, uppercased: how a city and a rate center are compared.
|
|
265
|
+
let letters = (name: string) => name.toUpperCase().replace(/[^A-Z]/g, '')
|
|
266
|
+
|
|
267
|
+
// Whether `short` is `long` with letters left out: BATTLECRK is BATTLECREEK.
|
|
268
|
+
let within = (short: string, long: string) => {
|
|
269
|
+
let i = 0
|
|
270
|
+
for (let c of long) if (c == short[i]) i++
|
|
271
|
+
return i == short.length
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The rate centers among `centers` a city is in. voip.ms names a rate center
|
|
276
|
+
* in at most ten capitals, cutting letters out of longer names ("BATTLE CRK",
|
|
277
|
+
* "MIDDLEVL") or adding a zone ("DETROITZN1"), so a center is the city's when
|
|
278
|
+
* it is the city, starts with it, or is it with letters left out; when one is
|
|
279
|
+
* the city exactly, only that one is.
|
|
280
|
+
*
|
|
281
|
+
* ```ts
|
|
282
|
+
* import { equal } from '@yaks/testing'
|
|
283
|
+
* import { centersOf } from '@yaks/voipms'
|
|
284
|
+
* let centers = ['BATTLE CRK', 'DETROITZN1', 'DETROITZN2', 'GRANT', 'KALAMAZOO']
|
|
285
|
+
* equal(centersOf('Battle Creek', centers), ['BATTLE CRK'])
|
|
286
|
+
* equal(centersOf('Detroit', centers), ['DETROITZN1', 'DETROITZN2'])
|
|
287
|
+
* equal(centersOf('Grand Rapids', centers), [])
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
290
|
+
export let centersOf = (city: string, centers: string[]): string[] => {
|
|
291
|
+
let want = letters(city)
|
|
292
|
+
let exact = centers.filter((c) => letters(c) == want)
|
|
293
|
+
if (exact.length) return exact
|
|
294
|
+
return centers.filter((c) => {
|
|
295
|
+
let have = letters(c)
|
|
296
|
+
return have[0] == want[0] &&
|
|
297
|
+
(have.startsWith(want) || within(have, want))
|
|
298
|
+
})
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Some of `all`, chosen at random, so any number asked for is any.
|
|
302
|
+
let some = <T>(all: T[], n: number) =>
|
|
303
|
+
all.map((x) => [Math.random(), x] as const).sort((a, b) => a[0] - b[0])
|
|
304
|
+
.slice(0, n).map(([, x]) => x)
|
|
305
|
+
|
|
306
|
+
// The sub-account a number bought here rings, by its ten digits.
|
|
307
|
+
// An offer read precedes a purchase. Leave time for that read, giving back
|
|
308
|
+
// any number already ordered, and the platform's own writes within a minute.
|
|
309
|
+
let FIND = 16_000
|
|
310
|
+
let BUY = 28_000
|
|
311
|
+
let CLEANUP = 10_000
|
|
312
|
+
let uncertain = (error: unknown) => error instanceof Unsent
|
|
313
|
+
|
|
314
|
+
// A number this account routed to the sub-account made for it.
|
|
315
|
+
let routed = (did: Available, digits: string) =>
|
|
316
|
+
String(did.routing).endsWith(`_${accountFor(digits)}`)
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* voip.ms's numbers, sold from our account: local US numbers found by area
|
|
320
|
+
* code, by state and city, by digits they contain, or any, at voip.ms's
|
|
321
|
+
* price by the minute. A number bought rings a sub-account of its own, which
|
|
322
|
+
* a softphone signs in to (`login`) and which calls out as the number, and
|
|
323
|
+
* `point` sends its texts to a host's URL. Buying a number the account
|
|
324
|
+
* already holds on the sub-account made for it completes, and `find` quotes
|
|
325
|
+
* it, so a purchase that ended before its host kept the number is finished
|
|
326
|
+
* by buying it again. `allow` stops its calls out, in, or both, and lets
|
|
327
|
+
* them go again, its password untouched. `calls` reads its calls from
|
|
328
|
+
* voip.ms's call records, each at what voip.ms charged for it, in UTC
|
|
329
|
+
* (`timezone` 0). voip.ms sells no toll-free
|
|
330
|
+
* number here, so none is ever verified.
|
|
331
|
+
*
|
|
332
|
+
* ```ts
|
|
333
|
+
* import { equal } from '@yaks/testing'
|
|
334
|
+
* import { numbers } from '@yaks/voipms'
|
|
335
|
+
* let said: Record<string, object> = {
|
|
336
|
+
* searchDIDsUSA: { dids: [{ did: '2692015487', ratecenter: 'WATERVLIET',
|
|
337
|
+
* state: 'MI', perminute_setup: '0.40', perminute_monthly: '1.10',
|
|
338
|
+
* sms: 1 }] },
|
|
339
|
+
* }
|
|
340
|
+
* let shop = numbers({ user: 'u', key: 'k', fetch: (url) => {
|
|
341
|
+
* let method = new URL(String(url)).searchParams.get('method')!
|
|
342
|
+
* return Promise.resolve(Response.json({ status: 'success', ...said[method] }))
|
|
343
|
+
* } })
|
|
344
|
+
* let [offer] = await shop.find({ area: '269' })
|
|
345
|
+
* equal(offer.number, '+12692015487')
|
|
346
|
+
* equal([offer.upfront, offer.monthly, offer.place], [0.4, 1.1, 'WATERVLIET, MI'])
|
|
347
|
+
* ```
|
|
348
|
+
*/
|
|
349
|
+
export let numbers = (
|
|
350
|
+
{ user, key, api, fetch }: Omit<Voipms, 'number' | 'methods'>,
|
|
351
|
+
time: Clock = system,
|
|
352
|
+
): Numbers => {
|
|
353
|
+
let request = requester({ user, key, api, fetch })
|
|
354
|
+
let call = bounded(request, time)
|
|
355
|
+
let found = async (call: Call, method: string, params: Params) => {
|
|
356
|
+
try {
|
|
357
|
+
return ((await call(method, params)).dids ?? []) as Available[]
|
|
358
|
+
} catch (error) {
|
|
359
|
+
// voip.ms answers a search that finds nothing as a failure.
|
|
360
|
+
if (/no_did|unavailable_info/.test(String(error))) return []
|
|
361
|
+
throw error
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
let centers = async (call: Call, state: string) =>
|
|
365
|
+
(((await call('getRateCentersUSA', { state })).ratecenters ?? []) as {
|
|
366
|
+
ratecenter: string
|
|
367
|
+
}[]).map((c) => c.ratecenter)
|
|
368
|
+
let states = async (call: Call) =>
|
|
369
|
+
(((await call('getStates')).states ?? []) as { state: string }[])
|
|
370
|
+
.map((s) => s.state)
|
|
371
|
+
let offered = (d: Available): Offer => ({
|
|
372
|
+
number: e164(String(d.did)),
|
|
373
|
+
upfront: Number(d.perminute_setup ?? 0),
|
|
374
|
+
monthly: Number(d.perminute_monthly ?? 0),
|
|
375
|
+
currency: 'USD',
|
|
376
|
+
sms: String(d.sms) == '1',
|
|
377
|
+
voice: true,
|
|
378
|
+
place: `${d.ratecenter}, ${d.state}`,
|
|
379
|
+
})
|
|
380
|
+
return {
|
|
381
|
+
find: async (s) => {
|
|
382
|
+
if (s.tollFree || (s.country ?? 'US') != 'US') return []
|
|
383
|
+
let search = bounded(request, time, time.now() + FIND)
|
|
384
|
+
let limit = s.limit ?? 5
|
|
385
|
+
let all: Available[]
|
|
386
|
+
if (s.number) {
|
|
387
|
+
let digits = ten(s.number)
|
|
388
|
+
all = (await found(search, 'searchDIDsUSA', {
|
|
389
|
+
type: 'starts',
|
|
390
|
+
query: digits,
|
|
391
|
+
}))
|
|
392
|
+
.filter((d) => String(d.did) == digits)
|
|
393
|
+
let held = all.length ? undefined : await heldDid(search, digits)
|
|
394
|
+
if (held && routed(held, digits)) {
|
|
395
|
+
// Bought already, so only its month is left to pay, at what its
|
|
396
|
+
// rate center's numbers cost.
|
|
397
|
+
let [ratecenter, state] = String(held.description).split(', ')
|
|
398
|
+
let [like] = await found(search, 'getDIDsUSA', { state, ratecenter })
|
|
399
|
+
return [{
|
|
400
|
+
...offered({ ...like, did: digits, ratecenter, state, sms: 1 }),
|
|
401
|
+
upfront: 0,
|
|
402
|
+
}]
|
|
403
|
+
}
|
|
404
|
+
} else if (s.region || !s.area && !s.contains) {
|
|
405
|
+
let state = s.region?.toUpperCase() ??
|
|
406
|
+
some(await states(search), 1)[0]
|
|
407
|
+
let at = s.city
|
|
408
|
+
? centersOf(s.city, await centers(search, state))
|
|
409
|
+
: [undefined]
|
|
410
|
+
all = (await Promise.all(
|
|
411
|
+
at.map((ratecenter) =>
|
|
412
|
+
found(search, 'getDIDsUSA', {
|
|
413
|
+
state,
|
|
414
|
+
...ratecenter ? { ratecenter } : {},
|
|
415
|
+
})
|
|
416
|
+
),
|
|
417
|
+
)).flat()
|
|
418
|
+
} else {
|
|
419
|
+
all = await found(search, 'searchDIDsUSA', {
|
|
420
|
+
type: s.area ? 'starts' : 'contains',
|
|
421
|
+
query: s.area ?? s.contains!,
|
|
422
|
+
})
|
|
423
|
+
}
|
|
424
|
+
return some(
|
|
425
|
+
all.filter((d) =>
|
|
426
|
+
String(d.sms) == '1' &&
|
|
427
|
+
(!s.area || String(d.did).startsWith(s.area)) &&
|
|
428
|
+
(!s.contains || String(d.did).includes(s.contains))
|
|
429
|
+
),
|
|
430
|
+
limit,
|
|
431
|
+
).map(offered)
|
|
432
|
+
},
|
|
433
|
+
buy: async (number) => {
|
|
434
|
+
let digits = ten(number)
|
|
435
|
+
let heldOrder = { number, order: digits, status: 'held' } as const
|
|
436
|
+
let purchase = bounded(request, time, time.now() + BUY)
|
|
437
|
+
// A number already ringing the sub-account made for it was bought
|
|
438
|
+
// here before, by a purchase that ended before its host kept it.
|
|
439
|
+
let held = await heldDid(purchase, digits)
|
|
440
|
+
if (held && routed(held, digits)) {
|
|
441
|
+
return heldOrder
|
|
442
|
+
}
|
|
443
|
+
if (held) {
|
|
444
|
+
throw new Error(`voip.ms already holds ${number} on another route`)
|
|
445
|
+
}
|
|
446
|
+
let routing = 'sys:hangup'
|
|
447
|
+
try {
|
|
448
|
+
// A sub-account calls out as a number the account already holds.
|
|
449
|
+
// Hang up on calls until the sub-account it rings is made.
|
|
450
|
+
await purchase('orderDID', {
|
|
451
|
+
did: digits,
|
|
452
|
+
routing,
|
|
453
|
+
pop: VOIPMS.pop,
|
|
454
|
+
dialtime: '60',
|
|
455
|
+
cnam: '1',
|
|
456
|
+
billing_type: VOIPMS.billing,
|
|
457
|
+
})
|
|
458
|
+
} catch (error) {
|
|
459
|
+
if (!uncertain(error)) throw error
|
|
460
|
+
let recovery = bounded(request, time, time.now() + CLEANUP)
|
|
461
|
+
let found = await heldDid(recovery, digits).catch((e) => {
|
|
462
|
+
if (!uncertain(e)) throw e
|
|
463
|
+
return undefined
|
|
464
|
+
})
|
|
465
|
+
return found?.routing == routing || found && routed(found, digits)
|
|
466
|
+
? heldOrder
|
|
467
|
+
: { ...heldOrder, status: 'pending' }
|
|
468
|
+
}
|
|
469
|
+
try {
|
|
470
|
+
let account = await made(purchase, digits, accountFor(digits), true)
|
|
471
|
+
await purchase('setDIDRouting', {
|
|
472
|
+
did: digits,
|
|
473
|
+
routing: `account:${account.account}`,
|
|
474
|
+
})
|
|
475
|
+
} catch (error) {
|
|
476
|
+
// The DID is held. The directory's voice reconciler can finish the
|
|
477
|
+
// sub-account and routing even if this request has ended.
|
|
478
|
+
if (uncertain(error)) return heldOrder
|
|
479
|
+
let cleanup = bounded(request, time, time.now() + CLEANUP)
|
|
480
|
+
try {
|
|
481
|
+
await abandon(cleanup, digits)
|
|
482
|
+
} catch (cause) {
|
|
483
|
+
throw new Error(
|
|
484
|
+
`${
|
|
485
|
+
(error as Error).message
|
|
486
|
+
}. Cleanup of ${number} could not be confirmed: ` +
|
|
487
|
+
`${
|
|
488
|
+
(cause as Error).message
|
|
489
|
+
}. Check the carrier account before retrying.`,
|
|
490
|
+
{ cause: new AggregateError([error, cause]) },
|
|
491
|
+
)
|
|
492
|
+
}
|
|
493
|
+
throw error
|
|
494
|
+
}
|
|
495
|
+
return heldOrder
|
|
496
|
+
},
|
|
497
|
+
ordered: async (number) => {
|
|
498
|
+
let digits = ten(number)
|
|
499
|
+
let did = await heldDid(call, digits)
|
|
500
|
+
return !!did && (did.routing == 'sys:hangup' || routed(did, digits))
|
|
501
|
+
},
|
|
502
|
+
// voip.ms takes a number's texting settings only a while after it is
|
|
503
|
+
// ordered, so a host points it once it puts the number to use.
|
|
504
|
+
point: (number, callback) => point(call, number, callback),
|
|
505
|
+
release: (number) => release(call, ten(number)),
|
|
506
|
+
cleanup: (number) => cleanup(call, ten(number)),
|
|
507
|
+
verify: () =>
|
|
508
|
+
Promise.reject(new Error('voip.ms sells no toll-free number to verify')),
|
|
509
|
+
verification: () =>
|
|
510
|
+
Promise.reject(new Error('voip.ms files no toll-free verification')),
|
|
511
|
+
calls: async (number, since) => {
|
|
512
|
+
let digits = ten(number)
|
|
513
|
+
let day = (t: number) => new Date(t).toISOString().slice(0, 10)
|
|
514
|
+
// voip.ms answers a month of records at most.
|
|
515
|
+
let from = Math.max(Date.parse(since), Date.now() - 30 * 86_400_000)
|
|
516
|
+
let said = await call('getCDR', {
|
|
517
|
+
date_from: day(from),
|
|
518
|
+
date_to: day(Date.now()),
|
|
519
|
+
timezone: '0',
|
|
520
|
+
answered: '1',
|
|
521
|
+
noanswer: '1',
|
|
522
|
+
busy: '1',
|
|
523
|
+
failed: '1',
|
|
524
|
+
}).catch((error) => {
|
|
525
|
+
if (/no_cdr/.test(String(error))) return { cdr: [] }
|
|
526
|
+
throw error
|
|
527
|
+
})
|
|
528
|
+
return ((said.cdr ?? []) as Available[])
|
|
529
|
+
.filter((c) =>
|
|
530
|
+
String(c.destination) == digits ||
|
|
531
|
+
callsAs(digits, String(c.account))
|
|
532
|
+
)
|
|
533
|
+
.map((c) => ({
|
|
534
|
+
id: String(c.uniqueid),
|
|
535
|
+
at: new Date(`${String(c.date).replace(' ', 'T')}Z`).toISOString(),
|
|
536
|
+
seconds: Number(c.seconds ?? 0),
|
|
537
|
+
cost: Number(c.total ?? 0),
|
|
538
|
+
}))
|
|
539
|
+
.filter((c) => c.at > since)
|
|
540
|
+
.sort((a, b) => a.at.localeCompare(b.at))
|
|
541
|
+
},
|
|
542
|
+
allow: (number, voice, submit) => allow(call, ten(number), voice, submit),
|
|
543
|
+
login: (number, seat, voice, submit) =>
|
|
544
|
+
login(call, ten(number), seat, voice, submit),
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// A voip.ms account from the settings a host gives it.
|
|
549
|
+
let opened = (
|
|
550
|
+
options: import('@yaks/phone').Transport,
|
|
551
|
+
attempt?: Attempt,
|
|
552
|
+
): Account => {
|
|
553
|
+
if (!options.key || !options.user) {
|
|
554
|
+
throw new Error('a voipms sender needs `user` and `key`')
|
|
555
|
+
}
|
|
556
|
+
return voipms(
|
|
557
|
+
{ ...options, key: options.key, user: options.user },
|
|
558
|
+
system,
|
|
559
|
+
attempt,
|
|
560
|
+
)
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* A person's own voip.ms account, connected with their sign-in email and API
|
|
565
|
+
* password: an {@link Account} that asks for only the `methods` their
|
|
566
|
+
* connection's integration lists, and none when it lists none. It never
|
|
567
|
+
* holds our own account's {@link numbers}, which alone order, cancel and
|
|
568
|
+
* route numbers.
|
|
569
|
+
*
|
|
570
|
+
* ```ts
|
|
571
|
+
* import { equal, ok } from '@yaks/testing'
|
|
572
|
+
* import { connected } from '@yaks/voipms'
|
|
573
|
+
* let line = connected({ via: 'voipms', user: 'u', key: 'k', methods: [] })
|
|
574
|
+
* equal(Object.keys(line).sort(), ['from', 'lines', 'point', 'send'])
|
|
575
|
+
* ok(!('release' in line))
|
|
576
|
+
* ```
|
|
577
|
+
*/
|
|
578
|
+
export let connected = (
|
|
579
|
+
options: import('@yaks/phone').Transport,
|
|
580
|
+
attempt?: Attempt,
|
|
581
|
+
): Account => opened({ ...options, methods: options.methods ?? [] }, attempt)
|
|
582
|
+
|
|
583
|
+
/** The transport plugged into @yaks/phone's provider boundary, and a
|
|
584
|
+
* person's own number: their sign-in email and API password, kept in their
|
|
585
|
+
* connection as `username` and `password`. */
|
|
586
|
+
export let provider: Provider = {
|
|
587
|
+
needs: ['key', 'user'],
|
|
588
|
+
sender: opened,
|
|
589
|
+
numbers: (options) => {
|
|
590
|
+
if (!options.key || !options.user) {
|
|
591
|
+
throw new Error('voipms numbers need `user` and `key`')
|
|
592
|
+
}
|
|
593
|
+
return numbers({ ...options, key: options.key, user: options.user })
|
|
594
|
+
},
|
|
595
|
+
own: {
|
|
596
|
+
fields: { user: 'username', key: 'password' },
|
|
597
|
+
account: connected,
|
|
598
|
+
callback: VOIPMS.callback,
|
|
599
|
+
called,
|
|
600
|
+
said: {
|
|
601
|
+
unopened: 'Enter your voip.ms sign-in email and its API password.',
|
|
602
|
+
untexting:
|
|
603
|
+
'No number on this voip.ms account has SMS turned on. Turn it on under DID Numbers, then Manage DIDs, and save the connection again.',
|
|
604
|
+
},
|
|
605
|
+
},
|
|
606
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaks/voipms",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.36",
|
|
4
|
+
"homepage": "https://jsr.io/@yaks/voipms",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./_dist/mod.d.ts",
|
|
9
|
+
"default": "./mod.js"
|
|
10
|
+
},
|
|
11
|
+
"./graph": {
|
|
12
|
+
"types": "./_dist/graph.d.ts",
|
|
13
|
+
"default": "./graph.js"
|
|
14
|
+
},
|
|
15
|
+
"./routes": {
|
|
16
|
+
"types": "./_dist/routes.d.ts",
|
|
17
|
+
"default": "./routes.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"license": "Apache-2.0",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@yaks/fp": "0.2.36",
|
|
23
|
+
"@yaks/host": "0.2.36",
|
|
24
|
+
"@yaks/phone": "0.2.36"
|
|
25
|
+
},
|
|
26
|
+
"description": "voip.ms texting transport, account numbers and inbound callbacks for @yaks/phone.",
|
|
27
|
+
"repository": "https://github.com/yak-sh/yak.git"
|
|
28
|
+
}
|