whatsapptwilio-apiglitch-framework

Why can't I get a response when I send a message to my Twilio WhatsApp Sandbox?


I want to be able to send a Whatsapp-Message from the number I haver registered as Verified Caller ID in Twilio to the number of my Twilio WhatsApp Sandbox, and getting as answer: "Hola, recibimos tu mensaje: [original message]"

This is the Sandbox Configuration: When a message comes in https://[my-project].glitch.me/webhook Method: POST

Status callback URL https://[my-project].glitch.me/webhook Method: GET

My Glitch proyect looks as follows: project name: [my-project]-glitch-app

There are no folders an only two files:

const twilio = require('twilio');
const accountSid = '[ACCOUNTSID]';
const authToken = '[AUTHTOKEN]';
const client = twilio(accountSid, authToken);
const express = require('express');
const bodyParser = require('body-parser');
const app = express().use(bodyParser.json());

app.post('/webhook', (req, res) => {
  const body = req.body;
  console.log('Webhook received', body);

  if (body.SmsStatus) {
    console.log('Status callback received', body);
    res.sendStatus(200);
    return;
  }

  const sender = body.From;
  const message = body.Body;

  if (message) {
    const responseText = `Hola, recibimos tu mensaje: "${message}".`;

    client.messages
      .create({
        body: responseText,
        from: 'whatsapp:[MY-SANDBOX-NUMBER]',
        to: sender
      })
      .then(message => console.log(`Mensaje enviado con éxito: ${message.sid}`))
      .catch(error => console.error(`Error al enviar el mensaje: ${error.message}`));
  }

  res.status(200).send('EVENT_RECEIVED');
});


app.get('/webhook', (req, res) => {
  res.status(200).send('Hello, World!');
});

const listener = app.listen(process.env.PORT, () => {
  console.log('Ihr App läuft auf Port ' + listener.address().port);
});

{
  "name": "[my-project]-glitch-app",
  "version": "0.0.1",
  "description": "Ein einfacher Node.js-App mit Express",
  "main": "src/server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "body-parser": "^1.20.2",
    "express": "^4.18.2",
    "twilio": "^4.11.0"
  },
  "engines": {
    "node": "14.x"
  },
  "repository": {
    "url": "https://glitch.com/edit/#!/[my-project]-glitch-app"
  },
  "license": "MIT",
  "keywords": [
    "node",
    "glitch",
    "express"
  ]
}

This is what the Glitch Log is showing:

node v14.19.1, npm 6.14.16
Installing...
audited 118 packages in 5.643s
9 packages are looking for funding
  run `npm fund` for details
found 0 vulnerabilities
Total install time: 14635ms
Ihr App läuft auf Port 3000

I have really tried for 2 days now, but I don't find the problem.

If I send a Whatsapp-Message from the number I haver registered as Verified Caller ID in Twilio to the number of my Twilio WhatsApp Sandbox I don't get anything printed in the Glitch Log.

But in the Twilio Logs I get the following two Error Log entries. The first one:

URL

https://[my-project].glitch.me/webhook

Product

Programmable SMS

Timestamp

2023-05-07 21:23:17

HTTP status

405

Request
URL
https://[my-project].glitch.me/webhook

Parameters

Show raw data
SmsMessageSid

SMbc96a0cbdd40d8def4f77493e67e55f5

NumMedia

0

ProfileName

[My-Name]

SmsSid

[SmsSid]

WaId

[MY-REGISTERED-NUMBER]

SmsStatus

received

Body

Yo

To

whatsapp:[MY-SANDBOX-NUMBER]

NumSegments

1

ReferralNumMedia

0

MessageSid

[SmsSid]

AccountSid

[ACCOUNTSID]

From

whatsapp:[MY-SANDBOX-NUMBER]

ApiVersion

2010-04-01

Message text

Show raw data
sourceComponent

14100

httpResponse

405

url

https://[my-project].glitch.me/webhook

ErrorCode

11200

LogLevel

ERROR

Msg

Method Not Allowed

EmailNotification

false

Response
Headers
Transfer-Encoding

chunked

server

AmazonS3

X-Twilio-WebhookRetriable

false

X-Twilio-WebhookAttempt

1

x-amz-id-2

K4zdxq35SUjCbOeg/28BsJonIkKyOSqCuFJH3NcxGjDSoAASdBhYYp481uZl/TQ7ddFm0DnOG54=

Connection

close

allow

HEAD, DELETE, GET, PUT

Content-Type

application/xml

x-amz-request-id

8NH0QX62WE2RVKT3

Date

Sun, 07 May 2023 21:23:17 GMT

Body

Show raw data

Copy response body
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>MethodNotAllowed</Code><Message>The specified method is not allowed against this resource.</Message><Method>POST</Meth

The second one:

URL

null

Product

Programmable SMS

Timestamp

2023-05-07 21:23:17

HTTP status

Request
URL
null

Parameters

Show raw data
ErrorUrl

