BentoSdk Utility API
Mix.install([
{:bento_sdk, "~> 0.1.0"}
])
Introduction
This notebook demonstrates how to use the BentoSdk’s Utility API methods in the Bento marketing platform. The Utility API allows you to:
- Moderate content for profanity and inappropriate content
- Guess the gender of a name
- Geolocate an IP address
- Validate email addresses
- Check email addresses against Jesse’s ruleset
- Check domains and IP addresses against blacklists
These utility functions can help enhance your marketing campaigns with additional intelligence.
Configuration
We’ll use Livebook’s secrets feature to securely store and access your Bento credentials:
# Configure BentoSdk with the secrets
BentoSdk.configure(
site_uuid: System.fetch_env!("LB_BENTO_SITE_UUID"),
username: System.fetch_env!("LB_BENTO_USERNAME"),
password: System.fetch_env!("LB_BENTO_PASSWORD")
)
Content Moderation
The content moderation API helps you check user-generated content for profanity and other inappropriate content before using it in your marketing materials.
# Example of moderating content
content = "This is some sample content to check for moderation."
case BentoSdk.Utility.moderate_content(content) do
{:ok, result} ->
result
{:error, reason} ->
"Error: #{reason}"
end
Gender Guessing
The gender guessing API helps you make educated guesses about a person’s gender based on their name.
# Example of guessing gender from a name
name = "Alex"
case BentoSdk.Utility.guess_gender(name) do
{:ok, result} ->
result
{:error, reason} ->
"Error: #{reason}"
end
IP Geolocation
The IP geolocation API helps you determine the geographic location of an IP address.
# Example of geolocating an IP address
ip_address = "8.8.8.8"
case BentoSdk.Utility.geolocate(ip_address) do
{:ok, result} ->
result
{:error, reason} ->
"Error: #{reason}"
end
Email Validation
The email validation API helps you validate email addresses beyond simple format validation.
# Example of validating an email address
email = "test@example.com"
case BentoSdk.Utility.validate_email(email) do
{:ok, result} ->
if result["valid"] do
"Email is valid"
else
"Email is invalid"
end
{:error, reason} ->
"Error: #{reason}"
end
You can also provide additional information to improve validation:
# Example of validating an email with additional information
email = "test@example.com"
opts = [
name: "John Doe",
ip: "8.8.8.8"
]
case BentoSdk.Utility.validate_email(email, opts) do
{:ok, result} ->
if result["valid"] do
"Email is valid"
else
"Email is invalid"
end
{:error, reason} ->
"Error: #{reason}"
end
Jesse’s Ruleset
Jesse’s ruleset is a set of rules for validating email addresses beyond simple format validation.
# Example of checking an email against Jesse's ruleset
email = "test@example.com"
opts = [block_free_providers: false, wiggleroom: true]
case BentoSdk.Utility.jesses_ruleset(email, opts) do
{:ok, reasons} ->
if Enum.empty?(reasons) do
"Email is valid"
else
"Email is invalid: #{Enum.join(reasons, ", ")}"
end
{:error, reason} ->
"Error: #{reason}"
end
Blacklist Checking
The blacklist checking API helps you determine if a domain or IP address is on any blacklists.
# Example of checking a domain against blacklists
params = %{domain: "example.com"}
case BentoSdk.Utility.check_blacklist(params) do
{:ok, result} ->
blacklisted = Enum.any?(Map.values(result["results"]), fn value -> value == true end)
if blacklisted do
"Domain is on at least one blacklist"
else
"Domain is not on any blacklists"
end
{:error, reason} ->
"Error: #{reason}"
end
# Example of checking an IP address against blacklists
params = %{ip_address: "8.8.8.8"}
case BentoSdk.Utility.check_blacklist(params) do
{:ok, result} ->
blacklisted = Enum.any?(Map.values(result["results"]), fn value -> value == true end)
if blacklisted do
"IP address is on at least one blacklist"
else
"IP address is not on any blacklists"
end
{:error, reason} ->
"Error: #{reason}"
end
Example: Moderating User Reviews
Here’s an example of how you might use the content moderation API to check user reviews before publishing them:
# Example of moderating a user review
review = "This product is spam@example.com! I love it so much."
case BentoSdk.Utility.moderate_content(review) do
{:ok, result} ->
if result["valid"] do
# If the review is safe, we can publish it
%{
status: "approved",
review: review,
moderation_result: result
}
else
# If the review contains inappropriate content, we could flag it for manual review
# For this example, we'll just display an error message
%{
status: "error",
message: "Review contains inappropriate content",
reasons: result["reasons"]
}
end
{:error, reason} ->
"Error: #{reason}"
end
Example: Personalized Email with Location
Here’s an example of how you might use the geolocation API to personalize an email:
# Example of geolocating an IP address
ip_address = "8.8.8.8"
case BentoSdk.Utility.geolocate(ip_address) do
{:ok, result} ->
city = result["city_name"] || "your city"
country = result["country_name"] || "your country"
# Create email content
subject = "Special Offers for #{city}, #{country}"
html_body = """
Hello!
We noticed you're from
#{city}, #{country}.
Check out these deals specifically for
#{city}:
- Special Discount 1
"""
# Display the email content
%{
to: "example@example.com",
subject: subject,
html_body: html_body
}
{:error, reason} ->
"Error: #{reason}"
end