NetSuite Twilio Outbound - Inbound Text Message Service Integration

64 Views Asked by At

I have created a script to send Netsuite data to my phone Using Twilio SMS Service API's and this is an Outbound Message. Now, I want to receive the data in Netsuite once I send a reply and for that I have developed a restlet with POST request and configured the custom Webhook URL to be my Reslet's Public URL in Twilio and When I trigger a message Using API Explorer(because Indian Number has restrictions and hence, cannot send reply from phone) and if I see error logs in Twilio and it shows :-

error code: INVALID_LOGIN_ATTEMPT and error message: Invalid login attempt

Twilio Debugger 1

Twilio Debugger 2

*My restlet code looks like this :- /**
 * @NApiVersion 2.x
 * @NScriptType Restlet
 * @NModuleScope SameAccount
 */
define(['N/record', 'N/crypto', 'N/log', 'N/encode'], function (record, crypto, log, encode) {
    /**
     * Function to handle incoming Twilio SMS messages.
     * @param {Object} context - The RESTlet request context.
     */
    function post(context) {
       
        var twilioSignature = context.request.headers['X-Twilio-Signature'];
        
        var twilioAuthToken = 'my token Auth';
        
        var url = 'https://ns-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=2194&deploy=1';
        
        var params = context.twilioData; 
      
        var isValidSignature = client.validateRequest(twilioApiSecret, twilioSignature, url, params);
        if (!isValidSignature) {
            log.error('Twilio request signature does not match. Request may be forged.');
            return {
                success: false,
                message: 'Invalid request signature'
            };
        }
       
        var receivedMessage = context.twilioData.Body;
        log.debug('Received SMS Message:', receivedMessage);
       
        var twimlResponse = '<?xml version="1.0" encoding="UTF-8" ?>';
        twimlResponse += '<Response>';
        
        twimlResponse += '<Message>Thank you for your message. We received: ' + receivedMessage + '</Message>';
        
        twimlResponse += '</Response>';
     
        context.response.contentType = 'text/xml';
       
        context.response.write(twimlResponse);
        
        var customRecord = record.create({
            type: 'customrecord_hs_eta_custom_msg'
        });

        customRecord.setText({
            fieldId: 'custrecord_hs_ns_twilio_sms_msg',
            text: receivedMessage
        });

        var customRecordId = customRecord.save();
        
log.debug("Custom Record ID", customRecordId);
        return {
            success: true,
            message: 'Request signature validated. Custom ETA record with ID - ' + customRecordId + ' created into NETSUITE',
        };
    }

    return {
        post: post
    };
});*

I want to see logs in Netsuite and custom record created in my Netsuite instance.

Please suggest any changes to the code and how can I test my restlet with any better alternative method.

Thanks

0

There are 0 best solutions below