2

我已经按照博客文章Twilio WhatsApp API and Flex in Minutes中的描述设置了项目。

我按照说明操作,即使在两个不同的帐户上重复该过程两次之后,当我(通过沙箱)发送消息时,也会发生以下错误:

cvc-complex-type.2.4.a:从元素“入队”开始发现无效内容。需要“{Sms, Message, Redirect}”之一。

该任务不会显示在 Flex 中。

我尝试检查 WhatsApp 发送的内容与 SMS 到达时发送的内容。trigger.message这是SMS的一个简单函数所说的:

{
    EventType=onMessageSent,
    InstanceSid=IS5aa457db5a2d44049c95f2d0b2699f56,
    Attributes={\"proxied\":true}, 
    DateCreated=2019-01-16T12:37:43.664Z, 
    Index=3, 
    From=sms_g13aggbhlwpgk7tbah3llj2kyjpsvvju, 
    MessageSid=IM9123e909eeda41ac92890201a6c3f1b4, 
    Source=API, 
    AccountSid=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX, 
    ChannelSid=CHbc79143368db4680a66c6c504103619f, 
    RetryCount=0, 
    ClientIdentity=sms_g13aggbhlwpgk7tbah3llj2kyjpsvvju, 
    WebhookType=studio, 
    To=CHbc79143368db4680a66c6c504103619f, 
    Body=Test Message, 
    ChannelAttributes={
        status=ACTIVE, 
        forwarding=true, 
        serviceNumber=sms_g13aggbhlwpgk7tbah3llj2kyjpsvvju, 
        twilioNumber=+1228xxxxxxx, 
        from=+44798xxxxxxx, 
        channel_type=sms, 
        proxySession=KC1be96a19ed42aca39f9b6a9f06c26997
    }, 
    WebhookSid=WHcfc83a43a7c2424693fd0053b8b00a01
}

这是 WhatsApp 消息显示的内容:

{
    ApiVersion=2010-04-01, 
    SmsSid=SMb44cfdb4854c41e9ee255032a5449199, 
    SmsStatus=received, 
    SmsMessageSid=SMb44cfdb4854c41e9ee255032a5449199, 
    NumSegments=1, 
    From=whatsapp:+44798xxxxxxx, 
    To=whatsapp:+1228xxxxxxx, 
    MessageSid=SMb44cfdb4854c41e9ee255032a5449199, 
    Body=Test Message, 
    AccountSid=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX, 
    NumMedia=0
}

如您所见,ChannelAttributesChannelSid不见了,这就是 Flex 使用的。

然后我尝试用一​​个函数来转换响应:

exports.handler = function(context, event, callback) {    
    let response;
    if (!event.message.ChannelAttributes) {
        response = {
            name: event.message.From,
            channelType: 'sms',
            channelSid: 'CHbc79143368db4680a66c6c504103619f'
        };
    }
    else {
        response = {
            name: "WhatsApp_" + event.message.ChannelAttributes.from,
            channelType: event.message.ChannelAttributes.channel_type,
            channelSid: event.message.ChannelSid
        };
    }

    const twilioResponse = new Twilio.Response();
    twilioResponse.setStatusCode(200);
    twilioResponse.appendHeader('Content-Type', 'application/json');
    twilioResponse.setBody(response);

    callback(null, twilioResponse);
};

并在 Studio 中分配属性,例如:

{
    "name":"{{widgets.MyFunction.body.name}}",
    "channelType":"{{widgets.MyFunction.body.channelType}}",
    "channelSid":"{{widgets.MyFunction.body.channelSid}}"
}

不幸的是,这显示了相同的错误。

4

0 回答 0