Let’s create a simple welcome bot that sends a welcome email to new users and answers questions about the product.

Prerequisites

  1. Create an inbox using the quickstart guide and set the webhook_url to the channel you created in the previous step
  2. Install the python client and get an api key - pip install botmailroom
  3. Install the openai client and get an api key - pip install openai

If you don’t want to use the python client, the REST API allows you to use the language of your choice. Tool schemas can be retrieved using the get tools endpoint.

Code

Make sure to replace YOUR_EMAIL_HERE with the actual email address you want to use for your inbox

import openai
from botmailroom import BotMailRoom

# setup the clients
openai_client = openai.OpenAI(api_key="your_openai_api_key")
botmailroom_client = BotMailRoom(api_key="your_botmailroom_api_key")

# change this to the email address you want to send emails to and respond with
TEST_EMAIL_ADDRESS = "YOUR_EMAIL_HERE"

# configuration parameters, feel free to change these
MAX_ITERATIONS = 10
MAX_EMAIL_WAIT_TIME_SECONDS = 30


# setup the system prompt and initialize the chat
system_prompt = f"""
- You are a helpful assistant that sends welcome emails and answers questions about the product, a teleportation machine.
- Do not create a new inbox or domain
- The user who has just signed up is {TEST_EMAIL_ADDRESS}.
- If they have not been sent a welcome email, you should send a welcome email.
- After sending an email, you should wait for a response from the user using the `wait_for_email`
- Once you are done waiting, look for a response with `get_emails`. If there is a response, you should reply to it with a helpful answer, otherwise, wait again.
- Make sure to include the `existing_email_id` in the `send_email` tool call if replying to an existing email.
- The only email address you should send emails to is {TEST_EMAIL_ADDRESS}.
- The maximum time you should wait for an email to arrive is {MAX_EMAIL_WAIT_TIME_SECONDS} seconds.
"""

chat = [
    {"role": "system", "content": system_prompt},
    {"role": "user", "content": f"{TEST_EMAIL_ADDRESS} has just signed up"},
]

iteration = 0
while iteration < MAX_ITERATIONS:
    # call the agent
    output = openai_client.chat.completions.create(
        model="gpt-4o",
        messages=chat,
        tools=botmailroom_client.get_tools(),
    )

    # execute the tool call if it exists
    if output.choices[0].message.tool_calls:
        chat.append(output.choices[0].message)
        tool_call = output.choices[0].message.tool_calls[0]
        print(f"Tool call: {tool_call.function.name} with args: {tool_call.function.arguments}")
        tool_output = botmailroom_client.execute_tool(
            tool_call.function.name, json.loads(tool_call.function.arguments), enforce_str_output=True
        )
        chat.append({"role": "tool", "content": tool_output, "tool_call_id": tool_call.id})
        print(f"Tool output: {tool_output}")
    else:
        chat.append(output.choices[0].message)
        print(f"Assistant message: {output.choices[0].message}")

    print("--------------------------------")
    iteration += 1

Once the agent has started running, you should receive emails in your inbox. If you respond to the emails, the agent will continue to respond to your emails.