Created
July 20, 2023 17:30
-
-
Save guilhermesalviano/249657d7fc233f45186b582d25339763 to your computer and use it in GitHub Desktop.
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
<script runat="server"> | |
Platform.Load("Core","1.1.1"); | |
var authEndpoint = 'https://xxxxxxxxxxxxxxxxx.auth.marketingcloudapis.com'; | |
var grantType = 'client_credentials'; | |
var clientId = 'xxxxxxxxxxxxxxx'; | |
var clientSecret = 'xxxxxxxxxxxxxxxx'; | |
var url = authEndpoint + '/v2/token'; | |
var contentType = 'application/json'; | |
try { | |
var authenticatedResponse = requestSFMCToken(url, contentType, grantType, clientId, clientSecret); | |
var accessToken = 'Bearer ' + authenticatedResponse.access_token; | |
var restInstanceUrl = authenticatedResponse.rest_instance_url; | |
var url = restInstanceUrl + 'push/v1/message'; | |
// create push | |
var appId = 'xxxxxxxxx-23ijko124'; | |
var appName = 'App IOS'; | |
var pushName = 'push_nome'; | |
var pushTitle = 'título'; | |
var pushContent = 'conteúdo'; | |
var pushLink = ''; | |
var pushSendDate = '2023-07-20T19:10:00Z'; | |
var pushScheduleStatus = '2'; //1 = draft - 2 = active | |
var deExternalKey = ''; | |
var url = restInstanceUrl + 'push/v1/message'; | |
var result = createPushNotification(url, contentType, accessToken, pushName, appId, appName, pushTitle, pushContent, pushScheduleStatus, pushLink, pushSendDate, deExternalKey); | |
} catch (error) { | |
// log error in Data extension | |
Write(Stringify(error)); | |
} | |
function requestSFMCToken(url, contentType, grantType, clientId, clientSecret) { | |
var authResponse = HTTP.Post(url, contentType, Stringify({ | |
grant_type: grantType, | |
client_id: clientId, | |
client_secret: clientSecret | |
})); | |
return Platform.Function.ParseJSON(authResponse.Response[0]); | |
} | |
function createPushNotification(url, contentType, accessToken, pushName, appId, appName, pushTitle, pushContent, pushScheduleStatus, pushLink, pushSendDate, deExternalKey) { | |
var payload = { | |
messageType: "1", // outbound | |
contentType: "1", // alert | |
name: pushName, | |
application: { | |
id: appId, | |
name: appName | |
}, | |
title: pushTitle, | |
alert: pushContent, | |
status: pushScheduleStatus, | |
sendInitiator: "0", //0 = ui - 2 = automation | |
openDirect: pushLink, | |
startDate: pushSendDate, | |
inclusionDataExtensions: [{ | |
id: deExternalKey | |
}] | |
}; | |
return HTTP.Post(url, contentType, Stringify(payload), ['Authorization'], [accessToken]); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apenas um teste de criação de Pushs via Automation Studio - Script.