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

Trigger every time an SMS is sent to the phone number set.

Created by hugo697 438 days ago Viewed 9272 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, phoneNumber: string) {
10
  const client = new Twilio(auth.accountSid, auth.token);
11

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

14
  const messages = await client.messages.list({
15
    to: phoneNumber,
16
    limit: 1000,
17
  });
18

19
  const newMessages = lastCheckedTime
20
    ? messages.filter((message) => message.dateCreated.getTime() > lastCheckedTime)
21
    : messages;
22

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

27
  return newMessages;
28
}