AdToBrevo/modules/ApiService.ps1
Administration 8699e89d9e Production
2025-12-08 11:01:09 +01:00

183 lines
5.6 KiB
PowerShell

function GetContactsFromListBrevo {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$IdListe
)
if (-not $Global:ApiKey) {
return [PSCustomObject]@{ Success = $false; Value = "La clé API n'est pas définie" }
}
$headers = @{
"accept" = "application/json"
"api-key" = $Global:ApiKey
}
try {
$response = Invoke-WebRequest `
-Uri "https://api.brevo.com/v3/contacts/lists/$($IdListe)/contacts?limit=50&offset=0&sort=desc" `
-Method GET `
-Headers $headers
$data = $response.Content | ConvertFrom-Json
return [PSCustomObject]@{
Success = $true
Value = $data
}
}
catch {
return [PSCustomObject]@{
Success = $false
Value = "Erreur lors de la récupération des contacts de la liste $IdListe : $($_.Exception.Message)"
}
}
}
function AddContactToBrevo{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$Email
)
if (-not $Global:ApiKey) {
return [PSCustomObject]@{ Success = $false; Value = "La clé API n'est pas définie" }
}
$headers = @{
"accept" = "application/json"
"content-type" = "application/json"
"api-key" = $Global:ApiKey
}
$Body = @{
email = $Email
} | ConvertTo-Json -Depth 3
try {
$response = Invoke-WebRequest -Uri 'https://api.brevo.com/v3/contacts' `
-Method POST `
-Headers $headers `
-ContentType 'application/json' `
-Body $Body
return [PSCustomObject]@{ Success = $true; Value = $response.Content }
}
catch {
return [PSCustomObject]@{ Success = $false; Value = "Erreur lors de l'ajout de l'utilisateur $Email : $($_.Exception.Message)" }
}
}
function AddContactBrevoToList{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$Email,
[Parameter(Mandatory=$true)]
[string]$IdListe
)
if (-not $Global:ApiKey) {
return [PSCustomObject]@{ Success = $false; Value = "La clé API n'est pas définie" }
}
$headers = @{
"accept" = "application/json"
"content-type" = "application/json"
"api-key" = $Global:ApiKey
}
$Body = @{ emails = @($Email) } | ConvertTo-Json -Depth 3
try {
$response = Invoke-WebRequest -Uri "https://api.brevo.com/v3/contacts/lists/$IdListe/contacts/add" `
-Method POST `
-Headers $headers `
-ContentType 'application/json' `
-Body $Body
return [PSCustomObject]@{ Success = $true; Value = $response.Content }
}
catch {
return [PSCustomObject]@{ Success = $false; Value = "Erreur lors de l'ajout de l'utilisateur $Email à la liste $IdListe : $($_.Exception.Message)" }
}
}
function RemoveContactFromListBrevo{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$Email,
[Parameter(Mandatory=$true)]
[string]$IdListe
)
if (-not $Global:ApiKey) {
return [PSCustomObject]@{ Success = $false; Value = "La clé API n'est pas définie" }
}
$headers = @{
"accept" = "application/json"
"content-type" = "application/json"
"api-key" = $Global:ApiKey
}
$Body = @{ emails = @($Email) } | ConvertTo-Json -Depth 3
try {
$response = Invoke-WebRequest -Uri "https://api.brevo.com/v3/contacts/lists/$IdListe/contacts/remove" `
-Method POST `
-Headers $headers `
-ContentType 'application/json' `
-Body $Body
return [PSCustomObject]@{ Success = $true; Value = $response.Content }
}
catch {
return [PSCustomObject]@{ Success = $false; Value = "Erreur lors de la suppresion de l'utilisateur $Email à la liste $IdListe : $($_.Exception.Message)" }
}
}
function RemoveContactFromBrevo{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$Email
)
if (-not $Global:ApiKey) {
return [PSCustomObject]@{ Success = $false; Value = "La clé API n'est pas définie" }
}
$headers = @{
"accept" = "application/json"
"content-type" = "application/json"
"api-key" = $Global:ApiKey
}
try {
$response = Invoke-WebRequest -Uri "https://api.brevo.com/v3/contacts/$Email" `
-Method DELETE `
-Headers $headers `
-ContentType 'application/json'
return [PSCustomObject]@{ Success = $true; Value = $response.Content }
}
catch {
return [PSCustomObject]@{ Success = $false; Value = "Erreur lors de la suppresion de l'utilisateur $Email à la liste $IdListe : $($_.Exception.Message)" }
}
}
#$test = AddContactToBrevo -Email "tris.ta@esadhar.fr"
#$test = AddContactBrevoToList -Email "tris.ta@esadhar.fr" -IdListe 9
#$test = RemoveContactFromListBrevo -Email "tris.ta@esadhar.fr" -IdListe 9
#$test = RemoveContactFromBrevo -Email "toto.tata@esadhar.fr"
# if (-not $test.Success)
# {
# Write-Output $test.Value -Level ERROR
# Write-Output "error"
# }
# else{
# Write-Output $test.Value
# Write-Output "ok"
# }