https://[my-project].glitch.me/webhook

SmsMessageSid

[SmsSid]

ErrorCode

11200

NumMedia

0

ProfileName

[My-Name]

SmsSid

[SmsSid]

WaId

[MY-REGISTERED-NUMBER]

SmsStatus

received

Body

Yo

To

whatsapp:[MY-SANDBOX-NUMBER]

NumSegments

1

ReferralNumMedia

0

MessageSid

[SmsSid]

AccountSid

[ACCOUNTSID]

From

whatsapp:[MY-REGISTERED-NUMBER]

ApiVersion

2010-04-01

Message text

Show raw data
sourceComponent

14100

url

null

ErrorCode

11200

LogLevel

ERROR

Msg

The URI scheme, of the URI null, must be equal (ignoring case) to 'http', 'https', 'ws', or 'wss'

EmailNotification

false

Response
Headers
Body

Show raw data

Copy response body

And I also get an entry in the Programmable Messaging Logs with these details:

Message Details
Properties
Message SID
[SmsSid]

Messaging Service
—

Created At
14:23:17 GMT-7 2023-05-07

From
whatsapp:[MY-REGISTERED-NUMBER]

To
whatsapp:[MY-SANDBOX-NUMBER]

Direction
Incoming

API Endpoint
/2010-04-01/Accounts/{AccountSid}/SMS/Messages

Message Segments
1

Encoding
NOT AVAILABLE

Cost
$0

Body
Yo
Delivery Steps
STAGE   STEP    TIMESTAMP   ELAPSED TIME
Twilio Platform Message Created 14:23:17 GMT-7 2023-05-07   —
Enqueued    14:23:17 GMT-7 2023-05-07   —
TwiML Fetch Succeeded:  14:23:17 GMT-7 2023-05-07   0.21 sec
Message Received    14:23:17 GMT-7 2023-05-07   0.21 sec
Errors and Warnings
NOTIFICATION SID
NOa71b48784d0eb1432729edca082b224a
WARNING
 11200 There was a failure attempting to retrieve the contents of this URL.
DESCRIPTION
There was a failure attempting to retrieve the contents of this URL.
NOTIFICATION SID
NO13cb8f31d5f73228d8a09c24ddb882b3
WARNING
 11200 There was a failure attempting to retrieve the contents of this URL.
DESCRIPTION
There was a failure attempting to retrieve the contents of this URL.
Request Inspector
+ Expand All
POST
null
2023-05-07 21:23:17 UTC
Request
URL
null
ParametersShow Raw
ErrorUrl    "https://[my-project].glitch.me/webhook"
SmsMessageSid   "[SmsSid]"
ErrorCode   "11200"
NumMedia    "0"
ProfileName "[My-Name]"
SmsSid  "[SmsSid]"
WaId    "[MY-REGISTERED-NUMBER]"
SmsStatus   "received"
Body    "Yo"
To  "whatsapp:[MY-SANDBOX-NUMBER]"
NumSegments "1"
ReferralNumMedia    "0"
MessageSid  "[SmsSid]"
AccountSid  "[ACCOUNTSID]"
From    "whatsapp:[MY-REGISTERED-NUMBER]"
ApiVersion  "2010-04-01"
Message TextShow Raw
sourceComponent "14100"
url "null"
ErrorCode   "11200"
LogLevel    "ERROR"
Msg "The URI scheme, of the URI null, must be equal (ignoring case) to 'http', 'https', 'ws', or 'wss'"
EmailNotification   "false"
Response
Headers
Body
No body.
POST
https://[my-project].glitch.me/webhook
2023-05-07 21:23:17 UTC405

When I insert and send the following Line as POST request in Postman curl -X POST -H "Content-Type: application/json" -d '{"From": "whatsapp:[MY-REGISTERED-NUMBER]", "Body": "Mensaje de prueba"}' https://[my-project].glitch.me/webhook I get this answer:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>MethodNotAllowed</Code>
    <Message>The specified method is not allowed against this resource.</Message>
    <Method>POST</Method>
    <ResourceType>OBJECT</ResourceType>
    <RequestId>2H4KVCP2E55EXADJ</RequestId>
    <HostId>tysx610iiZW3fIpOHkC2+yOv7ZnPzHfc/ykr01bK3mUuaDfABe1FQBM/OdNLlL6Ne83AWBjnDjk=</HostId>
</Error>

I would really appreciate it if someone might have a hint at where the problem might be. I tried to replace personal and account information, but maybe I wasn't complete a that task, so I hope that doesn't create any confusion. This is the first time I try to write a bot, and the end objective is to write a helper bot for taking delivery orders at a friend's restaurant.


Solution

  • I created a new project in Glitch and copied the existing code. And it worked!!! Now the 2 files are in folder src/. So already there was an error in my old package.json. But even if I put the files in src/-folder in my old proyect, it still wont work. So I now have I working solution, but still not quite sure what causes the old Glitch problem to continue failing.