Skip to content

Instantly share code, notes, and snippets.

@ChrisMarxDev
Created March 6, 2024 09:09
Show Gist options
  • Save ChrisMarxDev/92ab6e4650cd3157d4b85b906a88774f to your computer and use it in GitHub Desktop.
Save ChrisMarxDev/92ab6e4650cd3157d4b85b906a88774f to your computer and use it in GitHub Desktop.
Apps script gmail to crm integration
function loadAddOn(event) {
var accessToken = event.gmail.accessToken;
var messageId = event.gmail.messageId;
GmailApp.setCurrentMessageAccessToken(accessToken);
var mailMessage = GmailApp.getMessageById(messageId);
var from = mailMessage.getFrom()
var mail = from.split("<")[1].split(">")[0];
var name = from.split("<")[0];
var sendDataButton = CardService.newTextButton()
.setText("Create new contact")
.setOnClickAction(CardService.newAction().setFunctionName("sendDataToEndpoint")
.setParameters({}));
var senderNameInput = CardService.newTextInput()
.setFieldName("senderMailField")
.setTitle("New contacts mail")
.setValue(mail)
.setHint("Add the sender's mail");
var senderMailInput = CardService.newTextInput()
.setFieldName("senderNameField")
.setTitle("New contacts name")
.setValue(name)
.setHint("Add the sender's name");
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle("Create Pipedrive lead"))
.addSection(CardService.newCardSection()
.addWidget(CardService.newTextParagraph().setText("Calling this function will automatically create a new contact and a new lead for the sender of this mail"))
.addWidget(CardService.newTextParagraph().setText("The email is from: " + name))
.addWidget(CardService.newTextParagraph().setText("The address is: " + mail))
.addWidget(senderNameInput)
.addWidget(senderMailInput)
.addWidget(sendDataButton)) // Add the send data button to your card
.build();
return [card];
}
function sendDataToEndpoint(e) {
var name = e.formInput.senderNameField;
var mail = e.formInput.senderMailField; // Use the field name set with setFieldName
var options = {
method: "post",
contentType: "application/json",
muteHttpExceptions: true
};
// change to your endpoint
var endpoint = "$MYENDPOINT$/create_contact?email=" + mail + "&name=" + name;
UrlFetchApp.fetch(endpoint, options);
return CardService.newActionResponseBuilder()
.setNotification(CardService.newNotification()
.setText("Data sent successfully!"))
.build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment