BemoreThony | 2024-03-28 17:09:20 UTC | #1
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 clientid = 'XXXXXXXXXX'; String clientsecret ='XXXXXXXXX'; String smsBody = thisAutoSms; String accessToken = WSE012SendWhatsAppMessageGenesys.getAccessToken(clientid, 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 !
Greg_Boston | 2024-03-28 16:49:36 UTC | #2
Hi @BemoreThony I see a couple of issues here, I would suggest looking at the documentation on Agentless using the developer console https://apicentral.genesys.cloud/api-explorer-standalone#post-api-v2-conversations-messages-agentless 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.
Jerome.Saint-Marc | 2024-03-28 17:09:58 UTC | #3
@BemoreThony Please regenerate your client secret IMMEDIATELY and check your org for unauthorized access. I edited your credentials (clientid and clientsecret) out of your post, but they were exposed to the open internet for some time. Your client secret must be handled like a password.
BemoreThony | 2024-04-02 08:57:59 UTC | #4
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 !
BemoreThony | 2024-04-02 09:04:12 UTC | #5
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 !
system | 2024-05-02 09:04:51 UTC | #6
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
This post was migrated from the old Developer Forum.
ref: 25466