为什么我向 Twilio WhatsApp 沙盒发送消息时收不到回复?

问题描述 投票:0回答:0

我希望能够从我在 Twilio 中注册为已验证来电显示的号码向我的 Twilio WhatsApp 沙盒号码发送 Whatsapp 消息,并得到答案:“Hola, recibimos tu mensaje: [原始消息]”

这是沙盒配置: 收到消息时 https://[我的项目].glitch.me/webhook 方法:POST

状态回调地址 https://[我的项目].glitch.me/webhook 方法:GET

我的 Glitch 项目如下所示: 项目名称:[我的项目]-glitch-app

没有文件夹,只有两个文件:

  • server.js 包含以下内容:
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);
});

  • package.json 包含以下内容:
{
  "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"
  ]
}

这是故障日志显示的内容:

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

真的试了2天了,没发现问题

如果我从我在 Twilio 中注册为已验证呼叫者 ID 的号码发送 Whatsapp 消息到我的 Twilio WhatsApp 沙盒号码,我不会在故障日志中打印任何内容。

但在 Twilio 日志中,我得到以下两个错误日志条目。 第一个:

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

第二个:

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

我还在可编程消息日志中获得了一个条目,其中包含以下详细信息:

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

当我在 Postman 中插入并发送以下行作为 POST 请求

curl -X POST -H "Content-Type: application/json" -d '{"From": "whatsapp:[MY-REGISTERED-NUMBER]", "Body": "Mensaje de prueba"}' https://[my-project].glitch.me/webhook 
我得到这个答案:

<?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>

如果有人能提示问题出在哪里,我将不胜感激。我试图替换个人和帐户信息,但也许我没有完成那个任务,所以我希望这不会造成任何混乱。

postman whatsapp twilio-api glitch-framework
© www.soinside.com 2019 - 2024. All rights reserved.