Webhook for calendly
@RestResource(urlMapping='/calendlymeeting/*')
global with sharing class CalendlyWebhook {
@HttpPost
global static void handleWebhookData() {
System.debug('line 5');
// Get the incoming request body (Calendly data)
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
// Convert the request body to a string
String requestBody = req.requestBody.toString();
// Log the raw incoming JSON to the debug logs
System.debug('Incoming Calendly Webhook Data: ' + requestBody);
// Deserialize the JSON data to a Map (This is just an example; structure
may vary)
Map<String, Object> calendlyData = (Map<String, Object>)
JSON.deserializeUntyped(requestBody);
System.debug('calendlyData: ' + calendlyData);
// Extract key fields (you may need to adjust these based on actual data
from Calendly)
String inviteeName = (String) calendlyData.get('invitee_name');
String inviteeEmail = (String) calendlyData.get('invitee_email');
String eventType = (String) calendlyData.get('event_type');
String eventTime = (String) calendlyData.get('event_time');
// Log the extracted fields to debug
System.debug('Invitee Name: ' + inviteeName);
System.debug('Invitee Email: ' + inviteeEmail);
System.debug('Event Type: ' + eventType);
System.debug('Event Time: ' + eventTime);
// Here, you can create or update a Salesforce object, such as Lead or
Contact, if necessary
// For example, let's create a new Lead:
// Respond to Calendly with a success message
res.statusCode = 200;
//res.responseBody = 'Successfully processed Calendly data and created
Lead.';
// Log the final response (if needed)
System.debug('Response sent to Calendly: ' + res.responseBody);
}
@HttpGet
global static void handleWebhookDataGet() {
System.debug('line 46');
// Get the incoming request body (Calendly data)
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
// Convert the request body to a string
String requestBody = req.requestBody.toString();
// Log the raw incoming JSON to the debug logs
System.debug('Incoming Calendly Webhook Data: ' + requestBody);
// Deserialize the JSON data to a Map (This is just an example; structure
may vary)
Map<String, Object> calendlyData = (Map<String, Object>)
JSON.deserializeUntyped(requestBody);
System.debug('calendlyData: ' + calendlyData);
// Extract key fields (you may need to adjust these based on actual data
from Calendly)
String inviteeName = (String) calendlyData.get('invitee_name');
String inviteeEmail = (String) calendlyData.get('invitee_email');
String eventType = (String) calendlyData.get('event_type');
String eventTime = (String) calendlyData.get('event_time');
// Log the extracted fields to debug
System.debug('Invitee Name: ' + inviteeName);
System.debug('Invitee Email: ' + inviteeEmail);
System.debug('Event Type: ' + eventType);
System.debug('Event Time: ' + eventTime);
// Here, you can create or update a Salesforce object, such as Lead or
Contact, if necessary
// For example, let's create a new Lead:
// Respond to Calendly with a success message
res.statusCode = 200;
//res.responseBody = 'Successfully processed Calendly data and created
Lead.';
// Log the final response (if needed)
System.debug('Response sent to Calendly: ' + res.responseBody);
}
}
BatchClass for Calendly
public class CalendlyMeetingBatchClass implements Database.batchable<sObject>,
Database.AllowsCallouts {
private static final String API_KEY =
'eyJraWQiOiIxY2UxZTEzNjE3ZGNmNzY2YjNjZWJjY2Y4ZGM1YmFmYThhNjVlNjg0MDIzZjdjMzJiZTgzND
liMjM4MDEzNWI0IiwidHlwIjoiUEFUIiwiYWxnIjoiRVMyNTYifQ.eyJpc3MiOiJodHRwczovL2F1dGguY2
FsZW5kbHkuY29tIiwiaWF0IjoxNzI1MjkyNTgyLCJqdGkiOiI4NjhmMDk1YS00M2UxLTRjYTUtOWQ4YS05N
GI4YTFhMjM1MzMiLCJ1c2VyX3V1aWQiOiJFQ0ZERFJCUTRCREJKWFY1In0.oZEZbrlH6S-
i8E2r4dJ7PX31fSXMpWJ5h6kkI7bM1rHPsdWVNrbf9OO7Ij5QXFOtz8AJJ_uX2evjf4mITsmsIg';
public Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator([SELECT Id, Email FROM Contact]);
}
public void execute(Database.BatchableContext BC, List<Contact> scope){
String currentOrganisation = getOrganizationDetails();
System.debug('currentOrganisation==>'+currentOrganisation);
String result;
List<Contact> contactsToUpdate = new List<Contact>();
for (Contact con : scope) {
result = '';
if(con.Email != null && currentOrganisation != null){
result = getIsMeetingScheduled(con.Email, currentOrganisation);
if(result == 'Event is scheduled'){
con.X1st_call_scheduled__c = true;
}
else{
con.X1st_call_scheduled__c = false;
}
}
contactsToUpdate.add(con);
}
System.debug('contactsToUpdate ==> '+contactsToUpdate);
if (!contactsToUpdate.isEmpty()) {
update contactsToUpdate;
}
}
public void finish(Database.BatchableContext BC){
System.debug('Batch completed');
}
public static String getOrganizationDetails(){
HttpRequest req = new HttpRequest();
String endUrl = 'https://api.calendly.com/users/me';
req.setEndpoint(endUrl);
req.setMethod('GET');
req.setHeader('Accept', 'application/json');
req.setHeader('Accept-Charset', 'utf-8');
req.setHeader('Authorization', 'Bearer ' + API_KEY);
Http http = new Http();
HTTPResponse res = http.send(req);
if (res.getStatusCode() == 200) {
Map<String, Object> responseMap = (Map<String, Object>)
JSON.deserializeUntyped(res.getBody());
Map<String, Object> resourceMap = (Map<String, Object>)
responseMap.get('resource');
String currentOrganization = (String)
resourceMap.get('current_organization');
if(currentOrganization != null && currentOrganization != ''){
return currentOrganization;
}
else{
return null;
}
} else {
System.debug('HTTP Response Status: ' + res.getStatusCode());
System.debug('HTTP Response Body: ' + res.getBody());
}
return null;
}
public static String getIsMeetingScheduled(String contactEmail, String
currentOrganisation){
String result = '';
HttpRequest req = new HttpRequest();
String endUrl = 'https://api.calendly.com/scheduled_events?
count=100&invitee_email='+contactEmail+'&organization='+currentOrganisation;
System.debug('endUrl==>'+endUrl);
req.setEndpoint(endUrl);
req.setMethod('GET');
req.setHeader('Accept', 'application/json');
req.setHeader('Accept-Charset', 'utf-8');
req.setHeader('Authorization', 'Bearer ' + API_KEY);
Http http = new Http();
HTTPResponse res = http.send(req);
if (res.getStatusCode() == 200) {
Map<String, Object> responseMap = (Map<String, Object>)
JSON.deserializeUntyped(res.getBody());
System.debug('responseMap==>'+responseMap);
List<Map<String, Object>> collection = (List<Map<String, Object>>)
responseMap.get('collection');
String uri = (String) collection[0].get('uri');
String eventId = uri.split('/')[4];
System.debug('Event ID: ' + eventId);
} else {
System.debug('HTTP Response Status: ' + res.getStatusCode());
System.debug('HTTP Response Body: ' + res.getBody());
}
return result;
}
/*public static String getIsMeetingScheduled(String contactEmail, String
currentOrganisation){
String result = '';
HttpRequest req = new HttpRequest();
String endUrl = 'https://api.calendly.com/scheduled_events?
count=100&invitee_email='+contactEmail+'&organization='+currentOrganisation;
System.debug('endUrl==>'+endUrl);
req.setEndpoint(endUrl);
req.setMethod('GET');
req.setHeader('Accept', 'application/json');
req.setHeader('Accept-Charset', 'utf-8');
req.setHeader('Authorization', 'Bearer ' + API_KEY);
Http http = new Http();
HTTPResponse res = http.send(req);
if (res.getStatusCode() == 200) {
Map<String, Object> responseMap = (Map<String, Object>)
JSON.deserializeUntyped(res.getBody());
System.debug('responseMap==>'+responseMap);
List<Object> events = (List<Object>) responseMap.get('collection');
if(!events.isEmpty()){
for (Object eventObj : events) {
Map<String, Object> event = (Map<String, Object>) eventObj;
String name = (String) event.get('name');
System.debug('name==>'+name);
if(name == 'Boomin to the Bank' || name == 'Boomin Marketing'
|| name == 'Ready to Boom - Chattanooga'){
result = 'Event is scheduled';
}
else{
result = 'Event is not scheduled';
}
}
}
else{
result = 'Event is not scheduled';
}
} else {
System.debug('HTTP Response Status: ' + res.getStatusCode());
System.debug('HTTP Response Body: ' + res.getBody());
}
return result;
}*/
}