0
New Call Trigger
One script reply has been approved by the moderators Verified

Trigger each time a call to the phone number is completed.

Created by hugo697 421 days ago Viewed 8993 times
0
Submitted by hugo697 Bun
Verified 421 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, phoneNumber: string) {
10
  const client = new Twilio(auth.accountSid, auth.token);
11

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

14
  const calls = await client.calls.list({
15
    to: phoneNumber,
16
    status: "completed",
17
    limit: 1000,
18
  });
19

20
  const newCalls = lastCheckedTime
21
    ? calls.filter((call) => call.dateCreated.getTime() > lastCheckedTime)
22
    : calls;
23

24
  if (newCalls.length > 0) {
25
    await setState(newCalls[0].dateCreated.getTime());
26
  }
27

28
  return newCalls;
29
}
30