Detect email language using email body text data
In this example, we'll use the Detect Language API
to detect the language of an inbound
email message. The detected language is the one that the service detects with the most
confidence, so it may not be the most prominent one if the email includes several languages.
Each API request requires a valid API key to be provided in the authorization header. In this
example, the API key is stored in Secrets manager in a secret named
detectlanguage-api.
In this example, the expected response's body payload format is an array of detection result objects consisting of language and score. For example:
[
{
"language": "en",
"score": 0.9098
},
{
"language": "es",
"score": 0.2304
}
]
For this integration, you'll need to configure the following:
- Channel type: Email
- Endpoint URL: https://ws.detectlanguage.com/v3/detect
- Conversation Direction: You can only use this example for inbound conversations.
- Events: Received
- Queues: Use the destination queue of the conversation.
- JSON for Configuration:
In this example, the condition is that there's at least 20 characters of plain-text email body text data available (when multiple consecutive whitespace characters replaced with single space). When the condition is met, according to the q property definition in the request body, at most 100 characters of plain-text email body text data (multiple consecutive whitespace chars replaced with single space) are used as input for the detection.
{ "request": { "condition": "len(' '.join(contact.get_body() or '')[:200].split()) >= 20", "method": "POST", "headers": { "accept": "application/json", "authorization": "Bearer {secrets.detectlanguage-api}" }, "body": { "q!sre('[\\s]{2,}',' ')[:100]": "{contact.email.body}" } }, "response": { "set": { "language": "{response.body[0].language}" } } }
