Genesys API AGENTLES for WhatsApp message

Hello everyone,

I have maked this code in APEX on visual Studio, to send a WhatsApp message using this API: https://api.mypurecloud.de/api/v2/conversations/messages/agentless.

I create the Template On the WhatsApp integration with Meta.
I got the Id that i used to configure a Template In Genesys.

Using All that Informations i was able to create a code:

String client_id = 'XXXXXXXXXX';
String client_secret ='XXXXXXXXX';
String smsBody = thisAutoSms;
String accessToken = WSE012_SendWhatsAppMessageGenesys.getAccessToken(client_id, client_secret);

        Http http = new Http();
        
        HttpRequest request = new HttpRequest();
        HttpResponse response = new HttpResponse();
        request.setEndpoint('https://api.mypurecloud.de/api/v2/conversations/messages/agentless');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json');
        request.setHeader('Authorization', 'Bearer ' +accessToken);

        System.debug('jsonRep:' +accessToken);
        String jsonReq = '';
        String memberPhone = SendindPhoneNumber;

        Map<String, Object> parameters = new Map<String, Object> {
            'id' => '762c3e5d-a672-429c-ac3b-5e9d371cad3e',
            'value' => ''
        };

        Map<String, Object> messagingTemplate = new Map<String, Object> {
            'responseId' => '762c3e5d-a672-429c-ac3b-5e9d371cad3e',
            'parameters' => new List<Map<String, Object>>{ parameters }
        };


        Map<String, Object> mapData = new Map<String, Object> {
            
            'fromAddress' => '714175940796044', // (string required) The messaging address of the sender of the message. For an SMS messenger type, this must be a currently provisioned SMS phone number. For a WhatsApp messenger type use the provisioned WhatsApp integration’s ID.
            'toAddress' => '33XXXXXXXX', //  E.g. +13175555555 or +34234234234
            'toAddressMessengerType' => 'whatsapp', // sms, whatsapp, open
             // SMS - 765 characters, other channels - 2000 characters
            'messagingTemplate ' => messagingTemplate, // (MessagingTemplateRequest) The messaging template to use in the case of WhatsApp messenger type
            'useExistingActiveConversation' => TechUseExisting  // A rajouter dans les champs d'un objet pour la surveillance du statut nouvelle conversation ou pas !
              
          };

          System.debug('mapData:' +mapData);

          String mapString = (String) JSON.serialize(mapData);
          jsonReq = mapString;
          System.debug('jsonRep:' +jsonReq);

          request.setBody(jsonReq);

        if (Test.isRunningTest() && (mock!=null)) {
            response = mock.respond(request);
        } else {
            response = http.send(request);
        }


          if (response.getStatusCode() == 202) {

            String responseMessage = response.getBody();
            System.debug('SMS sent successfully: ' + responseMessage);
        } else {

            String errorMessage = 'SMS sending failed with status code: ' + response.getStatusCode();
            if (response.getBody() != null) {
                errorMessage += ', Error Message: ' + response.getBody();
            }
            System.debug(errorMessage);

        }

I'm receiving this error when trying to send a message using my Developper Console:

SMS sending failed with status code: 500, Error Message: {"message":"Internal server error. Type: java.lang.NullPointerException, Message: Cannot invoke "com.inin.contracts.ratatoskr.SendMessagingTemplate.getResponseId()" because the return value of "com.inin.contracts.ratatoskr.SendAgentlessOutboundMessage.getMessagingTemplate()" is null","code":"internal.server.error","status":500,"messageWithParams":"Internal server error. Type: {type}, Message: {message}","messageParams":{"type":"java.lang.NullPointerException","message":"Cannot invoke "com.inin.contracts.ratatoskr.SendMessagingTemplate.getResponseId()" because the return value of "com.inin.contracts.ratatoskr.SendAgentlessOutboundMessage.getMessagingTemplate()" is null"},"contextId":"6055b7d3-70dd-4b55-be48-9ba04905e59d","details":[],"errors":[]}

I have being doing many researchs and not founding nothing that can Help me.

Can you please take a look on this ?

Thank !

Hi @BemoreThony I see a couple of issues here, I would suggest looking at the documentation on Agentless using the developer console API Central just to make sure you get a clear understanding of the api.
Your 'fromAddress' => '714175940796044' there is wrong. it needs to be your Whatsapp IntegrationId (UUID).
For your body, you are going to want it to be something like this in JSON

{"fromAddress":"960d8461-e64c-460f-a54b-bf57770b37bb","toAddress":"33XXXXXXXX","toAddressMessengerType":"whatsapp","messagingTemplate":{"responseId":"762c3e5d-a672-429c-ac3b-5e9d371cad3e","parameters":[]},"useExistingActiveConversation":false}

The current issue looks like a problem with your JSON and the responseId. You might diff your JSON with what i have above and make sure you haven't malformed it in some way.

@BemoreThony
Please regenerate your client secret IMMEDIATELY and check your org for unauthorized access. I edited your credentials (client_id and client_secret) out of your post, but they were exposed to the open internet for some time. Your client secret must be handled like a password.

Hi @Greg_Boston thank you for your response, i have a good Whatsapp IntegrationId (UUID) and i am implementing with it. I just decided not to give you the clear one on the open internet.

But you where right about the JSON especially the "Parmeters" that take a List of objects. I make Changes to this JSON format and it worked good.

        Map<String, Object> parameters = new Map<String, Object> {
            'id' => '71417594XXXXXXX',
            'value' => 'test_model'
        };
        
        List<Map<String, Object>> parametersList = new List<Map<String, Object>>{ parameters };
        
        Map<String, Object> messagingTemplate = new Map<String, Object> {
            'responseId' => '762c3e5d-a672-429c-XXXX-XXXXXXXXXXXX',
            'parameters' => parametersList
        };
        
        Map<String, Object> mapData = new Map<String, Object> {
            'fromAddress' => '1022d8b3-7101-XXXX-b3a3-XXXXXXXXXXX',
            'toAddress' => '33XXXXXXXXXX',
            'toAddressMessengerType' => 'whatsapp',
            'messagingTemplate' => messagingTemplate,
            'useExistingActiveConversation' => false
        };

I'm now managing to have a clear responses templates.

Thanks again !

Hi @Jerome.Saint-Marc Thank you for editing it, but what you saw were not the right one. I modified and reduced them before, so don't worry i didn't have any issues form that.

Thanks !