@pipedream/twilio 0.3.3 → 0.3.5

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.
@@ -1,86 +0,0 @@
1
- const twilio = require("../../twilio.app.js");
2
- const MessagingResponse = require("twilio").twiml.MessagingResponse;
3
- const twilioClient = require("twilio");
4
-
5
- module.exports = {
6
- key: "twilio-new-incoming-sms",
7
- name: "New Incoming SMS",
8
- description:
9
- "Configures a webhook in Twilio, tied to an incoming phone number, and emits an event each time an SMS is sent to that number",
10
- version: "0.0.4",
11
- dedupe: "unique",
12
- props: {
13
- twilio,
14
- incomingPhoneNumber: { propDefinition: [twilio, "incomingPhoneNumber"] },
15
- authToken: { propDefinition: [twilio, "authToken"] },
16
- responseMessage: { propDefinition: [twilio, "responseMessage"] },
17
- http: {
18
- type: "$.interface.http",
19
- customResponse: true,
20
- },
21
- },
22
- hooks: {
23
- async activate() {
24
- console.log(
25
- `Creating webhook for phone number ${this.incomingPhoneNumber}`
26
- );
27
- const createWebhookResp = await this.twilio.setIncomingSMSWebhookURL(
28
- this.incomingPhoneNumber,
29
- this.http.endpoint
30
- );
31
- console.log(createWebhookResp);
32
- },
33
- async deactivate() {
34
- console.log(
35
- `Removing webhook from phone number ${this.incomingPhoneNumber}`
36
- );
37
- const deleteWebhookResp = await this.twilio.setIncomingSMSWebhookURL(
38
- this.incomingPhoneNumber,
39
- "" // remove the webhook URL
40
- );
41
- console.log(deleteWebhookResp);
42
- },
43
- },
44
- async run(event) {
45
- const { body, headers } = event;
46
- const twiml = new MessagingResponse();
47
-
48
- // https://support.twilio.com/hc/en-us/articles/223134127-Receive-SMS-and-MMS-Messages-without-Responding
49
- let responseBody = "<Response></Response>";
50
- if (this.responseMessage) {
51
- twiml.message(this.responseMessage);
52
- responseBody = twiml.toString();
53
- }
54
-
55
- this.http.respond({
56
- status: 200,
57
- headers: { "Content-Type": "text/xml" },
58
- body: responseBody,
59
- });
60
-
61
- const twilioSignature = headers["x-twilio-signature"];
62
- if (!twilioSignature) {
63
- console.log("No x-twilio-signature header in request. Exiting.");
64
- return;
65
- }
66
-
67
- // See https://www.twilio.com/docs/usage/webhooks/webhooks-security
68
- if (
69
- !twilioClient.validateRequest(
70
- this.authToken,
71
- twilioSignature,
72
- `${this.http.endpoint}/`, // This must match the incoming URL exactly, which contains a /
73
- body
74
- )
75
- ) {
76
- throw new Error(
77
- "Computed Twilio signature doesn't match signature received in header"
78
- );
79
- }
80
-
81
- this.$emit(body, {
82
- summary: body.Body, // the content of the text message
83
- id: headers["i-twilio-idempotency-token"], // if Twilio retries a message, but we've already emitted, dedupe
84
- });
85
- },
86
- };
package/twilio.app.js DELETED
@@ -1,52 +0,0 @@
1
- const twilioClient = require("twilio");
2
-
3
- module.exports = {
4
- type: "app",
5
- app: "twilio",
6
- propDefinitions: {
7
- authToken: {
8
- type: "string",
9
- secret: true,
10
- label: "Twilio Auth Token",
11
- description:
12
- "Your Twilio auth token, found [in your Twilio console](https://www.twilio.com/console). Required for validating Twilio events.",
13
- },
14
- incomingPhoneNumber: {
15
- type: "string",
16
- label: "Incoming Phone Number",
17
- description:
18
- "The Twilio phone number where you'll receive messages. This source creates a webhook tied to this incoming phone number, **overwriting any existing webhook URL**.",
19
- async options() {
20
- return await this.listIncomingPhoneNumbers();
21
- },
22
- },
23
- responseMessage: {
24
- type: "string",
25
- optional: true,
26
- label: "SMS Response Message",
27
- description:
28
- "The message you want to send in response to incoming messages. Leave this blank if you don't need to issue a response.",
29
- },
30
- },
31
- methods: {
32
- getClient() {
33
- return twilioClient(this.$auth.Sid, this.$auth.Secret, {
34
- accountSid: this.$auth.AccountSid,
35
- });
36
- },
37
- async setIncomingSMSWebhookURL(phoneNumberSid, url) {
38
- const client = this.getClient();
39
- return await client.incomingPhoneNumbers(phoneNumberSid).update({
40
- smsMethod: "POST",
41
- smsUrl: url,
42
- });
43
- },
44
- async listIncomingPhoneNumbers() {
45
- const client = this.getClient();
46
- const numbers = await client.incomingPhoneNumbers.list();
47
- return numbers.map((number) => {
48
- return { label: number.friendlyName, value: number.sid };
49
- });
50
- },
51
- },
52
- };