-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathsendSms.ts
43 lines (35 loc) · 1.06 KB
/
sendSms.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { FunctionEvent } from 'graphcool-lib'
import * as twilio from 'twilio'
interface EventData {
to: string
from: string
body: string
mediaUrl: string | null
}
export default async (event: FunctionEvent<EventData>) => {
if (!process.env['TWILIO_ACCOUNT_SID']) {
console.log('Please provide a valid twilio account sid!')
return { error: 'Module not configured correctly.' }
}
if (!process.env['TWILIO_AUTH_TOKEN']) {
console.log('Please provide a valid twilio auth token!')
return { error: 'Module not configured correctly.' }
}
try {
const accountSid = process.env['TWILIO_ACCOUNT_SID']
const authToken = process.env['TWILIO_AUTH_TOKEN']
const client = twilio(accountSid, authToken)
const { to, from, body, mediaUrl } = event.data
const data = await client.messages
.create({
to: to,
from: from,
body: body,
mediaUrl: mediaUrl
})
return { data: { sid: data.sid } }
} catch(error) {
console.log(error)
return { error: 'Unexpected error occured.' }
}
}