1
New Phone Number Trigger
One script reply has been approved by the moderators Verified

Trigger when you add a new phone number to your account

Created by hugo697 438 days ago Viewed 9108 times
0
Submitted by hugo697 Bun
Verified 438 days ago
1
import { Twilio } from "twilio";
2
import { getState, setState } from "windmill-client@1";
3

4
type Twilio = {
5
  accountSid: string;
6
  token: string;
7
};
8

9
export async function main(auth: Twilio) {
10
  const client = new Twilio(auth.accountSid, auth.token);
11

12
  const lastCheckedTime: number | null = await getState();
13

14
  const incomingPhoneNumbers = await client.incomingPhoneNumbers.list();
15

16
  const newPhoneNumbers = lastCheckedTime
17
    ? incomingPhoneNumbers.filter(
18
        (phoneNumber) => phoneNumber.dateCreated.getTime() > lastCheckedTime
19
      )
20
    : incomingPhoneNumbers;
21

22
  if (newPhoneNumbers.length > 0) {
23
    await setState(newPhoneNumbers[0].dateCreated.getTime());
24
  }
25

26
  return newPhoneNumbers;
27
}
28