Created
May 18, 2023 07:25
-
-
Save amitastreait/473e79b20c8bd264662db9ee9a57a975 to your computer and use it in GitHub Desktop.
Send template message using WhatsApp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WhatsAppUtils { | |
public static List<WAMessage__c> listAllMessageByCustomer(String customerPhone){ | |
List<WAMessage__c> messages = new List<WAMessage__c>(); | |
messages = [SELECT Id, Name, MessageContent__c, MessageType__c, Outgoing__c FROM WAMessage__c WHERE CustomerPhone__c =: customerPhone]; | |
return messages; | |
} | |
public static WAMessage__c sendTextMessage(String messageContent, String toPhone){ | |
HttpRequest httpReq = new HttpRequest(); | |
httpReq.setEndpoint('https://graph.facebook.com/v13.0/109671681865676/messages'); | |
httpReq.setMethod('POST'); | |
httpReq.setHeader('Content-Type', 'application/json'); | |
httpReq.setHeader('Authorization', 'Bearer '+System.Label.WHATSAPPACCESSTOKEN); | |
String messageBody = '{'+ | |
' "messaging_product": "whatsapp",'+ | |
' "recipient_type": "individual",'+ | |
' "to": "'+toPhone+'",'+ | |
' "type": "text",'+ | |
' "text": {'+ | |
' "preview_url": false,'+ | |
' "body": "'+messageContent+'"'+ | |
' }'+ | |
'}'; | |
httpReq.setBody(messageBody); | |
Http http = new Http(); | |
WAMessage__c salesforceMessage = new WAMessage__c(); | |
try{ | |
HttpResponse response = http.send(httpReq); | |
if( response.getStatusCode() == 200 ){ | |
// Parse & Create Message Record | |
System.debug('Successful!'); | |
WhatsAppUtils responseFromWA = (WhatsAppUtils) JSON.deserialize( response.getBody() , WhatsAppUtils.class); | |
salesforceMessage.MessageContent__c = messageContent; | |
salesforceMessage.CustomerPhone__c = toPhone; | |
salesforceMessage.MessageID__c = responseFromWA.messages.get(0).id; | |
salesforceMessage.MessageType__c = 'text'; | |
salesforceMessage.Outgoing__c = True; | |
salesforceMessage.AgentName__c = UserInfo.getFirstName()+' '+ UserInfo.getLastName(); | |
upsert salesforceMessage MessageID__c; | |
} | |
}catch(System.CalloutException ex){ | |
System.debug(' CalloutException Executed '+ ex.getStackTraceString() ); | |
System.debug(' CalloutException Executed '+ ex.getMessage() ); | |
}catch(System.Exception ex){ | |
System.debug(' System.Exception Executed '+ ex.getStackTraceString() ); | |
} | |
return salesforceMessage; | |
} | |
/// WhatsAppUtils.sendFlightBookingConfirmationMessage('916392302090', 'JOHN', 'USA', 'INDIA'); | |
public static WAMessage__c sendFlightBookingConfirmationMessage(String toPhone, String name, String source, String destination){ | |
String body = '{'+ | |
' "messaging_product": "whatsapp",'+ | |
' "recipient_type": "individual",'+ | |
' "to": "'+toPhone+'",'+ | |
' "type": "template",'+ | |
' "template": {'+ | |
' "name": "flight_booking_confirm_template",'+ | |
' "language": {'+ | |
' "code": "en"'+ | |
' },'+ | |
' "components": ['+ | |
' {'+ | |
' "type": "header",'+ | |
' "parameters": ['+ | |
' {'+ | |
' "type": "image",'+ | |
' "image": {'+ | |
' "link": "https://bit.ly/3AWkh2p"'+ | |
' }'+ | |
' }'+ | |
' ]'+ | |
' },'+ | |
' {'+ | |
' "type": "body",'+ | |
' "parameters": ['+ | |
' {'+ | |
' "type": "text",'+ | |
' "text": "'+Name+'"'+ | |
' },'+ | |
' {'+ | |
' "type": "text",'+ | |
' "text": "'+source+'"'+ | |
' },'+ | |
' {'+ | |
' "type": "text",'+ | |
' "text": "'+destination+'"'+ | |
' },'+ | |
' {'+ | |
' "type": "date_time",'+ | |
' "date_time" : {'+ | |
' "fallback_value": "October 25, 2023 22:34 PM",'+ | |
' "day_of_week": 6,'+ | |
' "day_of_month": 25,'+ | |
' "year": 2023,'+ | |
' "month": 10,'+ | |
' "hour": 12,'+ | |
' "minute": 34'+ | |
' }'+ | |
' },'+ | |
' {'+ | |
' "type": "text",'+ | |
' "text": "999-999-9999"'+ | |
' }'+ | |
' ]'+ | |
' },'+ | |
' {'+ | |
' "type": "button",'+ | |
' "sub_type": "quick_reply",'+ | |
' "index": "0",'+ | |
' "parameters": ['+ | |
' {'+ | |
' "type": "payload",'+ | |
' "payload": "Contact to Support"'+ | |
' }'+ | |
' ]'+ | |
' },'+ | |
' {'+ | |
' "type": "button",'+ | |
' "sub_type": "quick_reply",'+ | |
' "index": "1",'+ | |
' "parameters": ['+ | |
' {'+ | |
' "type": "payload",'+ | |
' "payload": "Check PNR Status"'+ | |
' }'+ | |
' ]'+ | |
' }'+ | |
' ]'+ | |
' }'+ | |
'}'; | |
HttpRequest httpReq = new HttpRequest(); | |
httpReq.setEndpoint('https://graph.facebook.com/v13.0/109671681865676/messages'); | |
httpReq.setMethod('POST'); | |
httpReq.setHeader('Content-Type', 'application/json'); | |
httpReq.setHeader('Authorization', 'Bearer '+System.Label.WHATSAPPACCESSTOKEN); | |
httpReq.setBody(body); | |
Http http = new Http(); | |
WAMessage__c salesforceMessage = new WAMessage__c(); | |
try{ | |
HttpResponse response = http.send(httpReq); | |
if( response.getStatusCode() == 200 ){ | |
// Parse & Create Message Record | |
System.debug('Successful!'); | |
WhatsAppUtils responseFromWA = (WhatsAppUtils) JSON.deserialize( response.getBody() , WhatsAppUtils.class); | |
salesforceMessage.MessageContent__c = body; | |
salesforceMessage.CustomerPhone__c = toPhone; | |
salesforceMessage.MessageID__c = responseFromWA.messages.get(0).id; | |
salesforceMessage.MessageType__c = 'template'; | |
salesforceMessage.Outgoing__c = True; | |
salesforceMessage.AgentName__c = UserInfo.getFirstName()+' '+ UserInfo.getLastName(); | |
upsert salesforceMessage MessageID__c; | |
} | |
}catch(System.CalloutException ex){ | |
System.debug(' CalloutException Executed '+ ex.getStackTraceString() ); | |
System.debug(' CalloutException Executed '+ ex.getMessage() ); | |
}catch(System.Exception ex){ | |
System.debug(' System.Exception Executed '+ ex.getStackTraceString() ); | |
} | |
return salesforceMessage; | |
} | |
public static WAMessage__c sendMediaMessages(String toPhone, String url){ | |
HttpRequest httpReq = new HttpRequest(); | |
httpReq.setEndpoint('https://graph.facebook.com/v13.0/109671681865676/messages'); | |
httpReq.setMethod('POST'); | |
httpReq.setHeader('Content-Type', 'application/json'); | |
httpReq.setHeader('Authorization', 'Bearer '+System.Label.WHATSAPPACCESSTOKEN); | |
if(String.isBlank(url)){ | |
url = 'https://d2o24x000002ypne-dev-ed.file.force.com/sfc/dist/version/download/?oid=00D4x000002ypne&ids=0684x00000QjKRW&d=%2Fa%2F4x000001lW7M%2FhwE9KL_ZuBbdeXEwse5wjZUb3uqtYPYWVLWVX6OdnIc&asPdf=false'; | |
} | |
String body = '{'+ | |
' "messaging_product": "whatsapp",'+ | |
' "recipient_type": "individual",'+ | |
' "to": "'+toPhone+'",'+ | |
' "type": "document",'+ | |
' "document": {'+ | |
' "link": "'+url+'",'+ | |
' "caption" : "Field Service Lightning Topics.pdf",'+ | |
' "filename" : "Field Service Lightning Topics.pdf"'+ | |
' }'+ | |
'}'; | |
httpReq.setBody(body); | |
Http http = new Http(); | |
WAMessage__c salesforceMessage = new WAMessage__c(); | |
try{ | |
HttpResponse response = http.send(httpReq); | |
if( response.getStatusCode() == 200 ){ | |
// Parse & Create Message Record | |
System.debug('Successful!'); | |
WhatsAppUtils responseFromWA = (WhatsAppUtils) JSON.deserialize( response.getBody() , WhatsAppUtils.class); | |
salesforceMessage.MessageContent__c = body; | |
salesforceMessage.CustomerPhone__c = toPhone; | |
salesforceMessage.MessageID__c = responseFromWA.messages.get(0).id; | |
salesforceMessage.MessageType__c = 'media'; | |
salesforceMessage.Outgoing__c = True; | |
salesforceMessage.AgentName__c = UserInfo.getFirstName()+' '+ UserInfo.getLastName(); | |
upsert salesforceMessage MessageID__c; | |
} | |
}catch(System.CalloutException ex){ | |
System.debug(' CalloutException Executed '+ ex.getStackTraceString() ); | |
System.debug(' CalloutException Executed '+ ex.getMessage() ); | |
}catch(System.Exception ex){ | |
System.debug(' System.Exception Executed '+ ex.getStackTraceString() ); | |
} | |
return salesforceMessage; | |
} | |
public String messaging_product; | |
public contacts[] contacts; | |
public messages[] messages; | |
public class contacts { | |
public String input; | |
public String wa_id; | |
} | |
public class messages { | |
public String id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment