SUPPORT.TWILIO.COM END OF LIFE NOTICE: This site, support.twilio.com, is scheduled to go End of Life on February 27, 2024. All Twilio Support content has been migrated to help.twilio.com, where you can continue to find helpful Support articles, API docs, and Twilio blog content, and escalate your issues to our Support team. We encourage you to update your bookmarks and begin using the new site today for all your Twilio Support needs.

How to Create an Outbound Email Task (Agent-Initiated) with the Interactions API in Python

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, from and from_name are required channel properties.
  • Participants are mandatory for agent-initiated interactions.
  • For routing, both queue_sid and worker_sid must 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 

Interactions API Overview

Email Channel Parameters

Email in Flex for administrators

 

 

 

 

Have more questions? Submit a request
Powered by Zendesk