Objective
This article explains how to create an agent-initiated outbound email task using the Twilio Interactions API in a Python application. The example uses the Twilio Python Helper Library to initiate an email interaction that generates an outbound TaskRouter task and routes it according to your Flex configuration.
Product
Twilio Flex
Procedure
Use the Twilio Python Helper Library to send requests to the Interactions API.
The following example shows how to create an outbound email interaction initiated by an agent. Replace placeholder SIDs, email addresses, and credentials with values from your Twilio project.
from twilio.rest import Client
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client(account_sid, auth_token)
def create_interaction():
interaction = client.flex_api.v1.interaction.create(
channel={
"type": "email",
"initiated_by": "agent", # Outbound email initiated by agent
# Required for agent-initiated email
"properties": {
"from": "support@example.com",
"from_name": "Support Team"
},
# Required for agent-initiated outbound interactions
"participants": [
{
"level": "to", # to | cc | bcc
"name": "John Doe",
"address": "customer@example.com"
}
]
},
routing={
"properties": {
"workspace_sid": "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"workflow_sid": "WWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"queue_sid": "WQXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", # required for agent-initiated
"worker_sid": "WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", # required for agent-initiated
"task_channel_unique_name": "email",
"attributes": {
"customerName": "John Doe",
"customerEmail": "customer@example.com",
"interactionType": "agent-outbound-email"
},
}
}
)
print("Outbound Email Interaction created!")
print("Interaction SID:", interaction.sid)
if __name__ == "__main__":
create_interaction()
-
initiated_by: "agent"creates an outbound task assigned to the agent who initiated the interaction. - For email interactions initiated by an agent or API,
fromandfrom_nameare required channel properties. - Participants are mandatory for agent-initiated interactions.
- For routing, both
queue_sidandworker_sidmust be provided for agent-initiated tasks. - All routing attributes are forwarded directly to TaskRouter and affect how the task is created and processed.
Additional Information
Email in Flex for administrators